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 : Edit Log



new_user3085
New User

Sep 18, 2017, 5:42 AM


Views: 1154
Hashes - variable substitution

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)


Edit Log:
Post edited by new_user3085 (New User) on Sep 18, 2017, 6:01 AM


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

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