Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^6: Undef values from file open

by Anonymous Monk
on Mar 22, 2016 at 21:31 UTC ( [id://1158556]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Undef values from file open
in thread Undef values from file open

Here, and I can not get it to work:
#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; my $file_name = "file.csv"; open my $lines, '<', $file_name or die "Can't open file $file_name, $!"; my @parms = qw( date day name ); while (my $line = <$lines>) { next unless $line =~ /\S/; # <- add chomp $line; my $data; ( $data->{ date }, $data->{ day }, $data->{ name } ) = split(/,/, $ +line); pp @{ $data }{ (@parms) }; }

Replies are listed 'Best First'.
Re^7: Undef values from file open
by poj (Abbot) on Mar 22, 2016 at 21:50 UTC

    What do you get with this

    #!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; my $file_name = "file.csv"; open my $lines, '<', $file_name or die "Can't open file $file_name, $!"; while (my $line = <$lines>) { pp $line; }
      It prints all the data from the file no problem, this line also prints:
      print "$data->{ date }, $data->{ day }, $data->{ name }\n"
      This one gets me nothing:
      pp @{ $data }{ (@parms) };

        Here's another step along the debugging path poj began here. We know that each line of the file is being read correctly; now, what happens when we process a line and assign its sub-strings to the  $data hashref?

        c:\@Work\Perl\monks\Anonymous Monk\1158543>perl -e "use strict; use warnings; use Data::Dump 'pp'; ;; my $file_name = 'file.csv'; open my $lines, '<', $file_name or die qq{opening file '$file_name': $!}; ;; while (my $line = <$lines>) { pp $line; next unless $line =~ /\S/; chomp $line; my $data; ($data->{date}, $data->{day}, $data->{name}) = split(/,/, $line); pp $data; print qq{------- \n} } " "01/04/2014,Friday,Joe\n" { date => "01/04/2014", day => "Friday", name => "Joe" } ------- "02/11/2011,Monday,Mary\n" { date => "02/11/2011", day => "Monday", name => "Mary" } ------- "05/09/2016,Monday,Ann\n" { date => "05/09/2016", day => "Monday", name => "Ann" } ------- "07/02/2013,MOnday,Marc\n" { date => "07/02/2013", day => "MOnday", name => "Marc" } ------- "\n"
        What do you get when you run this code? Note that my data file contains a blank line at the end for which no further processing is done, as expected, so this aspect of the program works.

        BTW: I'd still be curious to know the exact version of Perl you're using. I don't really know what being "current" means.


        Give a man a fish:  <%-{-{-{-<

        What does pp $data give, and what version of perl do you have ?

        What does pp @parms; give you ?

      My Perl version is current, I get undef for all values.
Re^7: Undef values from file open
by AnomalousMonk (Archbishop) on Mar 22, 2016 at 21:42 UTC

    Here's what I get from the code you just posted and the data from above:

    c:\@Work\Perl\monks\Anonymous Monk\1158543>perl -e "use strict; use warnings; ;; use Data::Dump 'pp'; use Data::Dumper; ;; my $file_name = \"file.csv\"; ;; open my $lines, '<', $file_name or die \"Can't open file $file_name, $!\"; ;; my @parms = qw( date day name ); ;; while (my $line = <$lines>) { next unless $line =~ /\S/; chomp $line; my $data; ( $data->{ date }, $data->{ day }, $data->{ name } ) = split(/,/, $ +line); pp @{ $data }{ (@parms) }; } " ("01/04/2014", "Friday", "Joe") ("02/11/2011", "Monday", "Mary") ("05/09/2016", "Monday", "Ann") ("07/02/2013", "MOnday", "Marc")


    Give a man a fish:  <%-{-{-{-<

      It's a mystery to me, why this line:
      print "$data->{ date }, $data->{ day }, $data->{ name }\n"
      prints all the values and this one gets me undefs:
      pp @{ $data }{ (@parms) };
      I'm stuck on this one!
      And this line prints anything for you?
      pp @{ $data }{ (@parms) };

        It prints what I posted:

        ("01/04/2014", "Friday", "Joe") ... ("07/02/2013", "MOnday", "Marc")
        What I posted was a line-by-line screenshot (a lineshot?) (update: aka a cut-and-paste) of my Windows command line interpreter window for your program and its output.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found