in reply to How can I group lines in a file?

There's nothing wrong with the other solutions offered, but it can be done more concisely with no loss of clarity:

my %HoA; ## Typo corrected open my $fh, '<', 'junk.dat' or die $!; /(\S+)\s+(\S+)/ and push @{ $HoA{ $1 } }, $2 while <$fh>; close $fh; pp \%HoA; { ernest => [38, 27], jim => [14, 34], john => [23, 44], matilda => [43, 22] }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: How can I group lines in a file?
by bichonfrise74 (Vicar) on May 17, 2009 at 02:39 UTC
    Hi BrowserUk,

    I like your solution but I'm not sure I understand it properly. Why does this line work?
    /(\S+)\s+(\S+)/ and push @{ $HoA{ $1 } }, $2 while <$fh>;
    I broke the line into 3 parts (split, push and the reading the file) and I understand what each part does but what I don't understand is why it worked.

    For example, should I read the line from right to left? So, read the file then split "and" push??? Why does split and push combo work together. This is what I don't understand.

    If you can explain it or point me to the proper documentation, I would really appreciate it. Thanks
      split is a function, so there is no split. That code is equivalent to
      while( <$fh> ){ if(/(\S+)\s+(\S+)/){ push @{ $HoA{ $1 } }, $2; } }
      to test, add before the if
      warn 'scalar ', /(\S+)\s+(\S+)/; warn 'list ', join '|', /(\S+)\s+(\S+)/;
        my mistake, it should be warn 'scalar ', scalar /(\S+)\s+(\S+)/;
        oops, my bad. When I was writing my own version, I used "split" to get the values which is basically the regex thing that BrowserUk did. Also, my version is similar to what you just wrote... Read the file, get the values, push it in a hash of array.

        But BrowserUk's version was done in a single line which I'm still perplexed. Why does it work? (regex thing "and" pushing it in a hash of array).

        How does "and" work? How can it get the regex values and push it to HoA? Thanks.
Re^2: How can I group lines in a file?
by akho (Hermit) on May 17, 2009 at 13:37 UTC
    Are you sure it works? It does if HoA is declared as a hash, not as a reference.

    Loss of clarity is kinda evident.

    It sure is understandable to a native speaker of Perl; but the fact that this is considered "clear" gives the language a bad name.

      It sure is understandable to a native speaker of Perl;

      It's Perl. You do have to understand Perl to understand it.

      but the fact that this is considered "clear" gives the language a bad name.
      • Does this combine a f b = (b >>=) . (return .) . f =<< a give Haskell a bad name?
      • How about this for Smalltalk?
        | s f | s := Prompter prompt: 'enter line'. f := Bag new. s do: [ :c | c isLetter ifTrue: [f add: c asLowercase] ]. ^f
      • Does this CH3(CH2)50CH3 give chemistry a bad name?
      • Or this y + ?y = ƒ(x+ ?x) = m (x + ?x) + c = m x + c + m ?x = y + m?x for mathematics?
      • Or this 1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Bxc6 dxc6 5. d3 Bb4+ 6. Nc3 Nf6 7. O-O Bxc3 chess?
      • Or this for physics?
      • Or this for choreography?
      • Or this musicians
      • ...

      Why do people expect Perl to look like Java or C and brand it with a '"bad name" when it doesn't?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        > It's Perl. You do have to understand Perl to understand it.

        You have to do more than that.

        As for the bullet points: sometimes people need complex stuff. However, they do not need obfuscated stuff. Being understandable and being clear are not the same thing. bichonfrise74's confusion demonstrates that; notice how zwon's solution did not raise questions.

        I myself often tend to devolve into one-liners, but that's not something to be proud of.

        Re Haskell: yes.

        > Why do people expect Perl to look like Java or C and brand it with a '"bad name" when it doesn't?

        Because they are human and want to understand stuff?..