What is the best data structure in Perl to store tabular data? -
i have table following data
1.1.1.1 routera texas 2.2.2.2 routerb texas 3.3.3.3 routerc california what best data structure in perl store data? thinking of storing in hash of hash ip address key
1.1.1.1 routera => texas, 2.2.2.2 routerb => texas, 3.3.3.3 routerc => california but if want ip addresses in texas, data structure may not flexible enough. there better way store if care ip addresses in texas?
pure perl task.
think of table array of records. in perl speak, array of hash references. (an aoa may applicable @ times, remember timtowtdi)
the keys of each hash reference correspond column/field name , values be, well, values particular record.
converting op's example data structure:
my @data = ( { ip => '1.1.1.1', router => 'routera', state => 'texas', }, { ip => '2.2.2.2', router => 'routerb', state => 'texas', }, { ip => '3.3.3.3', router => 'routera', state => 'california', } ); now fun part:
# give me ips in texas @ips_in_texas = map $_->{ip}, grep { $_->{state} =~ /texas/i } @data; # how many states data cover? use list::moreutils 'uniq'; $states_covered = uniq( map $_->{state}, @data ); # how many unique ips in each state? %ips_by_state; $ips_by_state{ $_->{state} }{ $_->{ip} }++ @data; print "'$_': ", scalar keys %{ $ips_by_state{$_} }, "\n" keys %ips_by_state; the knee-jerk reaction when suggest data structure centers around hunger memory. frankly speaking, won't issue unless you're dealing millions of records. , if case, dbms pencil-sharpening solution seek, not perl.
Comments
Post a Comment