Perl Monk, Perl Meditation 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 (Chancellor) 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?
 [Corion]: Meh, first round of escalations for me not wanting to fix in production what a project has mismanaged. Now another project, which eats up all the resources until end of this year wants to take that task and put it on my list of things as well.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2017-08-17 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (287 votes). Check out past polls.

Notices?