in reply to Another flatfile thingy

Well here is another rather sick way to do it if you want to keep the funky data structure you got. Note: I am not saying this is better or faster, just ugly and sort of fun
sub OpenFF { my ($file,$dlmt) = @_; $dlmt = $_[1] || "\t"; my(%hash,%row); open(READ,$file) or return; my @headers = split /$dlmt/, <READ>; s/^\s+|\s+$//g for @headers; chomp(@row{@headers} = split /$dlmt/, <READ>) and %hash = map { $_=>{ $hash{$_} ? %{$hash{$_}} : (), $row{$heade +rs[0]}=>$row{$_}} } @headers while !eof(READ); return %hash; }

Replies are listed 'Best First'.
(bbq) RE: Re: Another flatfile thingy
by BBQ (Deacon) on Aug 04, 2000 at 17:47 UTC
    Huh like $_, you REALLY like statement modifiers, don't you? :) Anyway, yeah that looks like lots of fun. I would never ship it out as production code, but still very fun! My favorite line from the above is:
    s/^\s+|\s+$//g for @headers;
    Waaaaaay cool! :)

    On a side note, I got a message from merlyn in the chatterbox pointing out that s/// for, has a great perfomance penalty despite its cool look. Then again, I believe that perlmonkey's idea was to make something funky as well and not be too worried about perfomance issues. Hey, we're all just tossing ideas here, right? Still, merlyn has a very good point that should be noted.

    # Trust no1!
      Hmm, did merlyn happen to mention a better construct, the s/// for ... it is something we put into production. I guess an alternative would be  map { s/^\s+|\s+$//g } @headers But I doubt it would be significantly better. I guess I will have to benchmark.

        Nope, he just made a note of it. (Better than running with scissors though) :)

        I'd like to see the results of whatever you come up with in place of s/// for. (either that or I have to RTFM and learn once and for all how to use benchmark;)

        # Trust no1!