James Turnbull am Sonntag, 23. Juli 2006 08:01:
> Hi all

Hi James

> This feels like a really dumb question but doing a regex find and
> replace is it possible to delete a line?  For example,

The example below does not handle lines, but array entries (the entries may be 
used as lines in the extended context, but they are just array entries for 
the moment). The name '@file' may be a bit misleading.

> for (@file) {
>   s/foo//g;
> }

This means: For every entry in the array, replace all occurances of the 
string 'foo' with the empty string, for example
"My foo loves foos" => "My  loves s"

> which equates to:
>
> for each element in array 'file' if you find 'foo' then replace it with
> null.
>
> I then write the array to a file (using Tie::File) and it leaves a blank
> line where the element containing 'foo' was like:
>
> null
> foo1
> foo2
> foo3
>
> Perhaps I am confabulating two ideas here?  Is there a way to walk
> through an array and find a particular array element and delete it
> rather than replace it with null (and then write it out to a file using
> Tie::File)?

If you want to delete entries from @file completely...

a) that _equal_ to 'foo' (and the entry does not contain a line ending):

@file=grep { $_ eq 'foo' } @file;

b) that _contain_ one ore more 'foo' (presence of line ending does 
   not matter):

@file=grep { !/foo/ } @file;


Does this help?

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