Article View: comp.sources.d
Article #4973Re: Perl help requested
From: J Greely
Date: Tue, 09 May 1989 16:57
Date: Tue, 09 May 1989 16:57
53 lines
2385 bytes
2385 bytes
In article <69461@pyramid.pyramid.com> jimmy@pyramid.pyramid.com
(Jimmy Aitken) writes:
>I've got a perl program that requires an array to be sorted ignoring
>case and non alpha-numeric characters. i.e. The equivalent of
>'sort -fd'.
The *fastest* way may be to actually use "sort -fd", but that violates
the kitchen sink principle of perl, so here goes.
>sub fieldsort {
> local($x=$a, $y=$b);
> $x =~ tr/A-Z/a-z/;
> $x =~ tr/ -@{-}//;
> $y =~ tr/A-Z/a-z/;
> $y =~ tr/ -@{-}//;
> $x lt $y ? -1 : $x gt $y ? 1 : 0;
>}
The biggest problem with this is that you're munging both strings for
each comparison, which is where your time is wasted. One
ugly-but-faster way to do the job would be to pre-chew the array, like
this:
# create sorting array, appending position in original array
# (so we can later move the real array into matching order)
#
@sort_list = ();
for ($line=0;$line <= $#list; $line++) {
$temp = $list[$line];
$temp =~ tr/A-Z/a-z/;
$temp =~ tr/ -@{-}//;
$temp .= "A$line"; # choose something not in any string. We *know*
# there's no capital A's anymore. You might prefer
# something like DEL, that would sort after anything
push(@sort_list,$temp);
}
@sort_list = sort(@sort_list);
#
# loop through the sorted copy, replacing the hacked version with
# the original
#
for ($line=0;$line <= $#sort_list;$line++) {
($temp,$num) = split(/A/,$sort_list[$line]);
$sort_list[$line] = $list[$num];
}
# sort_list now contains the original array, correctly sorted
This isn't perfect, and it *is* ugly, but it basically works. Having
typed this, I would probably just break down and call sort, but it's
a nice exercise.
-=-
J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)
Technical Details
Message-ID:
<47567@tut.cis.ohio-state.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!previous.cis.ohio-state.edu!jgreelyReferences:
<69461@pyramid.pyramid.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!previous.cis.ohio-state.edu!jgreely
From: jgreely@previous.cis.ohio-state.edu (J Greely)
Newsgroups: comp.sources.d
Subject: Re: Perl help requested
Keywords: perl sort
Message-ID: <47567@tut.cis.ohio-state.edu>
Date: 9 May 89 16:57:14 GMT
References: <69461@pyramid.pyramid.com>
Sender: news@tut.cis.ohio-state.edu
Reply-To: J Greely <jgreely@cis.ohio-state.edu>
Organization: The Ohio State University, Department of
Computer and Information Science
Lines: 53