access variables from array and set it back

S

Slickuser

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.
 
J

Jürgen Exner

Slickuser said:
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
 
S

Slickuser

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

Jürgen Exner

[Re-ordered into standard reading order]
Slickuser said:
Slickuser said:
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
 
S

Slickuser

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;



[Re-ordered into standard reading order]



Slickuser said:
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
 
G

Gunnar Hjalmarsson

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
 
J

Jürgen Exner

[Re-ordered AGAIN into standard reading order]
[Please do not top post]
Slickuser said:
[Re-ordered into standard reading order]
Slickuser said:
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::pSI 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
 
S

Slickuser

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.
 
G

Gunnar Hjalmarsson

Slickuser said:
Gunnar said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top