![]() |
access variables from array and set it back
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. |
Re: access variables from array and set it back
Slickuser <slick.users@gmail.com> 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 |
Re: access variables from array and set it back
I want to reference the variables and change the value to that
reference variable. On Apr 24, 4:29*pm, Jürgen Exner <jurge...@hotmail.com> wrote: > Slickuser <slick.us...@gmail.com> 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 |
Re: access variables from array and set it back
[Re-ordered into standard reading order]
Slickuser <slick.users@gmail.com> wrote: >On Apr 24, 4:29*pm, Jürgen Exner <jurge...@hotmail.com> wrote: >> Slickuser <slick.us...@gmail.com> 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 |
Re: access variables from array and set it back
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 <jurge...@hotmail.com> wrote: > [Re-ordered into standard reading order] > > > > Slickuser <slick.us...@gmail.com> wrote: > >On Apr 24, 4:29*pm, Jürgen Exner <jurge...@hotmail.com> wrote: > >> Slickuser <slick.us...@gmail.com> 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 |
Re: access variables from array and set it back
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 |
Re: access variables from array and set it back
[Re-ordered AGAIN into standard reading order]
[Please do not top post] Slickuser <slick.users@gmail.com> wrote: >On Apr 24, 6:20*pm, Jürgen Exner <jurge...@hotmail.com> wrote: >> [Re-ordered into standard reading order] >> Slickuser <slick.us...@gmail.com> wrote: >> >On Apr 24, 4:29*pm, Jürgen Exner <jurge...@hotmail.com> wrote: >> >> Slickuser <slick.us...@gmail.com> 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::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 |
Re: access variables from array and set it back
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 <nore...@gunnar.cc> 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 |
Re: access variables from array and set it back
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 |
| All times are GMT. The time now is 07:01 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.