Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Perl 6 implementation of the following Perl 5 code

by konnjuta (Acolyte)
on May 27, 2013 at 08:58 UTC ( #1035374=perlquestion: print w/replies, xml ) Need Help??
konnjuta has asked for the wisdom of the Perl Monks concerning the following question:


Given list of files delimited by whitespaces and following a format similar to the below example

ID SOURCE SCORE1 SCORE2 SCORE3 ..... SCOREN 66521 SRC1 12 34 27 67 88921 SRC2 34 56 59 88 66524 SRC1 56 66 3341 SRC1 44 59 67

Print out all SRC1 records in reverse column order delimited by a single space and sorted by the last score then sorted by ID.

Perl 5 (Possible Solution):

print join " ", reverse split foreach map { $_->[0] } sort { $a->[1]<=>$b->[1] || $a->[0] <=> $b->[0] } map { [ $_ , (split)[-1] ] } grep { /SRC1/ } <> ;

How can I do it in Perl 6?

UPDATE: Here's are possible one-liner solutions thanks to moritz

Functional Approach
say reverse .words for map *[2], sort map {[.words[*-1,0],$_]}, grep /SRC1/, lines;
Object-Oriented Approach
say .words.reverse for lines.grep(/ICSE/).map({[.words[*-1,0],$_]})*[2]);

The Perl 6 syntax is much cleaner.

Replies are listed 'Best First'.
Re: Perl 6 implementation of the following Perl 5 code
by moritz (Cardinal) on May 27, 2013 at 11:13 UTC

    A solution similar to yours in style:

    use v6; print join ' ', reverse .words for lines.grep(/SRC1/)\ .map({ [+<<.words[*-1, 0], $_]})\ .sort\ .map(*[2])

    And a version using feeds (note that feeds support in Rakudo is still somewhat lacking and experimental, but this one works:)

    use v6; lines() ==> grep(/SRC1/) ==> map({ [+<<.words[*-1, 0], $_]}) ==> sort() ==> map(*[2]) ==> my @lines; print ~.words.reverse for @lines;

    And here is another solution:

    class Line { has $.str handles 'words'; method Str { self.words.reverse.join(' ') } } multi infix:<cmp>(Line $a, Line $b) { $a.words[*-1] <=> $b.words[*-1] || $a.words[0] <=> $b.words[0]; } print lines().map({ => $_)}).grep(/SRC1/).sort;

    (Which admittedly doesn't do the Schwartz Transform).

    (Update: added second version, tested and fixed third version)

      Waitasec?!? What's the backslash at the end of the lines in the first solution? Looks suspiciously like a line continuation character in the worst tradition of Visual Basic and Ruby?!?

      Enoch was right!
      Enjoy the last years of Rome.

        It is "unspace". Whitespace is not allowed before subscripts or ".". Apparently this is required to allow "if" conditions without parens and is part of the "space is sometimes significant" path that was taken. (See: Synopsis 3 and the linked Synopsis 2)

        Update: Below, moritz clarifies that "if" without parens was not the reason for forbidding whitespace around "." methods calls, merely another of the consequences.

        Good Day,

      The 1st/2nd Perl 6 solution is interesting because you read it from left to right treating map,grep and sort as object methods.

      The Perl 5 version is read from right to left treating map,grep and sort as functions. I've attempted to do it the functional way in Perl 6 and got the following to work on Rakudo.

      use v6; say reverse .words for map {.[1]} ,sort( map { [.words[*-1],$_] } ,grep {/SRC1/} ,lines() );

      The syntax for doing Perl 6 functionally seems to be more untidy than the Perl 5 version and make it a bit harder to read. The required comma, the required parenthesis around sort. Its not too bad though. Will get some getting use to.

        If you make the commas stand out by putting them up front, it looks untidy, yes. As they say, the key is to play to Perl's strengths, not to its weaknesses.

        And if you include unnecessary punctuation, it also looks untidy. This one seems to work fine:

        use v6; say reverse .words for map *[1], sort map { [.words[*-1],$_] }, grep /SRC1/, lines;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035374]
Front-paged by Corion
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-10-21 07:27 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (269 votes). Check out past polls.