Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

storing variable names in an array

by scifiaddict (Initiate)
on Jun 10, 2005 at 20:00 UTC ( [id://465662]=perlquestion: print w/replies, xml ) Need Help??

scifiaddict has asked for the wisdom of the Perl Monks concerning the following question:

Howdy,

I'm storing a group of strings in an array that are names of variables. Something like ......
my $file1 = "tom"; my $file2 = "bob"; my $file3 = "joe"; my @names = ("file1", "file2", "file3");
I now want to change the value of $file2 from "bob" to "sam", but I would like to do it using only the information in the array? Seems like it should be easy, just can't figure it out.

Thanks.

Replies are listed 'Best First'.
Re: storing variable names in an array
by thundergnat (Deacon) on Jun 10, 2005 at 20:09 UTC

    Unless you need the names of the files in some specific order that is not easily obtained through sorting, you are much better off storing the file names in a hash.

    my %names = (file1 => "tom", file2 => "bob", file3 => "joe");

    Then if you need to change what file2 points to just do it directly.

    $names{file2} = "sam";

    To iterate over the %names...

    for (keys %name){ print "$_ = $names{$_}\n"; }
Re: storing variable names in an array
by kirbyk (Friar) on Jun 10, 2005 at 20:10 UTC
    It's possible to do that, but it's not really the easiest way.

    Consider using a hash instead:

    my %files = ( 'file1' => 'tom', 'file2' => 'bob', 'file3' => 'joe', ); $files{'file2'} = 'sam';
    Alternately, just store your filenames in the array (if order is important), like:
    my @names = ('tom','bob','joe'); $names[1] = 'sam';
    Adding the additional step of the array of variable names is usally the wrong path to go down.

    -- Kirby, WhitePages.com

Re: storing variable names in an array
by davidrw (Prior) on Jun 10, 2005 at 20:38 UTC
    The hash suggestions above are good ones .. for reasons not to use other 'solutions', see this thread: Using a scalar as an array name
    (and there's been a couple/few nodes since that one, too, on the same topic but i don't have them handy)
Re: storing variable names in an array
by tlm (Prior) on Jun 10, 2005 at 21:24 UTC

    In addition to the hash-based solutions offered, there's also

    my @file = qw( tom bob joe );
    so instead of referring to, e.g., $file1, you'd refer to $file[0]. You can change the values of these variables programmatically, by maniplulating the integers corresponding to the array indices. This node has some interesting thoughts on the perennial desire to manipulate variable names programmatically.

    the lowliest monk

Re: storing variable names in an array
by Joe_Cullity (Beadle) on Jun 10, 2005 at 20:33 UTC
    Sound like you'd be better off using a hash table
    #!/usr/bin/perl %a_hash = ( "file1", "tom", "file2", "bob", "file3", "joe"); $vale_from_hash = $a_hash{"file2"}; # Should return "bob" $a_hash{"file2"} = "Sam"; # file2 should now be "Sam" $vale_from_hash = $a_hash{"file2"}; # Should now return "Sam" print( %a_hash ); # Pairs will be matched but usually out of order you + expect
Re: storing variable names in an array
by djohnston (Monk) on Jun 10, 2005 at 23:18 UTC
    You can probably accomplish what you're asking using soft references. This is considered bad form, however, and as many have pointed out already, you should use a hash instead.

      Perldoc perlref specifically asks you not to call them "soft references". They're symbolic references. Just like in unix, you have hard links and symbolic links (symlinks).

Re: storing variable names in an array
by cool_jr256 (Acolyte) on Jun 10, 2005 at 20:09 UTC
    Hope I understood your question...
    $names[1]="sam";
    That would replace "bob" with "sam" in the array.. but then again it's so hot in my office my brain is going crazy..
      This would work if the array was an array of var names, but the OP has them as just strings..
Re: storing variable names in an array
by dReKurCe (Scribe) on Jun 11, 2005 at 21:42 UTC
    Greetings:
    The following code uses refs to access the scalar variables:
    our $file1 ="tom"; our $file2 = "bob"; our $file3 = "joe"; my @names =( "file1", "file2", "file3" ); $ref=\@names; $file=$ref->[0]; print ${$file};

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://465662]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2025-05-18 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.