Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Derive a variable name from a variable?

by mavericknik (Sexton)
on Dec 17, 2015 at 04:38 UTC ( #1150579=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, So here is my question: I have an array of names, say
my @names = qw(adam jake john betty);
And for each name, I have a corresponding array,
my @adambooks; my @jakebooks; . . .
Now I'm trying to do:
foreach (@names) { my $cur_name = $_; &modify($cur_name, "Their correspinding array") }
What is the easiest way to get the value of "Their corresponding array"?? I expect the answer is simple buy I'm pretty new to perl and cant for the life of me figure it out. Any pointers are greatly appreciated!

Replies are listed 'Best First'.
Re: Derive a variable name from a variable?
by GrandFather (Saint) on Dec 17, 2015 at 06:41 UTC

    Use a hash instead:

    #!/usr/bin/perl use warnings; use strict; my %booksByBorrower = ( adam => {"Learning Perl" => {borrowed => '2015-12-17'}}, jake => {"Programming Perl" => {borrowed => '2015-12-08'}}, john => {"Programming Perl" => {borrowed => '2015-12-12'}}, betty => {"High Order Perl" => {borrowed => '2015-12-10'}}, ); for my $name (sort keys %booksByBorrower) { print "$name\n"; for my $book (sort keys %{$booksByBorrower{$name}}) { print " Borrowed $book $booksByBorrower{$name}{$book}{borrowe +d}\n"; } }

    Prints:

    adam Borrowed Learning Perl 2015-12-17 betty Borrowed High Order Perl 2015-12-10 jake Borrowed Programming Perl 2015-12-08 john Borrowed Programming Perl 2015-12-12

    Of course for this sort of task you should really be using a database, but that's a little heavy for such a trivial example.

    Premature optimization is the root of all job security
      Thank you for the pointers, I used an array reference as the hash value to solve the problem. Yes, I need to look into a better way of doing this. This is an oversimplified example I used to explain my problem, the actual size of the code is pretty big.
Re: Derive a variable name from a variable?
by Laurent_R (Canon) on Dec 17, 2015 at 07:18 UTC
    Don't write Perl code that computes variable names. It can be done (it is called symbolic references), but it is bad practice, it is frowned upon for several good reasons, and it is bad enough that it will trigger a warning.

    Use a hash instead.

    Update: Just as you can shoot a bullet into your own foot or put your cat into the microwave oven, but you should not do it.

Re: Derive a variable name from a variable?
by Anonymous Monk on Dec 17, 2015 at 04:46 UTC
Re: Derive a variable name from a variable?
by Lennotoecom (Pilgrim) on Dec 17, 2015 at 06:47 UTC
    there is another way,
    but that is a path to the dark side of the force
    @names = qw/adam jake john betty/; @adambooks = qw/ad_1 ad_2/; @jakebooks = qw/ja_1 ja_2/; for $n (@names){ print "$n:\n"; print "\t@{$_}\n" for $n."books"; }

    there might be some jedi,
    who'd tell you that it's wrong
    they just want you to obey their will
      This is only half the answer because using strict is a standard nowadays.

      There might be cases where this is needed, but please apply no strict "refs" in a restricted block around it and use strict; in file scope.

      see for instance no strict refs for blocks?

      > there might be some jedi,

      > who'd tell you that it's wrong

      > they just want you to obey their will

      Perl has no taboos, just best practices.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      Haha I would've been seduced by the dark side if it weren't for the wisdom of the monks here!
        ^_^

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2022-11-28 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?