Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sort

Reply
 
 
asgweb
Guest
Posts: n/a
 
      03-08-2008
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);
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-08-2008

Quoth asgweb <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      03-08-2008
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

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 ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
The Colourised Bewitched -- sort of OK....... sort of! anthony DVD Video 26 06-28-2005 04:39 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
What is faster? C++ vector sort or sort in database JerryJ C++ 11 04-28-2004 10:23 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments