Click here to get back home

sort

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sort asgweb 03-07-2008
---> Re: sort Ben Morrow03-07-2008
  `--> Re: sort Uri Guttman03-07-2008
Posted by asgweb on March 7, 2008, 10:03 pm
Please log in for more thread options
hello. i have code (below) that sorts the contents of a directory. it
works fine, but i need to show only files that start with an 'R'.
thanks in advance for your help.
opendir(STOCKBACKGROUND, ".") || &error("STOCKBACKGROUND");
@stockbackgrounds = readdir(STOCKBACKGROUND);
@stockbackgrounds = sort {lc($a) cmp lc($b)} @stockbackgrounds;
closedir(STOCKBACKGROUND);

Posted by Ben Morrow on March 7, 2008, 11:08 pm
Please log in for more thread options

> hello. i have code (below) that sorts the contents of a directory. it
> works fine, but i need to show only files that start with an 'R'.
> thanks in advance for your help.

perldoc -f grep

> opendir(STOCKBACKGROUND, ".") || &error("STOCKBACKGROUND");

Use lexical filehandles in non-ancient versions of Perl (since 5.6.0).

Don't call subs with & unless you need the special effects that causes.

Include the system error and what you were trying to do in the error
message.

I would recommend using 'or' instead of '||', as you can then drop the
parens. You may not like that style, though.

opendir my $STOCKBACKGROUND, "." or error "can't opendir '.': $!";

> @stockbackgrounds = readdir(STOCKBACKGROUND);

You should have

use strict;

at the top of your script; this line will the need to become

my @stockbackgrounds = readdir($STOCKBACKGROUND);

> @stockbackgrounds = sort {lc($a) cmp lc($b)} @stockbackgrounds;
> closedir(STOCKBACKGROUND);

I would do this all in one go, without the intermediate assignments;
also, if you use lexical filehandles they close themselves at the end of
the block they are in, so you can simply write

my @stockbackgrounds = do {
opendir my $D, '.' or error "can't opendir '.': $!";
sort { lc($a) cmp lc($b) }
grep /^R/, readdir $D;
};

You may find that too compressed, though.

Ben


Posted by Uri Guttman on March 7, 2008, 11:48 pm
Please log in for more thread options

BM> my @stockbackgrounds = do {
BM> opendir my $D, '.' or error "can't opendir '.': $!";
BM> sort { lc($a) cmp lc($b) }
BM> grep /^R/, readdir $D;
BM> };

even though File::Slurp is a pretty popular module, it also has a lesser
known read_dir sub that cleans up that code a bit:

use File::Slurp ;

my @stockbackgrounds = sort { lc($a) cmp lc($b) } grep /^R/, read_dir '.';

one day i will add a filter option and it would look something like
this:

my @stockbackgrounds = sort { lc($a) cmp lc($b) } read_dir '.', qr/^R/;

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Similar ThreadsPosted
can Perl Sort do this, unix sort breaks on it (muliple spaces as demiliter) February 2, 2005, 6:08 pm
Re: A Sort Optimization Technique: decorate-sort-dedecorate August 30, 2006, 4:32 pm
[Q]: How to sort first abc, then 123? March 12, 2005, 7:16 pm
sort? May 24, 2007, 1:02 pm
Odd sort() problem September 29, 2004, 10:59 am
FAQ 4.52: How do I sort an array by (anything)? October 25, 2004, 5:03 am
FAQ 4.52: How do I sort an array by (anything)? November 1, 2004, 12:03 am
sort directory help January 3, 2005, 2:38 pm
FAQ 4.52 How do I sort an array by (anything)? January 25, 2005, 12:03 am
string sort() February 12, 2005, 4:14 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap