Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: structuring data: aka walk first, grok later

by pc88mxer (Vicar)
on May 30, 2008 at 16:25 UTC ( [id://689269]=note: print w/replies, xml ) Need Help??


in reply to structuring data: aka walk first, grok later

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.

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
    I'd probably just make the coordinates into an array (and maybe define constants):
    use constant { X => 0, Y => 1, }; my %DATA; while (<>) { my ($obs, $det, $x, $y) = split ' '; push(@{$DATA{$obs}{$det}}, [ $x, $y ]); } print "$DATA{21}{'DET-2'}[0][X]\n";
Re^2: structuring data: aka walk first, grok later
by chexmix (Hermit) on May 30, 2008 at 18:59 UTC
    Thanks. I recall hitting 'autovivification' in the Alpaca book and coming away babbling of sentient Froot Loops smoking Meerschaum pipes, since I could make neither head nor tail of it ... I think I posted something about this some time ago, here on PM.

    I look at it now and I can kinda wrap my mushy brain around it. I don't think this has to do with the Alpaca book being opaque (I need to go back to it, probably), but is perhaps a measure that even _I_ have learned something ... :)

    In this case $DATA is a reference to an HoHoA, right?

      No, in pc88mxer's node it's a reference to a HoHoH. In mine, it's (not a reference to) a HoHoA.
Re^2: structuring data: aka walk first, grok later
by chexmix (Hermit) on Jun 06, 2008 at 20:24 UTC
    OK. Using either approach, how do I do something with (e.g. treat as a unit or set) all the $x for a given $det in a given $obs?

    That is, how do I get that particular array back out of the data structure that has been built?

    Because that's how I need to process this data. It's the distribution of points on the axes of individual detectors that is of interest.

    My latest stab is this:

    ($obs, $det, $x, $y) = split /\ /; push @{ $histx{$obs}{$det} }, $x; push @{ $histy{$obs}{$det} }, $y; } for my $i (0..$#{ $histx{$obs}{$det} }) { print "${ $histx{$obs}{$det} }[$i]\n"; }

    I'm doing this bit of printing simply to see if I am catching all the data ... but this only prints out the stuff for the LAST $obs.

    NOTE: I should never have referenced / included the 'full precision' values in my discussion. The reasons for that are technical (beyond the need for this discussion), but it's entirely valid to just convert those to integers. I am sorry for the confusion that caused.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://689269]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-03-28 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found