Translated thus:

#!/usr/bin/perl -w

use strict;

my @array = qw(fairy goblin /emerald dust dice);


shift @array while @array && $array[0] !~ m!/!; print "@array";

Confirmed that it works in Perl on WinXP.

However, the solution seems a bit elegant for a beginner. Even the condition:

if(/(^\/)/){ ....; }

fails.

Happy New Year to all the Perl folks in Northern Hemisphere. It's 2005 here!!

alfred,


Paul Johnson wrote:

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";




-- Alfred Vahau IT Services - University of Papua New Guinea PO Box 320 - University PO - NCD 134 Papua New Guinea Ph. (675) 3267277 - Fax. (675) 3267187 - Mobile: (675) 6888926 email: [EMAIL PROTECTED]




Reply via email to