Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.
@stockbackgrounds = readdir(STOCKBACKGROUND);
@stockbackgrounds = sort {lc($a) cmp lc($b)} @stockbackgrounds;

Re: sort

Hallo, I made a little script that sort the lines of a file:

#! /usr/bin/perl

sub numerically {$a <=> $b;}

@array = <>;

@array = sort numerically (@array);

print (@array);

Maybe the code is not so good, but why if I give thi input file:

The result is this:

As I see perl sots only the first two field delimited by a ".", the others
are inserted as a fifo, first line encountered first line wrote in output,
why doesn't perl compare all the line?

Thanks, Pietro.
I will build myself a copper tower
With four ways out and no way in
But mine the glory, mine the power
(So I chose AmigaOS and GNU/Linux)

Re: sort

Pietro wrote:

Quoted text here. Click to load it

No, Perl sort the strings as numbers. If you convert the string
'' to a number then you get the number 1.3 (and you'll get a
warning too if you've enabled warnings.

Sounds to me like you may want to compare the strings as IP address.

There are modules on CPAN to manipulate IP addresses.

Re: sort

Pietro wrote:
Quoted text here. Click to load it

You're using a *numeric* sort. "" etc are not numbers.

If you had "use warnings;", you would see a load of these:

Argument "\n" isn't numeric in numeric comparison (<=>)


Re: sort

Quoted text here. Click to load it

perldoc -f grep

Quoted text here. Click to load it

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

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 '.': $!";

Quoted text here. Click to load it

You should have

    use strict;

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

    my @stockbackgrounds = readdir($STOCKBACKGROUND);

Quoted text here. Click to load it

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.


Re: sort

  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

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


Uri Guttman  ------  -------- --
-----  Perl Architecture, Development, Training, Support, Code Review  ------
-----------  Search or Offer Perl Jobs  ----- ---------
---------  Gourmet Hot Cocoa Mix  ---- ---------

Site Timeline