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'
                     }
        };
test2 => HASH(0xbf35c)

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) {
                open( INFILE, "<", "$input_file") or

                die "$input_file does not exists: $!";
                }
                my %policy_Hash;
                while (<INFILE>) {
                        my @n = split /,/, $_;
                my $number_of_elements = scalar @n;
                print scalar @n, "\n";
                my ($policy_name,$i,$element,$parameters);
                ($policy_name,$parameters) = split( /,/, $_, 2 );
                print "policy_name = $policy_name has
$number_of_elements elements\n";

                for ( $i=0; $i <= $number_of_elements-1; $i++ ) {
                        $element = (split /,/, $_)[$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);


-----Original Message-----
From: Uri Guttman [mailto:u...@stemsystems.com] 
Sent: Friday, September 25, 2009 1:58 PM
To: Johnson, Reginald (GTS)
Cc: beginners@perl.org
Subject: Re: hash question

>>>>> "JR(" == Johnson, Reginald (GTS) <reggie_john...@ml.com> writes:

  JR(> Sample input file "test2,MS-Windows-NT,Silver,NPRO30DINCR,Client
  JR(> JXWGTI7R5CHD1 WINDOWS NT,Schedule test2_full_1700 FULL 604800"

  JR(>   print "Enter the filename of input file with full path\n";
  JR(>         my $input_file = <>;

  JR(>         chomp($input_file);


  JR(> # check to see if input file exist
  JR(>         if ( -e $input_file) {
  JR(>                 open( INFILE, "<", "$input_file") or

  JR(>                 die "$input_file does not exists: $!";
  JR(>                 }

the -e check and the open/die are redundant. drop the -e as you don't
need it.

  JR(>                 my %policy_Hash=();

no need to initialize a hash to () as my does that.
  JR(>                 my ($parameters);

declare vars as they are used. 
  JR(>                 while (<INFILE>) {
  JR(>                         my @n = split /,/, $_;
  JR(>                 my $number_of_elements = scalar @n;
  JR(> print scalar @n, "\n";

why do you not use $number_of_elements since you just go it?
  JR(>                 my ($policy_name,$i,$element);
  JR(>                 ($policy_name,$parameters) = split( /,/, $_, 2 );



  JR(> print "policy_name = $policy_name has $number_of_elements
elements\n";

  JR(>                 for ( $i=0; $i <= $number_of_elements-1; $i++ ) {

don't loop over indices, loop over the data.
  JR(>                         $element = (split /,/, $_)[$i];

you split the whole line on , so that split won't do anything

  JR(>                         print "this is element i=$i  $element\n";
  JR(>                         $policy_Hash{$policy_name}{
'element_$i'}=
  JR(> (split /,/, $_)[$i];

here is your main bug. '' won't interpolate $i. use "" for that. this is
assigning each value to the same hash element and overwriting the
previous one. learn to use Data::Dumper to see what is really in your
hash.

  JR(> This message w/attachments (message) may be privileged,
confidential or proprietary, and if you are not an intended recipient,
please notify the sender, do not use or share it and delete it. Unless
specifically indicated, this message is not an offer to sell or a
solicitation of any investment products or other financial product or
service, an official confirmation of any transaction, or an official
statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may
monitor, review and retain e-communications (EC) traveling through its
networks/systems. The laws of the country of each sender/recipient may
impact the handling of EC, and EC may be archived, supervised and
produced in countries other than the country in which you are located.
This message cannot be guaranteed to be secure or error-free. References
to "Merrill Lynch" are references to any company in the Merrill Lynch &
Co., Inc. group of companies, which are wholly-owned by Bank of America
Corporation. Securities and Insurance Products: * Are Not FDIC Insured *
Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are
Not a Condition to Any Banking Service or Activity * Are Not Insured by
Any Federal Government Agency. Attachments that are part of this
E-communication may have additional important disclosures and
disclaimers, which you should read. This message is subject to terms
available at the following link:
http://www.ml.com/e-communications_terms/. By messaging with Merrill
Lynch you consent to the foregoing.

that disclaimer is longer than your code!

uri

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to