Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How best to return data from a package ???

by jdporter (Paladin)
on Apr 28, 2004 at 04:10 UTC ( [id://348716]=note: print w/replies, xml ) Need Help??


in reply to How best to return data from a package ???

I would return a list of "records", where each record is implemented as a hash. E.g.
my $infh = IO::File->new("< data") or die; local $_ = $infh->getline; s/LV /LV_/g; # :-) my @column_names = split; my @records; while ( $infh->getlines ) { my %rec; @rec{@column_names} = split; push @records, \%rec; } return @records; # or simply return map { my %r; @r{@c} = split; \%r } $infh->getlines;

...just for example. A better approach would parse fixed-width columns. A more robust extension of that would attempt to infer the column widths.

Update. If one of the columns is a "unique key", then it probably makes sense to load the set of records into a hash, using those values as the hash keys. E.g.:

return map { my %r; @r{@c} = split; ( $r{'LV_NAME'} => \%r ) } $infh->getlines;
(That doesn't really return a hash, it returns a list... but that list can sensibly be assigned to a hash and it will DWIM.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found