Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Getting Value from joining 2 variables

by ELISHEVA (Prior)
on Jan 02, 2011 at 09:07 UTC ( #880061=note: print w/replies, xml ) Need Help??


in reply to Getting Value from joining 2 variables

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++; }

Replies are listed 'Best First'.
Re^2: Getting Value from joining 2 variables
by moritz (Cardinal) on Jan 02, 2011 at 13:30 UTC
    "$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.

Re^2: Getting Value from joining 2 variables
by mellon85 (Monk) on Jan 02, 2011 at 11:31 UTC
    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;

        ... I also included: use strict; use warnings;

        Really?  With use strict it should've stopped working

        #!/usr/bin/perl use strict; use warnings; our $pax_1 = "foo"; my $plus = 1; my $name = "pax_$plus"; my $paxing = ${$name}; print "$paxing\n"; __END__ Can't use string ("pax_1") as a SCALAR ref while "strict refs" in use +at ./880063.pl line 8.

        unless you also disable strict refs, e.g. as follows

        #!/usr/bin/perl use strict; use warnings; our $pax_1 = "foo"; my $plus = 1; my $name = "pax_$plus"; my $paxing; { no strict 'refs'; $paxing = ${$name}; } print "$paxing\n"; # "foo"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-12-05 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?