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 | |
by ELISHEVA (Prior) on Jan 02, 2011 at 13:47 UTC | |
|
Re^2: Getting Value from joining 2 variables
by mellon85 (Monk) on Jan 02, 2011 at 11:31 UTC | |
by CountZero (Bishop) on Jan 02, 2011 at 13:03 UTC | |
by Noverast (Initiate) on Jan 02, 2011 at 15:06 UTC | |
by Anonyrnous Monk (Hermit) on Jan 02, 2011 at 15:33 UTC |