Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This program reads all of the data into a hash and reproduces it in the transformed state you require. This is fine unless the data is enormous and will not easily fit into the memory you have available, in which case you will need a different solution.</p> <p>The program expects the name of the input file as a command-line parameter, and defaults to <code>data.csv</code> if none was supplied. It sets <code>$/</code> to the null string to enable Perl's "paragraph mode" of input, where data is read up to the next blank line or end of file. That means all data for a given time interval are read at once, and must be split further into individual rows before processing.</p> <pre><code>use strict; use warnings; my ($subject, @ids); my @sort_order; my (%data, @keys); my ($file) = @ARGV; $file //= 'data.csv'; open my $fh, '&lt;', $file or die qq{Unable to open file "$file" for reading: $!}; local $/ = ''; while (&lt;$fh&gt;) { my @rows = split /\n/; unless ($subject) { ($subject, @ids) = split /,/, $rows[1]; @sort_order = sort { $ids[$a] cmp $ids[$b] } 0 .. $#ids; next; } my ($date, $time) = split /,/, shift @rows; for (@rows) { my ($id, @fields) = split /,/; push @keys, $id unless exists $data{$id}; push @{ $data{$id} }, [$date, $time, @fields[@sort_order]]; } } for my $key (@keys) { print join(',', $key, $subject, @ids[@sort_order]), "\n"; print join(',', @$_), "\n" for @{ $data{$key} }; print "\n"; } </code></pre> <p><strong>output</strong></p> <pre><code>Data1,Subject,ID#,ID1,ID2,[...] Date,Time1,aa#,aa1,aa2,[...] Date,Time2,ab#,ab1,ab2,[...] Data2,Subject,ID#,ID1,ID2,[...] Date,Time1,ba#,ba1,ba2,[...] Date,Time2,bb#,bb1,bb2,[...] Data3,Subject,ID#,ID1,ID2,[...] Date,Time1,ca#,ca1,ca2,[...] Date,Time2,cb#,cb1,cb2,[...] Data4,Subject,ID#,ID1,ID2,[...] Date,Time1,da#,da1,da2,[...] Date,Time2,db#,db1,db2,[...] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload