Sort of a sort problem. 
Author Message
 Sort of a sort problem.

Hi guys,
I have the following problem:
I have a directories with files called,

abod130
dfo131
abe132
abod133
dfo134
abe135
abod136
dfo137
etc.....

I need to make a script which does the following
##Pseudocode##:

- Sorts the files in numerical order regardless of prefix into an array.
I'm thinking that I could use some kind of map or sort routine but i
would appreciate a link or a pointer to a tutorial.

Thankful for any help.
Jimmy Lantz



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.
Jimmy Lantz wrote in comp.lang.perl.misc:

Quote:
>Hi guys,
>I have the following problem:
>I have a directories with files called,

>abod130
>dfo131
>abe132
>abod133
>dfo134
>abe135
>abod136
>dfo137
>etc.....

>I need to make a script which does the following
>##Pseudocode##:

>- Sorts the files in numerical order regardless of prefix into an array.
>I'm thinking that I could use some kind of map or sort routine but i
>would appreciate a link or a pointer to a tutorial.

Here are the links (not surprisingly, to the perl documentation bundled
with it):
  perldoc -f readdir
  perldoc -f sort
  man perlre
(the last one, for knowing how to extact the numerical part of the
filenames).

--
Rafael Garcia-Suarez



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.

Quote:

> Hi guys,
> I have the following problem:
> I have a directories with files called,

> abod130
> dfo131
> abe132
> abod133
> dfo134
> abe135
> abod136
> dfo137
> etc.....

> I need to make a script which does the following
> ##Pseudocode##:

> - Sorts the files in numerical order regardless of prefix into an array.

You could do something like this:

sub JimmySort
    {
    my($alpha,$beta)=($a,$b);
    $alpha=~s/\D//; #zap all non-digits
    $beta=~s/\D//;
    $alpha<=>$beta;
    }


Read up on the sort function if this isn't obvious to you.

Andras



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.

Quote:

> Hi guys,
> I have the following problem:
> I have a directories with files called,

> abod130
> dfo131
> abe132
> abod133
> dfo134
> abe135
> abod136
> dfo137
> etc.....

> I need to make a script which does the following
> ##Pseudocode##:

> - Sorts the files in numerical order regardless of prefix into an array.
> I'm thinking that I could use some kind of map or sort routine but i
> would appreciate a link or a pointer to a tutorial.

Perhaps someone else will post a good tutorial reference -- I'm not
sure what to reference.  But this will do the trick:


# Schwartzian transform definitely useful here
print map $_->[1],
        sort { $a->[0] <=> $b->[0] }
          map [ /\d+$/g, $_ ],

--
Ren Maddox



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.


...

Quote:
> > - Sorts the files in numerical order regardless of prefix into an array.

> You could do something like this:

> sub JimmySort
>     {
>     my($alpha,$beta)=($a,$b);
>     $alpha=~s/\D//; #zap all non-digits

That doesn't do what the comment says it does.

      $alpha =~ s/\D+//g;

Or, *much* faster,

      $alpha =~ tr/0-9//cd;

Quote:
>     $beta=~s/\D//;

Similarly.

Quote:
>     $alpha<=>$beta;
>     }



> Read up on the sort function if this isn't obvious to you.

It isn't the way to do it if performance for a reasonably long list is a
problem, because the sortkeys (the digits) are extracted far too often
(once per comparison instead of once per sort).  Use the Schwartz
Transform posted by Ren Maddux, or the Guttman-Rosler Transform.

See <URL:http://www.hpl.hp.com/personal/Larry_Rosler/sort/> for details.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.

Quote:

> sub JimmySort
>     {
>     my($alpha,$beta)=($a,$b);
>     $alpha=~s/\D//; #zap all non-digits
>     $beta=~s/\D//;
>     $alpha<=>$beta;
>     }

This looks effective (I'm sure you meant s/\D//g), but rather inefficient,
since 2 regexes will be evaluated for each comparison.  For larger arrays it




--
Stephen Kloder               |   "I say what it occurs to me to say.

Phone 404-874-6584           |   -- The Man in the Shack
ICQ #65153895                |            be :- think.



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.


...

As Tony the Tiger might say, "That's GReaT!".

You could save a few strokes by moving the regex directly into the
argument list, instead of the $1.  Any captures go directly into the
list.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.
On Wed, 09 Aug 2000 20:23:38 -0400, Stephen Kloder
...

Quote:
> Or, use a one-to-one transformation, which can be easily undone:





Which works as long as all numbers have the same number of digits.

Better to fix the number of digits (4 in my case):



--
Good luck,
Abe



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.

This is dangerous.  What if /(\d+)/ fails?  You get the previous $1!

*never* use $1 unless it is in conditional code based on the success
of the match!  Or at least, never use it, until you understand why
I say never, then you can use it. :-)

print $1 while "Just another Perl hacker," =~ /(.)/g

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.
[Posted and a courtesy copy mailed.]



Incorrect attribution.  I didn't post that code.  But I didn't pee on it
either, because it really is OK, considering the data set that was
posited, for which the regex couldn't fail.

Quote:


> This is dangerous.  What if /(\d+)/ fails?  You get the previous $1!

> *never* use $1 unless it is in conditional code based on the success
> of the match!  Or at least, never use it, until you understand why
> I say never, then you can use it. :-)

Your objection is solved simply by using a regex that cannot fail:

      /(\d*)$/

I would also capture the match directly in the argument list:



The worst that could happen there is a warning on conversion of a null
string to 0; a local $^W = 0; could deal with that.

(For ultimate speed, I would pack() to a two-byte short unsigned
integer, instead of sprintf() to a four-byte digit string; but that is
irrelevant to your objections.)

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.

Quote:

>> *never* use $1 unless it is in conditional code based on the success
>> of the match!  Or at least, never use it, until you understand why
>> I say never, then you can use it. :-)

Larry> Your objection is solved simply by using a regex that cannot fail:

Larry>       /(\d*)$/

That's why I said "never, until you understand why I said never".
*You* (Larry) obviously understand why I said "never". :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Wed, 18 Jun 1902 08:00:00 GMT  
 Sort of a sort problem.

Quote:

>(For ultimate speed, I would pack() to a two-byte short unsigned
>integer, instead of sprintf() to a four-byte digit string; but that is
>irrelevant to your objections.)

In BigEndian mode. But that was obvious?

--
        Bart.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. re-sorting a sorted list (and sorting by date)

2. YAPSQ (Perl Style Question): sorting and maintaining sorted order

3. Perl sort different from unix sort

4. SORT sort hash with alpha-numeric keys

5. Sorting a hash by value and sub sorting the keys

6. PROPOSAL: Sort::Topological: general topological sorting of acyclic directed graphs

7. PERLFUNC: sort - sort a list of values

8. PERLFUNC: sort - sort a list of values

9. sort of about sort

10. strange SEGV with sort or some sort of such thing

11. sort with dynamic sort expression help

12. insert to sorted list via sort?

 

 
Powered by phpBB® Forum Software