CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Hashes - variable substitution

 



new_user3085
New User

Sep 18, 2017, 5:42 AM

Post #1 of 6 (321 views)
Hashes - variable substitution Can't Post

Hi All,

I'm trying to print all the hashes using a for loop and using variable substitution, but it won't work. Could some one please take a look and guide me on where I'm going wrong. Thanks in advance.


Code
#!/usr/bin/perl 
$data1{'age1'} = 31;
$data1{'age2'} = 21;
$data1{'age3'} = 11;

%data2 = ( 'Mike', 2, 'Jeffrey', 3, 'Matt', 5);
%data3 = ( 'Mike_Sht' => 21, 'Jeffrey_Sht' => 31, 'Matt_Sht' => 51);

for my $a (1..3) {
print "\$data1{'age$a'} = $data1{'age$a'}\n"; #the values won't print here but print statement after the for loop works. Why?
}

print "\$data1{'age1'} = $data1{'age1'}\n";




Output:

$data1{'age1'} =
$data1{'age2'} =
$data1{'age3'} =
$data1{'age1'} = 31


(This post was edited by new_user3085 on Sep 18, 2017, 6:01 AM)


FishMonger
Veteran / Moderator

Sep 18, 2017, 8:17 AM

Post #2 of 6 (310 views)
Re: [new_user3085] Hashes - variable substitution [In reply to] Can't Post

Putting single quotes around the vars is preventing interpolation. You need to use double quotes.


Code
print qq(\$data1{"age$a"} = $data1{"age$a"}\n);



Laurent_R
Veteran / Moderator

Sep 18, 2017, 10:41 AM

Post #3 of 6 (304 views)
Re: [new_user3085] Hashes - variable substitution [In reply to] Can't Post

FishMonger has given you the right answer.

As a side comment, please note that $a and $b are poor names for variables, because they have a special meaning (used notably for sorting values and a few other things). But you may use $c or $d if you want.

Also you should use the:

Code
use strict; 
use warnings;

pragmas and declare your variables (usually with the key word my) before you use them. They will enable Perl to detect many of your errors (often at compile time) and save you a lot of debugging time.


new_user3085
New User

Sep 19, 2017, 8:00 AM

Post #4 of 6 (287 views)
Re: [FishMonger] Hashes - variable substitution [In reply to] Can't Post

Thank you @FishMonger.


new_user3085
New User

Sep 19, 2017, 8:04 AM

Post #5 of 6 (283 views)
Re: [Laurent_R] Hashes - variable substitution [In reply to] Can't Post

Thank you @Laurent_R.


BillKSmith
Veteran

Sep 23, 2017, 6:32 AM

Post #6 of 6 (262 views)
Re: [new_user3085] Hashes - variable substitution [In reply to] Can't Post

Sorry for my late reply, I have been ill.

A better data structure eliminates the problem.


Code
C:\Users\Bill\forums\guru>type user3085.pl 
#!/usr/bin/perl
#perlguru post=84345
use strict;
use warnings;

my %data1 = (
age => [ undef, 31, 21, 11, ],
);

print "\$data1{age}[$_] = $data1{age}[$_]\n" foreach 1..3;

C:\Users\Bill\forums\guru>perl user3085.pl
$data1{age}[1] = 31
$data1{age}[2] = 21
$data1{age}[3] = 11

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives