Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Undef values from file open

by Anonymous Monk
on Mar 22, 2016 at 20:51 UTC ( [id://1158543]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I cant get it why I am open a txt file and getting undef when I try to print this value: pp @{ $rec }{ (@parms) };
#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; my $file_name = "file.txt"; open ( LINES, $file_name) or eval { warn "Can't open file $file_name."; exit; }; my @parms = qw( date day name ); while (my $line = <LINES>) { chomp $line; my $data; ( $data->{ date }, $data->{ day }, $data->{ name } ) = split(/,/,$l +ine); print "\n $data->{ date }, $data->{ day }, $data->{ name }\n"; pp @{ $rec }{ (@parms) }; }
Any suggestion? Thank you!

Replies are listed 'Best First'.
Re: Undef values from file open
by Mr. Muskrat (Canon) on Mar 22, 2016 at 20:56 UTC

    Did you not see the compilation error?

    Global symbol "$rec" requires explicit package name at ./1158543.pl line 27. Execution of ./1158543.pl aborted due to compilation errors.

    You never defined $rec.

    Update: I'd probably rewrite that like this:

    #!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; my $file_name = "file.txt"; open my $lines, '<', $file_name or die "Can't open file $file_name, $!"; my @parms = qw( date day name ); while (my $line = <$lines>) { chomp $line; my $data; ( $data->{ date }, $data->{ day }, $data->{ name } ) = split(/,/, $ +line); print "\n $data->{ date }, $data->{ day }, $data->{ name }\n"; pp @{ $data }{ (@parms) }; }

      typo, Im sorry, its this:
      pp @{ $data }{ (@parms) };

        It works for me.

        1/1/16, Mon, foo ("1/1/16", "Mon", "foo") 1/2/16, Tue, bar ("1/2/16", "Tue", "bar")

        What does your data look like?

      Its driving me crazy, I even copied your code and its still:
      (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef) (undef, undef, undef)
      Why?? Can't understand it?!!
        You probably have a configuration problem. Download (not cut and paste) Mr.Muskrat's code. Download your own data file and rename it to "file.txt". Run the code that you just downloaded. I have duplicated his results this way.
        Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1158543]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found