map woes

S

seven.reeds

Hi,

I have out-smarted myself. I have a directory that has various files
inside. Some are named like:

<number>.zone

The "<number>" bit is a simple integer so you might have the files:
1.zone, 31.zone, 820.zone, etc

I can open the dir. I can readdir() to get the filenames, grep() to
get the "\d+\.zone" files but now I want to pull off the numbers, sort
them numericaly highest to lowest and store them for later use. I
have tried

my @list =
sort {$b <=> $a}
map {s/(\d+)\.news/$1/}
grep(/\d+\.news/, readdir(NEWSDIR));

The readdir and grep work as I expect. The result of the map is a
list of "1"s, prolly the count of successful s// matches. Where am I
messing this up?
 
J

John W. Krahn

seven.reeds said:
I have out-smarted myself. I have a directory that has various files
inside. Some are named like:

<number>.zone

The "<number>" bit is a simple integer so you might have the files:
1.zone, 31.zone, 820.zone, etc

I can open the dir. I can readdir() to get the filenames, grep() to
get the "\d+\.zone" files but now I want to pull off the numbers, sort
them numericaly highest to lowest and store them for later use. I
have tried

my @list =
sort {$b <=> $a}
map {s/(\d+)\.news/$1/}
grep(/\d+\.news/, readdir(NEWSDIR));

The readdir and grep work as I expect. The result of the map is a
list of "1"s, prolly the count of successful s// matches. Where am I
messing this up?

The return value of s/// is always true or false (1 or ''). Just use m//
instead (and you don't really need grep):

my @list =
sort { $b <=> $a }
map /\A(\d+)\.news\z/,
readdir NEWSDIR;



John
 
M

Michele Dondi

map {s/(\d+)\.news/$1/}
grep(/\d+\.news/, readdir(NEWSDIR));

The readdir and grep work as I expect. The result of the map is a
list of "1"s, prolly the count of successful s// matches. Where am I
messing this up?

In that this is not yet Perl 6 and you don't have a .subst method yet.
You have to work around instead.

map { (my $s=$_) =~ s/(\d+)\.news/$1/; $s }


Michele
 
M

Mirco Wahab

seven.reeds said:
I can open the dir. I can readdir() to get the filenames, grep() to
get the "\d+\.zone" files but now I want to pull off the numbers, sort
them numericaly highest to lowest and store them for later use. I
have tried

my @list =
sort {$b <=> $a}
map {s/(\d+)\.news/$1/}
grep(/\d+\.news/, readdir(NEWSDIR));

The readdir and grep work as I expect. The result of the map is a
list of "1"s, prolly the count of successful s// matches. Where am I
messing this up?

There have been correct answers already, but I'll
add another version to the dozen (what I think what would
be somehow appropriate) ...

...
sub descending { $b<=>$a }
...

opendir my $dirh, '.' or die "cant't opendir!$!";

my @list = sort descending map /\d+(?=\.news)/g, readdir $dirh;

closedir $dirh;
...


Regards

M.
 
M

Michele Dondi

Others have pointed out how to do it. I just want to point out the
comma operator, and how it could be used in this context.

map {s/(\d+)\.news/$1/, $_}

Well, since it's a BLOCK-map(), it can be a semicolon, i.e. two
different statements. In which case it still sports a side effect that
is not what map() should really be for. And then there's a also a risk
of strange behaviours about which I learnt here and reported
elsewhere:

http://perlmonks.org/?node_id=623600


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top