in reply to perl print to csv

It would certainly help to better understand your question if you showed the output you expect on basis of the data in your posting.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: perl print to csv
by Marshall (Canon) on Apr 21, 2015 at 06:35 UTC
    Yes!

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

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

      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,