Some resources re sorting:
- http://perldoc.perl.org/perlfaq4.html#How-do-I-sort-an-array-by-%28anything%29%3f
- Replies in MsExcel like Multi Column Sorting
- Super Search
...and
- "Learning Perl" (AKA "The Llama book") Schwartz, foy & Phoenix, O'Reilly, esp Ch 15
The preferred data structure will depend on how you want to use the data later -- your notion of a hash with epoch_times as the keys will be straightforward (the time will be converted to a string, but that's not an issue); putting the data into a database (SQLite, for one) will give the power to manipulate and retrieve it in many ways.
And you certainly could put it in an array if the format you show is absolutely guaranteed to be invariant:
- split offers the option of splitting once, after the first space:
@array = split /PATTERN/,EXPR,LIMIT
where epoch_time will be the first, third, fifth (and so on) elements of the array to which you've split
- or making three fields:
split /PATTERN/,EXPR
TIMTOWTDI: could be much more elegant (MUCH more!) but this may be easy to follow:
#!/usr/bin/perl
use strict;
use warnings;
#825421
use Data::Dumper;
my (@array, @tmp, $time, $fields);
my @lines = <DATA>;
for my $line(@lines) {
chomp $line;
($time, $fields) = split / /, $line, 2;
push @array, $time;
push @array, $fields;
}
my @arr2;
for my $line(@lines) {
chomp $line;
my @tmp2 = split / /, $line;
for my $element2(@tmp2) {
push @arr2, ($element2 . "\t");
}
}
print "\n first array \n";
for $_(@array) { print "$_ \n"; }
print "\n";
print "\n second array\n";
for $_(@arr2) {print "$_ \n "; }
print "\n";
__DATA__
12345 f1 f2
23456 F3 F4
98765 f-five f-six<c>
<p>Output:</p>
<c> first array
12345
f1 f2
23456
F3 F4
98765
f-five f-six
second array
12345
f1
f2
23456
F3
F4
98765
f-five
f-six
Updated code and output after initially (and dumb-ly!) pasting both in a form that didn't even come close to OP's spec.
Code for sorting left as an exercise for elwoodblues. :-)
|