Perl makes this easy with its autovivification feature:
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);
}
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;
while (<>) {
my ($obs, $det, $x, $y) = split(' ', $_);
push(@{$DATA->{$obs}->{$det}}, {x => $x, y => $y});
}
And here's how to access the data (using the second approach):
print $DATA->{21}->{'DET-2'}->[0]->{x}; # prints 896.657564735788
print $DATA->{47}->{'DET-7'}->[1]->{y}; # prints 519.649226713148
Whether or not this is the
best data structure for your data depends on what you want to do with it.