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

Re^2: perl print to csv

by Marshall (Canon)
on Apr 21, 2015 at 06:35 UTC ( [id://1124108]=note: print w/replies, xml ) Need Help??


in reply to Re: perl print to csv
in thread perl print to csv

Yes!

I have no idea of what this gibberish is supposed to do:

$#$_!=2 ? next : push @{$data{$_->2}}, @$_1,0

Replies are listed 'Best First'.
Re^3: perl print to csv
by Athanasius (Archbishop) on Apr 21, 2015 at 07:11 UTC

    That’s because two pairs of square brackets were lost due to the absence of <code> tags. I believe the two statements within the loop were originally as follows:

    @$_ = split /(?:,|\s)+/, $row; $#$_ != 2 ? next : push @{$data{$_->[2]}}, @$_[1, 0];

    The first line treats $_ as an array reference and autovivifies the array @$_, populating it with the comma-and-whitespace-separated fields in $row. In the second line, $#$_ is the index of the last element in @$_, so if it is not 2 (corresponding to 3 fields) then this data line is skipped. If exactly 3 fields were extracted from $row into @$_, the second and first fields, in that order, are pushed onto the anonymous array $data{$_->[2]}, where $_->[2] is the name field. So after running the loop with the data given, the hash %data (formatted by Data::Dump) looks like this:

    { bill => [1427766556, 5], bob => [1427766557, 12, 1427766555, 10] }

    To the OP: What makes this code look strange — apart from the missing square brackets — is:

    • the unusual use of $_: normally we would expect to see it used as the loop variable; and
    • the use of the conditional (ternary) operator ?: for flow control: it is considered better practice to restrict the use of this operator to data selection.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Greetings all, thanks for the reponses. What im trying to accomplish is this. i would like the file im working on which contains lines such as this
      52,1427766557, bob
      12,1427766556, bob
      5,1427766555, bill
      10,1427766554, joe
      23,1427766553, bob

      and turn into something like this where i can open it in excel and it would have a grid type of format where bob is the heading of the column, 12 is the value and 1427766557 is the time. 
                              bob         joe      bill

      1427766557   52

      1427766556   12

      1427766555                               5

      1427766554                   10

      1427766553   23

      @$_ = split /(?:,|\s)+/, $row;
      $#$_!=2 ? next : push @{$data{$_->2}}, @$_1,0

      with these 2 lines im splitting the row then assigning each part to an array and taking the 3rd part which is the name and setting it as the hash key and setting the other two elelents, time and value as the keys of the hash.

      thanks,

        How about

        use strict; use warnings; my %cols; my $ctr=0; my @data = map {/(.*),(.*), (.*)/; $2."\t"x($cols{"\t$3"}//=++$ctr)."$ +1\n" } <DATA>; print sort {$cols{$a}<=>$cols{$b}} keys %cols; print "\n", @data; __DATA__ 52,1427766557, bob 12,1427766556, bob 5,1427766555, bill 10,1427766554, joe 23,1427766553, bob

        Comment: headers look slightly misaligned due to usage of tab stopps. This could be resolved by monitoring the length of $2.

Log In?
Username:
Password:

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

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

    No recent polls found