Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
P is for Practical
 
PerlMonks  

Schwartzian Transfrom using a subroutine

by oliverhuber (Initiate)
on Jan 09, 2013 at 11:42 UTC ( #1012454=perlquestion: print w/ replies, xml ) Need Help??
oliverhuber has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

finding my ways through the vast fields of perl using "intermediate perl" by o'reilly as a guide ... currently, in the middle of not-loosing-my-head while trying to understand the schwartzian transform ... one thing acutally strikes me: the following code is working sweet as:

my @words = qw (Oli Sonni sonja oliver speedy 112); my @output = map $_->[0], sort {$a->[1] cmp $b->[1]} map { my $string = $_; $string =~ tr/A-Z/a-z/; $string =~ tr/a-z//cd; [$_, $string]} @words; for my $word (@output){say $word;}

The output, as expected is a "dictionary"-sorted list (acutllay, that's the solution proposed in the actual book itself): 123 ... Oli ... speedy However, I did come up with quite a similar solution. The only difference is that I stuffed the regexes into a subroutine:

my @words = qw (Oli Sonni sonja oliver speedy 112); my @output = map $_->[0], sort {$a->[1] cmp $b->[1]} map [$_, transform($_)], @words; sub transform{ my $string = shift; $string =~ tr/A-Z/a-z/;#kleinschreibung $string =~ tr/a-z//cd; } for my $word (@output){say $word;}

As a result, I am getting the original sorting of the list. Some enlighted monk being able to see what I do not see ...?

Thanks in advance, Oli

Comment on Schwartzian Transfrom using a subroutine
Select or Download Code
Re: Schwartzian Transfrom using a subroutine
by choroba (Abbot) on Jan 09, 2013 at 11:47 UTC
    Missing return or correct return value in the sub.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Schwartzian Transfrom using a subroutine
by BrowserUk (Pope) on Jan 09, 2013 at 11:53 UTC
    I am getting the original sorting of the list.

    Because your sub doesn't return the transformed string; it returns the result of the the last transform. Change that to:

    sub transform{ my $string = shift; $string =~ tr/A-Z/a-z/;#kleinschreibung $string =~ tr/a-z//cd; $string; }

    (As an aside; are you aware that for my $word (@output){say $word;} is more easily written as say for @output?)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
      Many thanks! It's working now. Thanks also for the sidenote!
      (As an aside; are you aware that for my $word (@output){say $word;} is more easily written as say for @output?)

      They are not the same:

      $ perl -E'my @x = qw/one two three/; for my $x ( @x ) { say $x }' one two three $ perl -E'my @x = qw/one two three/; say @x' onetwothree
        perl -E'my @x = qw/one two three/; say @x'

        You missed something...

        is more easily written as say for @output?) #.............................^^^

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012454]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-04-18 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (463 votes), past polls