Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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

Replies are listed 'Best First'.
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.
Re: Schwartzian Transfrom using a subroutine
by choroba (Bishop) on Jan 09, 2013 at 11:47 UTC
    Missing return or correct return value in the sub.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

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?
[Veltro]: feasible in the sense of 'making money'?
[marto]: if your app just calls a remote or third party service to do the task, you app does not 'do the task', it's just a messenger
[marto]: in the sense that for various values of "a while ago" it would not have been possible (and arguable still isn't) to have a fingerprint of every piece of commercially released music to compare against.
[marto]: a co-worker refers to the late 1980s as "a while ago"
[Veltro]: Like I said before, I ment the solution as a whole
[Veltro]: People thought it was not possible.
[marto]: and until recently it wasn't and arguable (for some samples) probably isn't
[Veltro]: How did we get here? Oh yeah, advertisement. So let's get back there. What I ment to say is that I think it is possible to create an algorithm that filters out advertisement
[Veltro]: And yes, maybe your decoder needs the cloud to do so.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-05-24 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?