On Fri, Dec 31, 2004 at 03:25:53PM +0000, Oisin Peavoy wrote: > Hi, > > 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? > If it matters, I'm using MacOS X. > > > ---CODE--- > #!/usr/bin/perl -w > > use strict; > > my @array = ("fairy", "goblin", "emerald","dust","/dice"); > > foreach (@array){ > if(/(\/)+.+/){ > print "last\n"; > last; > }else{ > print "shift\n"; > shift @array; > } > } > print "@array";
perlsyn says: If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don't do that. The following seems to work for me: @a = qw(a b /c d e); shift @a while @a && $a[0] !~ m!/!; print "@a"; -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>