From: Oisin Peavoy <[EMAIL PROTECTED]>
> I have a Perl program which I'm having some difficulty with,
> essentially I'm
> trying to `shift()' all the ellements in an array untill an element
> containing a forward slash is encountered. Howerver, the method I'm
> using is producing incorrect results.
>
> If the forward slash is placed infront of "fairy" or "dust" the
> program executes correctly, if it's placed infront of one of the other
> words in the list the program doesn't produce the correct results.
>
> Can anyone help with this? What am I doing wrong?
>
> ---CODE---
> my @array = ("fairy", "goblin", "emerald","dust","/dice");
>
> foreach (@array){
> if(/(\/)+.+/){
> print "last\n";
> last;
> }else{
> print "shift\n";
> shift @array;
> }
> }
> print "@array";
The problem is that the foreach() doesn't notice that you shifted off
an item from the array and moves the pointer anyway. You want to do
something like this instead:
while (@array) {
last if $array[0] =~ m{/};
shift(@array);
}
That is if I understand your request correctly.
The m{/} is just another, more readable way to write /\//. That is a
regexp that matches any string containing a slash. Both the .+ and
the () in your regexp was unnecessary.
HTH, Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>