0

I am trying to learn complex data structure in perl, for that i i have written a code but i am not getting any output :

#!/usr/bin/perl 
use strict;
use warnings;

my %abc=(Education => "BE",Marital_Status => "Single", Age => "28", Working_Exp => "4yrs");
my %cde=(Education => "BE",Marital_Status => "Single", Age => "29", Working_Exp => "5yrs");


my %info =(info_one => "\%abc", info_two => "\%cde");

foreach my $val (keys %info)
{
  foreach my $check ( keys %{$info{val}})
  {
    print ${$info{val}}{check}."\n";
  }
}
Maverick
  • 575
  • 1
  • 11
  • 27
  • Use anonymous hashes that will help you to store and manage data. – Naghaveer R Feb 27 '14 at 05:38
  • @NaghaveerRGowda: you mean, use "[hashrefs](http://stackoverflow.com/questions/1817394/whats-the-difference-between-a-hash-and-hash-reference-in-perl)", and you want to be more precise than saying "will help you to store and manage data" :) – Dan Dascalescu Feb 27 '14 at 06:23
  • 1
    @DanDascalescu: yes Its "hashrefs" and Thanks Dan :) – Naghaveer R Feb 27 '14 at 07:22

1 Answers1

5

For learning complex data structures in Perl, there's no better reference than the Perl Data Structures Cookbook.

The assignments to info_one and info_two are strings, so you want to remove the double quotes from around \%abc and \%cde. Also, you need to add the $ scalar sign to val and check in the last print line, because those are variables.

#!/usr/bin/perl 
use strict;
use warnings;

my %abc= (
    Education => "BE",
    Marital_Status => "Single", 
    Age => "28", 
    Working_Exp => "4yrs"
);
my %cde= (
    Education => "BE",
    Marital_Status => "Single", 
    Age => "29", 
    Working_Exp => "5yrs"
);


my %info = (
   info_one => \%abc,
   info_two => \%cde
);

foreach my $val (keys %info) {
    foreach my $check ( keys %{$info{$val}} ) {
        print ${$info{$val}}{$check}."\n";
    }
}

The last line is a little ugly, but as you read through the Data Structures Cookbook, you'll learn to use the -> operator and write the statement more elegantly.

Moh
  • 304
  • 2
  • 8
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404