Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Another flatfile thingy

by btrott (Parson)
on Aug 04, 2000 at 10:15 UTC ( [id://26140]=note: print w/replies, xml ) Need Help??


in reply to Another flatfile thingy

A couple of things:
  • W/r/t your data structure: personally, I always prefer a hash of records, with each of those records being a hash reference of columns and values. Whereas yours is the other way round. Does that make sense? In other words, I'd prefer
    $foo{3}{NAME1}
    instead of
    $foo{NAME1}{3}
    But that's just me.
  • Don't read the entire file into an array; just loop over each line and process it as necessary.
  • Returning a reference would be less memory-intensive.
  • A hash slice would be nice for setting the values. But that would really only work with the data structure I prefer (that which I outlined above).
  • When you return on an error, you don't have to explicitly return 0.
That all said, here's my rewrite:
sub OpenFF { my($file, $dlmt) = @_; $dlmt = "\t" unless defined $dlmt; my $hash = {}; open READ, $file or return; chomp(my $head = <READ>); my @headers = split $dlmt, $head; while (<READ>) { chomp; my @columns = split $dlmt; @{$hash->{$columns[0]}}{@headers} = @columns; } close READ; return $hash; }

Replies are listed 'Best First'.
(bbq) RE: Re: Another flatfile thingy
by BBQ (Curate) on Aug 04, 2000 at 17:37 UTC
    • W/r/t your data structure: personally, I always prefer a hash of records, with each of those records being a hash reference of columns and values. Whereas yours is the other way round. Does that make sense? In other words, I'd prefer

      Yeah it makes sense, its just a matter of $hash{x}{y} vs. $hash{y}{x}. As you said, personal preference. I think of tables in a very visual way, so I just guess my feeling is for x first.

    • Returning a reference would be less memory-intensive.

      Forgive me for my dense question, but how do you return a reference of a hash if the hash is private to the subroutine? Can that be done?

    • When you return on an error, you don't have to explicitly return 0.

      Oh, that's just an office habit we have. We are always very explicit about everything, we even avoid using $_ when possible. Ex: saying foreach $v instead of just foreach. But thanks for the note anyway!

    @{$hash->{$columns[0]}}{@headers} = @columns;Now that has to be one of the coolest things I've seen here lately... Congrats, dude. You hit the nail on the head it seems. Thanks for your views.

    #!/home/bbq/bin/perl
    # Trust no1!
      You wrote:
      > Forgive me for my dense question, but how do you return > a reference of a hash if the hash is private to the > subroutine? Can that be done?
      Don't think of the hash as "private" to the subroutine; yes, it's declared lexically within the sub, but that's not all that matters. Think of the hash as a piece of data *somewhere* that you've referenced lexically within the sub.

      Perl's garbage collection is based on reference counting; so each reference to such a piece of data increments the reference count. Once you've declared

      my $hash = {}
      the reference count of the underlying structure is incremented to one.

      Normally, you'd just have that one reference to the data, so once the lexical has gone out of scope--at the end of the subroutine--the data would be destroyed. But not here, because you've returned a reference to the data and assigned it somewhere else. Even though leaving the sub decrements the reference count, then, you re-increment it by assigning the return value of the sub. So the reference count is still one, and the data is still there, and valid.

      The data won't be destroyed until all references to it have gone away.

      BTW: this is, as far as I know, correct, but someone who knows more about Perl's internals is free to correct anything wrong. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-04-19 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found