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

Hi, This is what i have at this point. A hash key contains the 3rd element of an array and the value is the other 2 elements of an array
This is the data i am working on

12,1427766557, bob 5,1427766556, bill 10,1427766555, bob
my %data; open(DATA, '<<', 'test.csv') or die "Can't open file: $!"; while (my $row=<DATA>) { @$_ = split /(?:,|\s)+/, $row; $#$_!=2 ? next : push @{$data{$_-&gt;[2]}}, @$_[1,0] }
i just need to read this out so that it will look like this. Help please? im hoping to export this as a csv.
         name        name       name
 time    value       value      value
 time    value       value      value
 etc...

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

    Hello bishop2001,

    First, please enclose code and data in <code> ... </code> tags; see Markup in the Monastery.

    Second, to get the output you want, you need to work backwards and design an appropriate data structure. In this case, I think you would be better off with a hash keyed to times, with each value an inner hash of name/value pairs:

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

    (I am assuming that, in a data entry of the form 12,1427766557, bob, the first field is the “value” and the second is the time.)

    Once you have this data structure, you can write the code:

    Third (and notwithstanding the above code), when dealing with CSV files, you are generally well-advised to use a dedicated module, such as Text::CSV_XS.

    Hope that helps,

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

Re: perl print to csv
by Marshall (Canon) on Apr 21, 2015 at 04:07 UTC
    You already have a .csv set of data.
    I am not sure what you are trying to do?

    #!usr/bin/perl -w use strict; while (<DATA>) { s/\s//g; #compress spaces to nothing my ($small, $time, $name) = split /\,/; print "$time,$small,$name\n"; } =prints 1427766557,12,bob 1427766556,5,bill 1427766555,10,bob =cut __DATA__ 12,1427766557, bob 5,1427766556, bill 10,1427766555, bob
    UPDATE: If you want to sort the .CSV file, there are ways to do this.
    Please explain your objective and show input and output.
Re: perl print to csv
by CountZero (Bishop) on Apr 21, 2015 at 06:25 UTC
    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
      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,