On 9/25/09 Fri Sep 25, 2009 12:48 PM, "Johnson, Reginald (GTS)" <reggie_john...@ml.com> scribbled:
> Uri, > Thank you for the assistance. I have incorporated data dumper and the " > " into the code. What I find odd is that data dumper shows the values of > the hash as I expect them to be. Yet still when I print I get a hex > number. > > My output > > Enter the filename of input file with full path > /tmp/reggiej > 6 > policy_name = test2 has 6 elements > this is element i=0 test2 > this is element i=1 MS-Windows-NT > this is element i=2 Silver > this is element i=3 NPRO30DINCR > this is element i=4 Client JXWGTI7R5CHD1 WINDOWS NT > this is element i=5 Schedule test2_full_1700 FULL 604800 > > $VAR1 = { > 'test2' => { > 'element_5' => 'Schedule test2_full_1700 FULL > 604800 > ', > 'element_0' => 'test2', > 'element_3' => 'NPRO30DINCR', > 'element_4' => 'Client JXWGTI7R5CHD1 WINDOWS NT', > 'element_2' => 'Silver', > 'element_1' => 'MS-Windows-NT' > } > }; The output from Data::Dumper shows that you have a "hash-of-hashes". %policy_Hash is a hash variable. Element $policy_Hash{'test2'} is a reference to an anonymous hash. > test2 => HASH(0xbf35c) This is how a hash reference is printed. > > MY code > > # cat sample.pl > #!/usr/bin/perl > use strict; > use warnings; > use Data::Dumper qw(Dumper); > > print "Enter the filename of input file with full path\n"; > my $input_file = <>; > > chomp($input_file); > > > # check to see if input file exist > if ( $input_file) { Delete the above line. You have removed the '-e' in front of the file, so you are not testing for the existence of the file, you are just testing if the file name is not empty. > open( INFILE, "<", "$input_file") or > > die "$input_file does not exists: $!"; > } Delete the above line as well. > my %policy_Hash; > while (<INFILE>) { > my @n = split /,/, $_; > my $number_of_elements = scalar @n; > print scalar @n, "\n"; > my ($policy_name,$i,$element,$parameters); There is no need to have separate declaration statements. Just put 'my' in front the next line. > ($policy_name,$parameters) = split( /,/, $_, 2 ); You have already split the input line and stored the results in @n. You should be using that array instead of resplitting $_ again. It is more efficient and the code is shorter: my($policy_name,$parameters) = @n[0,1]; > print "policy_name = $policy_name has > $number_of_elements elements\n"; > > for ( $i=0; $i <= $number_of_elements-1; $i++ ) { > $element = (split /,/, $_)[$i]; my $element = $n[$i]; > print "this is element i=$i $element\n"; > $policy_Hash{$policy_name}{ "element_$i"}= > (split /,/, $_)[$i]; > } > print Dumper( \%policy_Hash); > while ( my ($key,$value)= each %policy_Hash ) { > print "$key => $value \n"; > } > } #end while > close(INFILE); This is how to print a two-level hash-of-hashes: while ( my ($key,$value)= each %policy_Hash ) { while ( my($key2,$value2) = each %{$value} ) { print "$key => $key2 => $value2 \n"; } } Here is a way that sorts the keys: for my $key1 ( sort keys %policy_Hash ) { for my $key2 ( sort keys %{policy_Hash{$key1}} ) { print "$key1 => $key2 => $policy_Hash{$key1}->{$key2}\n"; } } This will only work if all values of the first-level hash are references to a hash. Data::Dumper can handle the case where this is not true. -- Jim Gibson -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/