Home > Archive > PERL Beginners > October 2006 > scalar in array name?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
scalar in array name?
|
|
| Kenton Brede 2006-10-06, 6:57 pm |
| I've done some searching and can't find an answer to this. I'd like
to use a scalar variable in an array name. Something like "@$scalar"
I've tried different permutations like "\@$scalar", "@"$scalar""
"@\$scalar" and none of them work.
What I'm trying to do is come up with a way to slurp ARGV variables in
and if a match is found in @list, then print out the matching array,
either @rhel or @all. The following code is an example.
Thanks for any help,
Kent
-----------------------------------------------------------------------
#!/usr/bin/perl
use warnings;
use strict;
my $first_arg = "$ARGV[0]";
my @list = ("all", "rhel");
my @rhel = ("server1", "server3", "server4");
my @all = ("server2", "server5", server7");
foreach my $list_name (@list) {
if ( $list_name eq $first_arg ) {
print "@$first_arg";
}
}
-----------------------------------------------------------------------
| |
| Paul Lalli 2006-10-06, 6:57 pm |
| Kenton Brede wrote:
> I've done some searching and can't find an answer to this.
Apparenlty the official built-in Perl FAQ wasn't one of the places you
tried to search?
perldoc -q "variable name"
> I'd like
> to use a scalar variable in an array name.
No, you don't. You think you do. You have a problem, and you think
this is a good solution, but you don't know how to implement the
solution. So instead of asking us what the proper solution to your
problem is, you're asking us how to do your solution. This is known as
an XY problem.
> Something like "@$scalar"
> I've tried different permutations like "\@$scalar", "@"$scalar""
> "@\$scalar" and none of them work.
The very first one works perfectly well - on global variables, if and
only if you disable strict. Neither of those are a good idea, so don't
do them.
> What I'm trying to do is come up with a way to slurp ARGV variables in
> and if a match is found in @list, then print out the matching array,
> either @rhel or @all. The following code is an example.
Instead of two separate variables with those name, create a hash with
those keys, whose values are anonymous arrays.
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $first_arg = "$ARGV[0]";
There is no need for those quotes. Again, please see the FAQ:
perldoc -q quoting
> my @list = ("all", "rhel");
> my @rhel = ("server1", "server3", "server4");
> my @all = ("server2", "server5", server7");
my %lists = (
'rhel' => [qw/server1 server3 server4/],
'all' => [qw/server2 server5 server7/],
);
>
> foreach my $list_name (@list) {
> if ( $list_name eq $first_arg ) {
> print "@$first_arg";
> }
> }
Change all of this to:
if (exists $lists{$first_arg}) {
print "@{$lists{$first_arg}}";
}
Paul Lalli
| |
| Ricardo SIGNES 2006-10-06, 6:57 pm |
| * Kenton Brede <kbrede@gmail.com> [2006-10-06T11:38:49]
> I've done some searching and can't find an answer to this. I'd like
> to use a scalar variable in an array name. Something like "@$scalar"
> I've tried different permutations like "\@$scalar", "@"$scalar""
> "@\$scalar" and none of them work.
What you're talking about is a symbolic reference. The canonical reference for
why /not/ to do this is here:
http://perl.plover.com/varvarname.html
> my $first_arg = "$ARGV[0]";
> my @list = ("all", "rhel");
> my @rhel = ("server1", "server3", "server4");
> my @all = ("server2", "server5", server7");
>
> foreach my $list_name (@list) {
> if ( $list_name eq $first_arg ) {
> print "@$first_arg";
> }
> }
Use a hash instead:
my $first_arg = $ARGV[0];
my %group = (
rhel => [ 'server1', 'server3', 'server4' ],
all => [ 'server2', 'server5', 'server7' ],
);
my @servers = @{ $group{ $first_arg } };
print "@servers";
--
rjbs
| |
| Dr.Ruud 2006-10-06, 6:57 pm |
| "Kenton Brede" schreef:
> I've done some searching and can't find an answer to this. I'd like
> to use a scalar variable in an array name. Something like "@$scalar"
> I've tried different permutations like "\@$scalar", "@"$scalar""
> "@\$scalar" and none of them work.
>
> What I'm trying to do is come up with a way to slurp ARGV variables in
> and if a match is found in @list, then print out the matching array,
> either @rhel or @all. The following code is an example.
>
> ----------------------------------------------------------------------
-
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $first_arg = "$ARGV[0]";
Why the quotes?
> my @list = ("all", "rhel");
> my @rhel = ("server1", "server3", "server4");
> my @all = ("server2", "server5", server7");
>
> foreach my $list_name (@list) {
> if ( $list_name eq $first_arg ) {
> print "@$first_arg";
> }
> }
> ----------------------------------------------------------------------
-
Dispatch:
my %list =
(
all => \@all,
rhel => \@rhel,
)
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| DJ Stunks 2006-10-06, 6:57 pm |
| Kenton Brede wrote:
> I've done some searching and can't find an answer to this. I'd like
> to use a scalar variable in an array name. Something like "@$scalar"
> I've tried different permutations like "\@$scalar", "@"$scalar""
> "@\$scalar" and none of them work.
>
> What I'm trying to do is come up with a way to slurp ARGV variables in
> and if a match is found in @list, then print out the matching array,
> either @rhel or @all. The following code is an example.
> Thanks for any help,
> Kent
>
> -----------------------------------------------------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $first_arg = "$ARGV[0]";
> my @list = ("all", "rhel");
> my @rhel = ("server1", "server3", "server4");
> my @all = ("server2", "server5", server7");
>
> foreach my $list_name (@list) {
> if ( $list_name eq $first_arg ) {
> print "@$first_arg";
> }
> }
> -----------------------------------------------------------------------
What you want to do is a very common thought, but ultimately a poor
decision. Use a hash instead:
#!/usr/bin/perl
use strict;
use warnings;
my %list = (
rhel => [ qw{ server1 server3 server4 } ],
all => [ qw{ server2 server5 server7 } ],
);
my $lookup = shift;
if ( not defined $lookup ) {
usage();
}
elsif ( exists $list{ $lookup } ) {
local $, = ', ';
print @{ $list{ $lookup } };
}
else {
print "Sorry, '$lookup' not found.\n";
usage();
}
sub usage {
printf "Usage: $0 {%s}\n", join ', ', keys %list;
exit 1;
}
__END__
| |
| Eugene Kosov 2006-10-08, 6:58 pm |
| I think eval can help you...
See perldoc -f eval.
Kenton Brede wrote:
> I've done some searching and can't find an answer to this. I'd like
> to use a scalar variable in an array name. Something like "@$scalar"
> I've tried different permutations like "\@$scalar", "@"$scalar""
> "@\$scalar" and none of them work.
>
> What I'm trying to do is come up with a way to slurp ARGV variables in
> and if a match is found in @list, then print out the matching array,
> either @rhel or @all. The following code is an example.
> Thanks for any help,
> Kent
>
> -----------------------------------------------------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $first_arg = "$ARGV[0]";
> my @list = ("all", "rhel");
> my @rhel = ("server1", "server3", "server4");
> my @all = ("server2", "server5", server7");
>
> foreach my $list_name (@list) {
> if ( $list_name eq $first_arg ) {
> print "@$first_arg";
> }
> }
> -----------------------------------------------------------------------
>
| |
| John W. Krahn 2006-10-08, 6:58 pm |
| Eugene Kosov wrote:
> I think eval can help you...
>
> See perldoc -f eval.
Or see:
perldoc -q "How can I use a variable as a variable name"
To find out why that is not really a good idea at all.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Dr.Ruud 2006-10-08, 6:58 pm |
| Eugene Kosov schreef:
> I think eval can help you...
....to leap into the fire?
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
|
|
|