Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > access variables from array and set it back

Reply
Thread Tools

access variables from array and set it back

 
 
Slickuser
Guest
Posts: n/a
 
      04-24-2009
I have these variables:
my $path1 = 'c:\abc';
my $filea = 'Y:\a.txt';
my $namex = 'Dr. X';

I want to push this into a hash array or array of variables & values.

Later on, I want loop through these array and modify the value and set
back to the variable.


do a loop now
get variable back
change value

$path1 = 'c:\newpath';
$filea = 'Y:\newfilea.txt';
$namex = 'Dr. Y';

How can I achieve something like this? Thanks.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-24-2009
Slickuser <(E-Mail Removed)> wrote:
>I have these variables:
>my $path1 = 'c:\abc';
>my $filea = 'Y:\a.txt';
>my $namex = 'Dr. X';
>
>I want to push this into a hash array or array of variables & values.


May I suggest that you get your terminology straightened out first? It
makes communication so much easier if everyone uses the same terms for
the same things.

There are hashes: they are mappings from strings to scalars. In the old
days they were also called associative arrays.
There are arrays: they are mappings from (consecutive whole) numbers to
scalars.
There are variables: they can be any of scalar, array, hash, and a few
other types.
And there are values: they are typically stored in variables.

Now, having said that, there are no arrays of variables.

>Later on, I want loop through these array and modify the value and set
>back to the variable.
>
>do a loop now
>get variable back
>change value
>
>$path1 = 'c:\newpath';
>$filea = 'Y:\newfilea.txt';
>$namex = 'Dr. Y';
>
>How can I achieve something like this? Thanks.


Wild guess: maybe you are looking for a simple hash?
%myhash = (path1 => 'c:\newpath',
filea => 'Y:\newfilea.txt',
namex => 'Dr. Y};

You can loop through the entries by
foreach (keys(%myhash) {
print $myhash{$_};
}
And of course you can assign and modify the values as you please, too.

Or are you looking for references, i.e. you want to store a reference to
each of your variables in such a hash or an array and then work with
those references?

jue
 
Reply With Quote
 
 
 
 
Slickuser
Guest
Posts: n/a
 
      04-25-2009
I want to reference the variables and change the value to that
reference variable.

On Apr 24, 4:29*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> Slickuser <(E-Mail Removed)> wrote:
> >I have these variables:
> >my $path1 = 'c:\abc';
> >my $filea = 'Y:\a.txt';
> >my $namex = 'Dr. X';

>
> >I want to push this into a hash array or array of variables & values.

>
> May I suggest that you get your terminology straightened out first? It
> makes communication so much easier if everyone uses the same terms for
> the same things.
>
> There are hashes: they are mappings from strings to scalars. In the old
> days they were also called associative arrays.
> There are arrays: they are mappings from (consecutive whole) numbers to
> scalars.
> There are variables: they can be any of scalar, array, hash, and a few
> other types.
> And there are values: they are typically stored in variables.
>
> Now, having said that, there are no arrays of variables.
>
> >Later on, I want loop through these array and modify the value and set
> >back to the variable.

>
> >do a loop now
> >get variable back
> >change value

>
> >$path1 = 'c:\newpath';
> >$filea = 'Y:\newfilea.txt';
> >$namex = 'Dr. Y';

>
> >How can I achieve something like this? Thanks.

>
> Wild guess: maybe you are looking for a simple hash?
> %myhash = (path1 => 'c:\newpath',
> * * * * * * * * filea => 'Y:\newfilea.txt',
> * * * * * * * * namex => 'Dr. Y};
>
> You can loop through the entries by
> * * * * foreach (keys(%myhash) {
> * * * * * * * * print $myhash{$_};
> * * * * }
> And of course you can assign and modify the values as you please, too.
>
> Or are you looking for references, i.e. you want to store a reference to
> each of your variables in such a hash or an array and then work with
> those references?
>
> jue


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-25-2009
[Re-ordered into standard reading order]
Slickuser <(E-Mail Removed)> wrote:
>On Apr 24, 4:29*pm, Jürgen Exner <(E-Mail Removed)> wrote:
>> Slickuser <(E-Mail Removed)> wrote:
>> >I have these variables:
>> >my $path1 = 'c:\abc';
>> >my $filea = 'Y:\a.txt';
>> >my $namex = 'Dr. X';

>>
>> >I want to push this into a hash array or array of variables & values.

>>
>> May I suggest that you get your terminology straightened out first? It
>> makes communication so much easier if everyone uses the same terms for
>> the same things.

[...]
>> Or are you looking for references, i.e. you want to store a reference to
>> each of your variables in such a hash or an array and then work with
>> those references?


>I want to reference the variables and change the value to that
>reference variable.


Then please see "perldoc perlreftut" and "perldoc perlref".

jue
 
Reply With Quote
 
Slickuser
Guest
Posts: n/a
 
      04-27-2009
my $path1 = 'c:\abc';
my $filea = 'Y:\a.txt';
my $namex = 'Dr. X';

My goal is get from the top the end.

$path1 = 'c:\newpath';
$filea = 'Y:\newfilea.txt';
$namex = 'Dr. Y';


The top will assign by manually by the user, but the bottom one will
somehow automate re-assign the value. I don't want to do one by one.
That's why I'm thinking of putting in a hash and loop and modify the
value. This is where I'm stuck.

Mostly I just do a replace and search but I don't know which variable
to set it back to.

How would I achieve this in a loop?
$HASHX{'path1'} = $path1;
$HASHX{'filea '} = $filea;



On Apr 24, 6:20*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> [Re-ordered into standard reading order]
>
>
>
> Slickuser <(E-Mail Removed)> wrote:
> >On Apr 24, 4:29*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> >> Slickuser <(E-Mail Removed)> wrote:
> >> >I have these variables:
> >> >my $path1 = 'c:\abc';
> >> >my $filea = 'Y:\a.txt';
> >> >my $namex = 'Dr. X';

>
> >> >I want to push this into a hash array or array of variables & values.

>
> >> May I suggest that you get your terminology straightened out first? It
> >> makes communication so much easier if everyone uses the same terms for
> >> the same things.

> [...]
> >> Or are you looking for references, i.e. you want to store a reference to
> >> each of your variables in such a hash or an array and then work with
> >> those references?

> >I want to reference the variables and change the value to that
> >reference variable.

>
> Then please see "perldoc perlreftut" and "perldoc perlref".
>
> jue


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-27-2009
perl -le '
$path1 = "c:\\abc";
$filea = "Y:\\a.txt";
$namex = "Dr. X";
@refs = \($path1, $filea, $namex);
@values = ("c:\\newpath", "Y:\\newfilea.txt", "Dr. Y");
${ $refs[$_] } = $values[$_] for 0..2;
print for $path1, $filea, $namex;
'
c:\newpath
Y:\newfilea.txt
Dr. Y

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-27-2009
[Re-ordered AGAIN into standard reading order]
[Please do not top post]
Slickuser <(E-Mail Removed)> wrote:
>On Apr 24, 6:20*pm, Jürgen Exner <(E-Mail Removed)> wrote:
>> [Re-ordered into standard reading order]
>> Slickuser <(E-Mail Removed)> wrote:
>> >On Apr 24, 4:29*pm, Jürgen Exner <(E-Mail Removed)> wrote:
>> >> Slickuser <(E-Mail Removed)> wrote:
>> >> >I have these variables:
>> >> >my $path1 = 'c:\abc';
>> >> >my $filea = 'Y:\a.txt';
>> >> >my $namex = 'Dr. X';

>>
>> >> >I want to push this into a hash array or array of variables & values.

>>
>> >> May I suggest that you get your terminology straightened out first? It
>> >> makes communication so much easier if everyone uses the same terms for
>> >> the same things.

>> [...]
>> >> Or are you looking for references, i.e. you want to store a reference to
>> >> each of your variables in such a hash or an array and then work with
>> >> those references?
>> >I want to reference the variables and change the value to that
>> >reference variable.

>>
>> Then please see "perldoc perlreftut" and "perldoc perlref".
>>
>> jue

>my $path1 = 'c:\abc';
>my $filea = 'Y:\a.txt';
>my $namex = 'Dr. X';
>
>My goal is get from the top the end.
>
>$path1 = 'c:\newpath';
>$filea = 'Y:\newfilea.txt';
>$namex = 'Dr. Y';
>
>
>The top will assign by manually by the user, but the bottom one will
>somehow automate re-assign the value. I don't want to do one by one.
>That's why I'm thinking of putting in a hash and loop and modify the
>value. This is where I'm stuck.


Sorry, my EPS:SI capabilities are very limited and I am very poor at
guessing what other people might have meant.
Someone else will have to decode the paragraph above and translate it
into plain English. I have no idea what you mean by that.

>Mostly I just do a replace and search but I don't know which variable
>to set it back to.
>
>How would I achieve this in a loop?
>$HASHX{'path1'} = $path1;
>$HASHX{'filea '} = $filea;


I don't get it. Are you looking for references are you looking for loops
or what? One day you are saying X and the next day you are saying Y. I
am lost and am giving up now.

jue
 
Reply With Quote
 
Slickuser
Guest
Posts: n/a
 
      04-27-2009
This is what I want.

Can do I this in a loop?

$path1 = "c:\\abc";
$filea = "Y:\\a.txt";
$namex = "Dr. X";
@refs = \($path1, $filea, $namex);

I don't want to the set the values right away. Just loop through refs
then doing search & replace.
And set this value to the variable.

I'll look at this example and modify it. thank you.

On Apr 26, 10:16*pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> perl -le '
> $path1 = "c:\\abc";
> $filea = "Y:\\a.txt";
> $namex = "Dr. X";
> @refs = \($path1, $filea, $namex);
> @values = ("c:\\newpath", "Y:\\newfilea.txt", "Dr. Y");
> ${ $refs[$_] } = $values[$_] for 0..2;
> print for $path1, $filea, $namex;
> '
> c:\newpath
> Y:\newfilea.txt
> Dr. Y
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-27-2009
Slickuser wrote:
> Gunnar Hjalmarsson wrote:
>> perl -le '
>> $path1 = "c:\\abc";
>> $filea = "Y:\\a.txt";
>> $namex = "Dr. X";
>> @refs = \($path1, $filea, $namex);
>> @values = ("c:\\newpath", "Y:\\newfilea.txt", "Dr. Y");
>> ${ $refs[$_] } = $values[$_] for 0..2;
>> print for $path1, $filea, $namex;
>> '
>> c:\newpath
>> Y:\newfilea.txt
>> Dr. Y

>
> This is what I want.
>
> Can do I this in a loop?
>
> $path1 = "c:\\abc";
> $filea = "Y:\\a.txt";
> $namex = "Dr. X";
> @refs = \($path1, $filea, $namex);
>
> I don't want to the set the values right away. Just loop through refs
> then doing search & replace.


You have three scalar variables, so search and replace does not make
sense IMO. Either you reassign the variables directly, or you reassign
them indirectly via references.

Or did I miss something?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
can a method access/set another's variables? asdf1234234 Python 11 04-02-2007 04:51 PM
how do i pass an array to a function .. and also return an array back from the func???? bhejafry.mmk@gmail.com C Programming 3 11-30-2006 05:05 PM
Array Integers vs Array Variables Rob Meade ASP General 16 02-08-2004 12:46 PM



Advertisments