implementing Schwartzian Transform
implementing Schwartzian Transform
am 18.11.2007 14:55:20 von jbl
I believe that Schwartzian Transform will do what I need if I ever
understand it.
I am trying to sort an array and ignore non alphanumeric characters.
Actual data being sorted is many regexes containing all of the usual;
regex characters ( )[ ]/ \ $ " ' . etc and these are what I need to
ignore during sort
I got this routine from a web site and now have misplaced the link to
it but I double checked it as I copied & pasted it and did not key it.
The @list data is something I added.
I get an error message:
error Schwartzian_Transform.pl line 15, near "/ }"
#!/usr/bin/perl
use warnings;
use strict;
@list = ( '*=ziggy', '<>Zaggy','animal', ']min&imal' , '-!animal]',
'%#Anardvark');
@dictionary_order_list =
map { /^\w* (.*)// } # line 15
sort
map {
my $d = lc; # convert to lower case
$d =~ s/[\W_]+//g; # remove non-alphanumerics
"$d $_" # [original, transform]
}
@list;
Thanks
jbl
Re: implementing Schwartzian Transform
am 18.11.2007 16:00:04 von Paul Lalli
On Nov 18, 8:55 am, jbl wrote:
> I believe that Schwartzian Transform will do what I need if I ever
> understand it.
>
> I am trying to sort an array and ignore non alphanumeric characters.
> Actual data being sorted is many regexes containing all of the usual;
> regex characters ( )[ ]/ \ $ " ' . etc and these are what I need to
> ignore during sort
>
> I got this routine from a web site and now have misplaced the link to
> it but I double checked it as I copied & pasted it and did not key it.
>
> The @list data is something I added.
>
> I get an error message:
> error Schwartzian_Transform.pl line 15, near "/ }"
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> @list = ( '*=ziggy', '<>Zaggy','animal', ']min&imal' , '-!animal]',
> '%#Anardvark');
> @dictionary_order_list =
> map { /^\w* (.*)// } # line 15
> sort
> map {
> my $d = lc; # convert to lower case
> $d =~ s/[\W_]+//g; # remove non-alphanumerics
> "$d $_" # [original, transform]
> }
> @list;
The syntax error is that you have an extra / in the top map block.
Remove the third one. It has no business being there. A pattern
match in Perl is two slashes[1] with the pattern match in between. A
search and replace (which you have in the second line of the bottom
map) is three slashes - the pattern to find in between the first two,
and the string to replace with in between the second and third.
There is also a problem with the comment in your bottom map block.
According to the comment, this block is returning a string consisting
of the original, followed by the transformed version of the string.
But the code actually returns the string in the opposite order -
transformed followed by original.
Paul Lalli
[1] Or any non-alpha-numeric character you choose to use as a
delimiter.
Re: implementing Schwartzian Transform
am 18.11.2007 16:54:56 von Tad McClellan
jbl wrote:
> I believe that Schwartzian Transform will do what I need if I ever
> understand it.
>
> I am trying to sort an array and ignore non alphanumeric characters.
> Actual data being sorted is many regexes containing all of the usual;
> regex characters ( )[ ]/ \ $ " ' . etc and these are what I need to
> ignore during sort
>
> I got this routine from a web site and now have misplaced the link to
> it
I recommend abandoning it, and writing a proper ST instead.
> but I double checked it as I copied & pasted it and did not key it.
Please make a short and complete program *that we can run*, as
suggested in the Posting Guidelines that are posted here frequently,
then copy and paste _that_ into your post.
> The @list data is something I added.
>
>
> I get an error message:
> error Schwartzian_Transform.pl line 15, near "/ }"
It is line 8, not 15, in the code you posted.
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> @list = ( '*=ziggy', '<>Zaggy','animal', ']min&imal' , '-!animal]',
> '%#Anardvark');
You must declare @list when you have enabled strict.
> @dictionary_order_list =
> map { /^\w* (.*)// } # line 15
The match operator has 2 slashes, not 3.
> sort
> map {
> my $d = lc; # convert to lower case
> $d =~ s/[\W_]+//g; # remove non-alphanumerics
> "$d $_" # [original, transform]
That is in the order [transform, original]...
> }
> @list;
Copy the ST given in the Perl FAQ, then modify it for your situation:
my @dictionary_order_list =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map {
my $d = lc; # convert to lower case
$d =~ tr/a-z//cd; # remove non-alphanumerics
[$_, $d] # [original, transform]
}
@list;
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: implementing Schwartzian Transform
am 19.11.2007 06:00:53 von Uri Guttman
>>>>> "TM" == Tad McClellan writes:
TM> Copy the ST given in the Perl FAQ, then modify it for your situation:
TM> my @dictionary_order_list =
TM> map { $_->[0] }
TM> sort { $a->[1] cmp $b->[1] }
TM> map {
TM> my $d = lc; # convert to lower case
TM> $d =~ tr/a-z//cd; # remove non-alphanumerics
TM> [$_, $d] # [original, transform]
TM> }
TM> @list;
or use Sort::Maker to generate the ST for you and copy/paste that code.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org