good chemistry is complicated,and a little bit messy -LW PerlMonks

### Schwartzian Transfrom using a subroutine

by oliverhuber (Initiate)
 on Jan 09, 2013 at 11:42 UTC 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 ...?

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.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012454]
Approved by Athanasius
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-04-25 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (94 votes). Check out past polls.

Notices?