december has asked for the wisdom of the Perl Monks concerning the following question:
Ave fellow monks,
From the frightening depths of ignorance I come for thee again, to ask for thy infinite wisdom.
I have the following construction (extract):
opendir(DIR, "$dirstr") || die ("[error] proc [...]"); rewinddir (DIR); @piclist = grep(!/^\.\.?$/ && /.jpg$/, readdir (DIR)); closedir (DIR); $index = grep $piclist[$_] eq $pic, 0 .. $#piclist; print "$index\n"; print "$piclist[$index] equals $pic\n";
It opens a directory, searches for a certain file in the list, and returns the index of that file (so I can link to previous and next picture in a gallery). The problem is that index is always '1', and the second print-out says 'SininenLaguuni.jpg equals KeltaisenKesanBooli.jpg', which is clearly not the case.
If I do the index-searching like this:
my ($c); foreach $c (0 .. $#piclist) { print "$c - $piclist[$c] equals $pic\n" if ($piclist[$c] eq $pic); }
... it seems to work: '9 - KeltaisenKesanBooli.jpg equals KeltaisenKesanBooli.jpg'.
What I don't understand is why the first solution doesn't and the second one does work. These are all list operations on lists, as far as I see.
Enlighten me, please... I'm sure it's something very stupid I'm too tired to see.
thanks,
wouter
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: not a list?
by Kanji (Parson) on Jul 26, 2002 at 02:10 UTC | |
by december (Pilgrim) on Jul 26, 2002 at 02:47 UTC | |
|
Re: not a list?
by dws (Chancellor) on Jul 26, 2002 at 03:56 UTC | |
by december (Pilgrim) on Jul 26, 2002 at 05:09 UTC |