Smith, Derek am Montag, 24. Juli 2006 22:03:
> -----Original Message-----
> From: John W. Krahn [mailto:[EMAIL PROTECTED]
> Sent: Monday, July 24, 2006 3:36 PM
> To: Perl Beginners
> Subject: Re: Regex find and replace - delete line
>
> Chris Charley wrote:
> > ----- Original Message ----- From: ""John W. Krahn""
>
> <[EMAIL PROTECTED]>
>
> >> And if you want to do it with one loop:
> >>
> >>
> >> for my $i ( reverse 0 .. $#file ) {
> >>  splice @file, $i, 1 if /foo/;
> >> }
> >
> > Hi John
> >
> > Shouldn't the above read like:
> >
> > for my $i ( reverse 0 .. $#file ) {
> >  splice @file, $i, 1 if $file[$i] =~ /foo/;
>
> Yes it should, sorry about that mistake.
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
>
> I tested this code
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @file = ();
> @file = qq(foo, derek, smith, bar);

Test again with

@file = qw(foo derek smith bar);

:-)

> for my $i ( reverse 0 .. $#file ) {
>     splice @file, $i, 1 if $file[$i] =~ /foo/;
> }
> print @file;
>
> and it prints nothing.  

The first and only element matched /foo/ and has been deleted.

> I thought the goal was to just splice the 
> element that matched foo???
>
> I changed it to
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @file = ();
> @file = qq(foo, derek, smith, bar);
> for my $i ( reverse 0 .. $#file ) {
>     splice @file, $i, 1 if $file[$i] eq "foo";
> }
> print @file;
>
> and it prints
> foo, derek, smith, bar

The first and only element was not 'foo' and has not been deleted.

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to