Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Assigning variables via looping

by gwadej (Chaplain)
on Jun 02, 2009 at 19:48 UTC ( #767755=note: print w/replies, xml ) Need Help??


in reply to Assigning variables via looping

You are using symbolic references, which cannot point to lexical (my) variables. (use strict would have told you that.)

In general, this is a path to much pain. You would (usually) be better off using a hash. (There are a very few cases where the symbolic references might be a better choice, but the odds of this being one of them is really slim.

my %greeting; my @names = ('peter', 'jonah', 'larry'); foreach my $n (@names) { $greeting{$n} = "i like $n"; } print "$greeting{peter} \n $greeting{jonah} \n $greeting{larry} \n";

Of course, there's always more than one way.

my %greeting = map { $_ => "i like $_" } ('peter', 'jonah', 'larry'); print join( " \n ", @greeting{qw/peter jonah larry/} ), " \n";
G. Wade

Replies are listed 'Best First'.
Re^2: Assigning variables via looping
by Errto (Vicar) on Jun 02, 2009 at 21:14 UTC
    (use strict would have told you that.)

    Well, use strict wouldn't let you use symbolic references at all. Even if you declared @names, $jonah, $peter, and $larry. Though I guess if you only enable strict for vars, not refs, that might tell you something.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://767755]
help
Chatterbox?
[Discipulus]: a solution would be disabiltate sig as default and let users to show them
[karlgoethebier]: now it's getting weird...
[GotToBTru]: 'getting'?
[Discipulus]: why karlgoethebier ?
[GotToBTru]: ugh .. what's the term for comparing versions of an algorithm to determine which is most efficient?

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2017-06-22 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (519 votes). Check out past polls.