in reply to structuring data: aka walk first, grok later
Perl makes this easy with its autovivification feature:
This uses two HoHoA - one for the X coordinate data and another for the Y coordinate data. Or you could store the coordinates in a data structure:my $DATA_X; my $DATA_Y; while (<>) { my ($obs, $det, $x, $y) = split(' ', $_); push(@{$DATA_X->{$obs}->{$det}}, $x); push(@{$DATA_Y->{$obs}->{$det}}, $y); }
And here's how to access the data (using the second approach):my $DATA; while (<>) { my ($obs, $det, $x, $y) = split(' ', $_); push(@{$DATA->{$obs}->{$det}}, {x => $x, y => $y}); }
Whether or not this is the best data structure for your data depends on what you want to do with it.print $DATA->{21}->{'DET-2'}->[0]->{x}; # prints 896.657564735788 print $DATA->{47}->{'DET-7'}->[1]->{y}; # prints 519.649226713148
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: structuring data: aka walk first, grok later
by runrig (Abbot) on May 30, 2008 at 16:48 UTC | |
Re^2: structuring data: aka walk first, grok later
by chexmix (Hermit) on May 30, 2008 at 18:59 UTC | |
by runrig (Abbot) on May 30, 2008 at 20:05 UTC | |
Re^2: structuring data: aka walk first, grok later
by chexmix (Hermit) on Jun 06, 2008 at 20:24 UTC |
In Section
Seekers of Perl Wisdom