Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

golf: shortest way to parse a pipe delimited file

by bageler (Hermit)
on Nov 09, 2005 at 19:57 UTC ( #507198=obfuscated: print w/replies, xml ) Need Help??

looking at this node, I took a quick stab at shortening the process of parsing a pipe delimited file into a HoA. Basically, given input
foo|bar|baz aaa|bbb|ccc
you should get an HoA like
$VAR1 = 'aaa'; $VAR2 = [ 'bbb', 'ccc' ]; $VAR3 = 'foo'; $VAR4 = [ 'bar', 'baz' ];


I came up with 43 chars:
#12345678901234567890123456789012345678901234567890 %prod=map{chomp;split/\|/;shift@_,[@_]}<SRC>

Replies are listed 'Best First'.
Re: golf: shortest way to parse a pipe delimited file
by BrowserUk (Patriarch) on Nov 09, 2005 at 21:36 UTC

    This will do it, but I don't know how you would score it for comparison.

    perl -anF"\|" -le"$f{shift@F}=[@F[1..$#F]]" pipe.txt

    And you need this to prove it worked

    P:\test>perl -MData::Dumper -anF"\|" -le"$f{shift@F}=[@F[1..$#F]]}{pri +nt Dumper \%f" pipe.txt $VAR1 = { '6' => [ 'foo', 'bar', 'bax' ], '3' => [ 'foo', 'bar', 'bax' ], '7' => [ 'foo', 'bar', 'bax' ], '9' => [ 'foo', 'bar', 'bax' ], '2' => [ 'foo', 'bar', 'bax' ], '8' => [ 'foo', 'bar', 'bax' ], '1' => [ 'foo', 'bar', 'bax' ], '4' => [ 'foo', 'bar', 'bax' ], '10' => [ 'foo', 'bar', 'bax' ], '5' => [ 'foo', 'bar', 'bax' ] };

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      very nice, but unfortunately my command line doesn't like it at all:
      Bareword found where operator expected at -e line 1, near "0F" (Missing operator before F?) syntax error at -e line 1, near "}=" Execution of -e aborted due to compilation errors.
      This is how you'd score it:
      #1234567890123456789012345678901234567890123456789012345 perl -anF"\|" -le"$f{shift@F}=[@F[1..$#F]]" pipe.txt
      42.
        my command line doesn't like it at all

        Then adapt it to your shell. Something like this might work,

        perl -anF'|' -le'$f{shift@F}=[@F[1..$#F]]' pipe.txt

        but I don't use your shell so that is a guess, and I cannot test it as it wouldn't work on my shell.

        This is how you'd score it:

        That doesn't seem quite fair, as to use your example, you would need at least this to make it work:

        #!/usr/bin/perl #12345678901234567890123456789012345678901234567890123456789012345 open SRC, '<pipe.txt';%prod=map{chomp;split/\|/;shift@_,[@_]}<SRC>

        Which I get to be a total of 89 and that's without the command line to run it!


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      This will do it, but I don't know how you would score it for comparison.

      Well, no matter how you score it, I gotcha by two. :-)

      perl -F'\|' -lane'($k,@v)=@F;$f{$k}=[@v]'

      -sauoq
      "My two cents aren't worth a dime.";
      

        Update: Oh crap!

        In that case, gotcha by 1 :)

        perl -F'\|' -lane'($k,@v)=@F;$f{$k}=\@v'

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: golf: shortest way to parse a pipe delimited file
by dragonchild (Archbishop) on Nov 09, 2005 at 20:30 UTC
    perl -MData::Dumper -lanF'\|' -e '$p{$F[0]}=[@F[1..$#F]]}END{print Dum +per \%p' abcd
    Where abcd has your data.

    Assuming standard golfing rules where you give the body of a function that takes whatever parameters are useful:

    #!/usr/bin/perl my %p; sub func { my($x,@x)=$_[0]=~/([^\|\n]+)/g;$p{$x}=\@x # Or ... chomp(my($x,@x)=split'\|',pop);$p{$x}=\@x } open SRC, 'abcd'; while (<SRC>) { func($_); } use Data::Dumper; print Dumper \%p;
    Either way, I'm at 41 characters

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Your first solution will misbehave if you run across a line like:
      foo||bar
      Your second solution can be shortened by 3 characters:
      sub func { #23456789_123456789_123456789_12345678 chomp(@_=split'\|',pop);$p{+shift}=\@_ }
      and one more if you're willing to replace chomp with chop.
        this fails by giving an empty hash entry.
        $VAR1 = { '' => [], 'aaa' => [ 'bbb', 'ccc' ], 'foo' => [ 'bar', 'baz' ] };
      I see more than 41 characters because you say while (<SRC>) { } In my solution, the filehandle is part of the solution character count. If I put my solution in the context of yours it looks like this:
      sub parse{ #12345678901234567890123456789012345678901234567890 %p=map{chomp;split/\|/;shift@_,[@_]}@_ } parse(<SRC>);
      and is 37 characters.
        Save a couple of chars by taking advantage of the fact that shift defaults to @_ while in a sub.
        %p=map{chomp;split/\|/;shift,[@_]}@_
        Using chop instead of chomp would help, but might not be portable.

        Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-12-06 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?