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

Getting Value from joining 2 variables

by Noverast (Initiate)
on Jan 02, 2011 at 08:20 UTC ( #880060=perlquestion: print w/replies, xml ) Need Help??

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

Please help me with the following. Here is my code:

$pax_1="180"; $pax_2="260"; $pax_3="300" $pax_4="450"; $maxadults = "3"; $plus2 = "1"; $currencycode = "ZAR"; while ($maxadults >= $plus) { $paxing = "$pax_$plus"; $sleeps = $sleeps . "$currencycode$paxing for $plus persons<br>"; $plus++; }

I keep getting ZAR1 for 1 persons, ZAR2 for 2 persons, etc.. I've tried '' but then I get R$pax_$plus for 1 persons Pleas help.

Replies are listed 'Best First'.
Re: Getting Value from joining 2 variables
by ELISHEVA (Prior) on Jan 02, 2011 at 09:07 UTC

    Welcome to PerlMonks and Happy New Year!

    It looks like you might be confusing variable names and strings. "$pax_$plus" simply creates a string. If $pax is "foo", and $plus is "100", then "$pax_$plus" creates the string "foo_100" "100" , not the variable $foo_100. (see moritz's reply below for why the resulting string is "100" and not "foo_100")

    When you want to retrieve data associated with a series of consecutive numbers (i.e. 1 to 4), you need to use an array, like this:

    # use these lines at the start to get Perl help you find # bugs use strict; use warnings; # Note: declare your variables with "my" # This helps Perl catch any misspellings in variable names # and reminds you to set initial values for everything # price for 0, 1, 2, 3, 4 persons my @pax = (0, 100, 260, 300, 450); my $plus = 1; my $currencycode = 'ZAR'; my $sleeps=''; my $maxadults = 4; while ($maxadults >= $plus) { my $paxing = $pax[$plus]; #get $plus member of @pax array $sleeps = $sleeps . "$currencycode$paxing for $plus persons<br>"; $plus++; }
      "$pax_$plus" simply creates a string. If $pax is "foo", and $plus is "100", then "$pax_$plus" creates the string "foo_100"

      Not at all. It tries to look up the variable $pax_ (since an underscore is valid part of a variable name), and fails (and thus interpolates the empty string for $pax_).

      use strict; use warnings; would have caught the problem, and can only be highly recommended to Noverast.

        Good catch!

        I frequently trip on that one and also $sClass::Blah when I mean ${sClass}::Blah. Thanks.

      or you could just be using
      my $name = "pax_$plus"; $paxing = ${$name};
      as it works without using strict refs like you do. But consider using strict and warning
        Very naughty. This is actually code which --undeservedly-- gives Perl a bad name.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Thanks, This did the trick. I also included: use strict; use warnings;

Re: Getting Value from joining 2 variables
by CountZero (Bishop) on Jan 02, 2011 at 13:07 UTC
    You set the variable $plus2 to one, but then you never use it and use $plus.

    This type of error is one of the main reasons why you always should use strict; and use warnings; or useModern::Perl; (if you are using Perl 5.10 or later).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Getting Value from joining 2 variables
by fisher (Priest) on Jan 03, 2011 at 09:23 UTC
    BTW, why don't you use the '.=' operator? This line
    $sleeps = $sleeps . "$currencycode$paxing for $plus persons<br>";
    transforms to
    $sleeps .= "$currencycode$paxing for $plus persons<br>";
    ...and looks better, isn't it?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-12-08 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?