Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   sorting file names (http://www.velocityreviews.com/forums/t884030-sorting-file-names.html)

mixo 12-05-2003 07:54 AM

sorting file names
 
I have file name that have a prefix "t-", which
is followed by a number (integer), like
t-1
t-2
t-3
t-10
t-21
and so on.

How can I sort this in numeric order? This avoid a situation
where I get:
t-1
t-10
t-2
t-21
t-3
and so on.

So I far have the following which suffers from the above symptom:
++++++++++++
#!/usr/bin/perl -w
opendir THISDIR, "." or die "serious dainbramage: $!";
@allfiles = readdir THISDIR;
@allfiles = sort @allfiles;
#@allfiles = reverse @allfiles;


closedir THISDIR;
print "@allfiles\n";
++++++++++++


W K 12-05-2003 09:27 AM

Re: sorting file names
 

"Christian Winter" <thepoet_nospam@arcor.de> wrote in message
news:3fd040f3$0$20227$9b4e6d93@newsread2.arcor-online.net...

> But im almost 100% sure there are others here who can
> give you more elegant solutions.


I don't usually like to play about with "elegant" and "map" as some C
programmer might have to look at it.
Anyway:

@a=qw(t-1 t-12 t-4 t-50 t-24);
print join("\n",sort {substr($a,2)<=>substr($b,2)} @a);

Moral of the story : you can muck about with what is in the sort {block}




Ingo Menger 12-05-2003 01:53 PM

Re: sorting file names
 
mixo <mixo@beth.uniforum.org.za> wrote in message news:<bqpdjd$86k$1@ctb-nnrp2.saix.net>...


> @allfiles = sort @allfiles;


@allfiles = map { "t-$_" }
sort { $a <=> $b }
map { s/^t-// } @allfiles;

In english:
Assign to @allfiles the array that results from
prepending the string "t-" to each element of the array
that results from a numeric sort of the array
that results from stripping the string "t-" from each element of @allfiles

> #@allfiles = reverse @allfiles;


To reverse the sort, swap $a and $b in the sort sub.

Anno Siegel 12-05-2003 02:02 PM

Re: sorting file names
 
Ingo Menger <quetzalcotl@consultant.com> wrote in comp.lang.perl.misc:
> mixo <mixo@beth.uniforum.org.za> wrote in message
> news:<bqpdjd$86k$1@ctb-nnrp2.saix.net>...
>
>
> > @allfiles = sort @allfiles;

>
> @allfiles = map { "t-$_" }
> sort { $a <=> $b }
> map { s/^t-// } @allfiles;


Untested, eh?

You forgot that s/// returns success or failure of the replacement, not
the changed string. Change the last line to

map { s/^t-//; $_ } @allfiles;

Anno

Jeff 'japhy' Pinyan 12-05-2003 02:12 PM

Re: sorting file names
 
On 5 Dec 2003, Anno Siegel wrote:

>Ingo Menger <quetzalcotl@consultant.com> wrote in comp.lang.perl.misc:
>>
>> @allfiles = map { "t-$_" }
>> sort { $a <=> $b }
>> map { s/^t-// } @allfiles;

>
>You forgot that s/// returns success or failure of the replacement, not
>the changed string. Change the last line to
>
> map { s/^t-//; $_ } @allfiles;


I'd use grep(). It saves the effort of having to return $_, and it makes
more sense because this code is designed to sort those SPECIFIC files:

@allfiles =
map "t-$_",
sort { $a <=> $b }
grep s/^t-//,
@allfiles;

--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)


Sara 12-05-2003 02:52 PM

Re: sorting file names
 
mixo <mixo@beth.uniforum.org.za> wrote in message news:<bqpdjd$86k$1@ctb-nnrp2.saix.net>...
> I have file name that have a prefix "t-", which
> is followed by a number (integer), like
> t-1
> t-2
> t-3
> t-10
> t-21
> and so on.
>
> How can I sort this in numeric order? This avoid a situation
> where I get:
> t-1
> t-10
> t-2
> t-21
> t-3
> and so on.
>
> So I far have the following which suffers from the above symptom:
> ++++++++++++
> #!/usr/bin/perl -w
> opendir THISDIR, "." or die "serious dainbramage: $!";
> @allfiles = readdir THISDIR;
> @allfiles = sort @allfiles;
> #@allfiles = reverse @allfiles;
>
>
> closedir THISDIR;
> print "@allfiles\n";
> ++++++++++++



The statement

@allfiles = sort @allfiles;

is NOT a numeric sort. It's an alphanumeric sort. You'll need to use
the spaceship operator to do a numeric sort <=> . However you don't
have numbers..

Since you APPEAR to have a constant 'T-' in front of the filenames,
lets strip that off, sort numerically, then put it back. If its not a
true constant, you will have to store the prefix then put it back.

OK lets get busy. Remember, loops like for {} BAD! Let's use some Perl
instead:

#!/usr/bin/perl -wd

my @a = qw(T-1 T-12 T-5 T-3 T-6);
map s/^\D+//,@a; # stip off the prefix, don't really need the carat
but its OK
@a = sort { $a <=> $b } @a; # sort numerically using spaceship
map s/^/T-/,@a; # put the prefix back on
print "$_\n" for @a; # report success!


DB<1> c
T-1
T-3
T-5
T-6
T-12
Debugged program terminated. Use q to quit or R to restart,

Happy Holidays.
G

Jürgen Exner 12-05-2003 03:25 PM

Re: sorting file names
 
mixo wrote:
> I have file name that have a prefix "t-", which
> is followed by a number (integer), like
> t-1
> t-2
> t-3
> t-10
> t-21
> and so on.
>
> How can I sort this in numeric order?


Oh come on, it's not that difficult.

> ++++++++++++
> #!/usr/bin/perl -w
> opendir THISDIR, "." or die "serious dainbramage: $!";
> @allfiles = readdir THISDIR;
> @allfiles = sort @allfiles;


Did you read the documentation of sort()?
It clearly says:
[...]. If SUBNAME or
BLOCK is omitted, "sort"s in standard string comparison order.

And further down in the examples:
# sort numerically ascending
@articles = sort {$a <=> $b} @files;

Now, obviously your file names are not numbers, so the code block needs to
extract the number part of $a and $b before calling <=>. Because you have
such a well-defined format you could simply use substr to remove the
unwanted "t-":

sort {substr($a,2)<=>substr($b,2)} @a;

> closedir THISDIR;
> print "@allfiles\n";
> ++++++++++++


jue



David 12-05-2003 05:44 PM

Re: sorting file names
 
mixo <mixo@beth.uniforum.org.za> wrote:

> How can I sort this in numeric order? This avoid a situation
> where I get:
> t-1
> t-10
> t-2
> t-21
> t-3
> and so on.


Sounds like a job for Sort::Naturally, available at your local CPAN mirror.

- David

James E Keenan 12-05-2003 06:27 PM

Re: sorting file names
 
mixo <mixo@beth.uniforum.org.za> wrote in message news:<bqpdjd$86k$1@ctb-nnrp2.saix.net>...
> I have file name that have a prefix "t-", which
> is followed by a number (integer), like
> t-1
> t-2
> t-3
> t-10
> t-21
> and so on.
>
> How can I sort this in numeric order? This avoid a situation
> where I get:
> t-1
> t-10
> t-2
> t-21
> t-3
> and so on.
>
> So I far have the following which suffers from the above symptom:
> ++++++++++++
> #!/usr/bin/perl -w
> opendir THISDIR, "." or die "serious dainbramage: $!";
> @allfiles = readdir THISDIR;
> @allfiles = sort @allfiles;
> #@allfiles = reverse @allfiles;
>
>
> closedir THISDIR;
> print "@allfiles\n";
> ++++++++++++


my @list = qw(t-1
t-2
t-3
t-10
t-21);

my (@files);
push (@files, (split/-/, $_)[1]) for (@list);

for (sort {$a <=> $b } @files) {
print "t-$_\n";
# do appropriate processing
}

HTH

jimk

Chris Charley 12-05-2003 09:31 PM

Re: sorting file names
 
mixo <mixo@beth.uniforum.org.za> wrote in message news:<bqpdjd$86k$1@ctb-nnrp2.saix.net>...
> I have file name that have a prefix "t-", which
> is followed by a number (integer), like
> t-1
> t-2
> t-3
> t-10
> t-21
> and so on.
>
> How can I sort this in numeric order?


You could use the Schwartzian Transform. It is easier to follow if you
read from the end of the expression (because this is the order of
execution).
Note that each file name, and the digits to sort on are placed in an
anonymous array in the bottom map which is then passed to the sort
line, and so forth.

Chris

#!/usr/bin/perl
use strict;
use warnings;

my @files = qw / t-1.txt t-4.txt t-10.txt t-14.txt t-11.txt /;

my @sorted = map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map { [ $_, /-(\d+)/ ] }
@files;

print "@sorted\n";

__END__
output: t-1.txt t-4.txt t-10.txt t-11.txt t-14.txt


All times are GMT. The time now is 07:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.