On 9/25/09 Fri  Sep 25, 2009  10:05 AM, "Johnson, Reginald (GTS)"
<reggie_john...@ml.com> scribbled:

> I don't see what I am doing wrong in printing out the values in the
> hash. I am inputting a file with records of varying number of fields and
> want to put them in a hash. My output says Use of uninitialized value in
> concatenation (.) or string at sample.pl line 35, <INFILE> line 1.
>  => test2
> 
> Sample input file "test2,MS-Windows-NT,Silver,NPRO30DINCR,Client
> JXWGTI7R5CHD1 WINDOWS NT,Schedule test2_full_1700 FULL 604800"
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
>   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 ( -e $input_file) {
>                 open( INFILE, "<", "$input_file") or
> 
>                 die "$input_file does not exists: $!";
>                 }
>                 my %policy_Hash=();
>                 my ($parameters);
>                 while (<INFILE>) {
>                         my @n = split /,/, $_;
>                 my $number_of_elements = scalar @n;
> print scalar @n, "\n";
>                 my ($policy_name,$i,$element);
>                 ($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];
> #                       $policy_Hash{$policy_name}{ 'element_$i'}=
> $element;
>                                 }
>                 my ($key,$value);
>                 while ( $key,$value= each(%policy_Hash) ) {
>                         print "$key => $value \n";
>                 }
>                 } #end while
> close(INFILE);

There are many improvements you can make to this program, including better
indentation and eliminating all but one call to split. However, since your
immediate problem is getting the program to work, I suggest that you may
have a precedence problem in your call to the each routine. Try this line
instead:

    while( my($key,$value) = each(%policy_Hash) ) {

Note the parentheses around $key,$value.

See 'perldoc -f each' for an example.


-- 
Jim Gibson



-- 
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