Reference of hash (Perl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reference of hash (Perl)
# 1  
Old 01-22-2013
Reference of hash (Perl)

Hi Perl users,

Could somebody help me to find the solution of my problem below.
Here is my data:
Code:
__DATA__
===================================================
NameOfipaddress                       ippair_1
propertiesx                           y
propertiesy                           x
ipAddress_1                           192.168.200.1
ipAddress_2                           192.168.200.2 
===================================================
NameOfipaddress                       ippair_2
propertiesx                           y
propertiesy                           x
ipAddress_1                           192.168.200.3
ipAddress_2                           192.168.200.4

Here is the expected result of the code:
Code:
__EXPECTED_RESULT__
FIRST_IP_PAIR
NameOfipaddress : ippair_1       
ipAddress_1     : 192.168.200.1  
ipAddress_2     : 192.168.200.2 

SECOND_IP_PAIR
NameOfipaddress : ippair_2
ipAddress_1     : 192.168.200.3
ipAddress_2     : 192.168.200.4

Here is my code and the unexpected result:

Code:
LINE:while (my $line = <FH>){
         chomp ($line);
         if ($line =~ m/^(NameOfipaddress)\s*(.*)/){
           push @{$datalog{'IP'}{$1}}, $2;
           next;
         }
         if ($line =~ m/^(ipAddress_1)\s*(.*)/){
           push @{$datalog{'IP'}{$1}}, $2;
           next;
         }
         if ($line =~ m/^(ipAddress_2)\s*(.*)/){
           push @{$datalog{'IP'}{$1}}, $2;
           redo LINE if ($line =~ m/^(NameOfipaddress)\s*(.*)/);
         }
     }
     
foreach my $key (sort keys %{$datalog{'IP'}}){
   print "$key: @{$datalog{'IP'}{$key}}";
   print "\n";
}     

__UNEXPECTED_RESULT(my code result)__
NameOfipaddress : ippair_1       ippair_2
ipAddress_1     : 192.168.200.1  192.168.200.3
ipAddress_2     : 192.168.200.2  192.168.200.4

# 2  
Old 01-22-2013
You need to store the data structure a little differently. To know, how it is done in this, just uncomment the line "print Dumper \%datalog".

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

$\="\n";

my $curr_pair="";
my $ipadrnm="";
my %datalog;
LINE:while (my $line = <DATA>){
         chomp ($line);
         if ($line =~ m/^(NameOfipaddress)\s*(.*)/){
           $datalog{'IP'}{$2}={ };
           $curr_pair=$2;
           next;
         }
         if ($line =~ m/^(ipAddress_1)\s*(.*)/){
           $datalog{'IP'}{$curr_pair}{$1}=$2;
           next;
         }
         if ($line =~ m/^(ipAddress_2)\s*(.*)/){
           $datalog{'IP'}{$curr_pair}{$1}=$2;
           redo LINE if ($line =~ m/^(NameOfipaddress)\s*(.*)/);
         }
     }

#print Dumper \%datalog;
my $cnt=1;
foreach my $ipname (sort keys%{$datalog{IP}}){
        print 'IP_PAIR : ', $cnt++;
        print "NameofIpAddress : $ipname ";
        foreach my $ipadr (keys%{$datalog{IP}{$ipname}}){
                print "$ipadr : $datalog{IP}{$ipname}{$ipadr} ";
        }
        print "";
}

On running this:

Code:
IP_PAIR : 1
NameofIpAddress : ippair_1
ipAddress_1 : 192.168.200.1
ipAddress_2 : 192.168.200.2

IP_PAIR : 2
NameofIpAddress : ippair_2
ipAddress_1 : 192.168.200.3
ipAddress_2 : 192.168.200.4

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

5. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

6. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

7. Shell Programming and Scripting

Perl reference

Hi all, I have a reference named $test. it points to the data structure as follows 'test' => }, ... (1 Reply)
Discussion started by: Damon sine
1 Replies

8. Shell Programming and Scripting

hash of hashes : how to print reference and its internal structure?

#use perl 5.8.5; my %h1=(a=>'b', c=>'d'); my %h2=(a1=>'b1', c1=>'d1'); my $R1=\%h1; my $R2=\%h2; my %h= {$R1, $R2}; my $href=\%h; # hash of hashes foreach my $key (keys %$href){ print "Z::$$href{$key}\n" } When I am trying to print elements of hash of hashes, it prints HASH... (1 Reply)
Discussion started by: shristi
1 Replies

9. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

10. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question