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";
}
| [reply] [d/l] [select] |
Re: storing variable names in an array
by kirbyk (Friar) on Jun 10, 2005 at 20:10 UTC
|
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.
| [reply] [d/l] [select] |
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) | [reply] |
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.
| [reply] [d/l] |
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
| [reply] [d/l] |
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. | [reply] |
|
| [reply] |
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.. | [reply] [d/l] |
|
This would work if the array was an array of var names, but the OP has them as just strings..
| [reply] |
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};
| [reply] [d/l] |