John W. Krahn schreef:

Hendrik Maryns wrote:

Hi,


Hello,

I'm writing a little script for removing "rustle" from a log file from chat channels, in order to do linguistic research on them. I took the file and tied it with Tie::File, in order to easily acces it. This probably isn't all necessary here, but I want to modify the file itself, not writing the output to a new one.

The first thing is stripping of a date and time at the beginning of each line. My re seems to be correct, as it works. I do not understand why I need the /g modifier though. If I remove it, only the first line that matches gets stripped. I thought the substitution was started all over again for every line?


It should work without the /g modifier. Using /g means that you want to match
the pattern one or more times on the same string and I assume that you only
want to match it once?



Well, in writing this, I solved half of my problem, but one still remains: how can I remove a line? I tried with delete, as you see below, but (of course) this does not work, as $lijn is no array element. How can I totally remove that line from my file?


The simple answer is to use grep():

I came up with this solution too, but indeed, it reads the file twice, which takes quite some time.


tie my @bestand, 'Tie::File', $best or die "Kon het bestand niet binden: ", $!;
s{\[\d+/\d+/\d+\s\d+:\d+\]\s}{} for @bestand;
@bestand = grep ! /^<.*>/, @bestand;



But that means that you have to traverse the array twice. You can do both in
a single loop like this:


tie my @bestand, 'Tie::File', $best or die "Kon het bestand niet binden: ", $!;

for ( reverse 0 .. $#bestand ) {
    $bestand[ $_ ] =~ s{\[\d+/\d+/\d+\s\d+:\d+\]\s}{};
    splice @bestand, $_, 1 if $bestand[ $_ ] !~ /^<.*>/;
}


Note that you have to start at the end of the array for splice() to remove the
correct element.

Can you explain this a bit further? I read about splice in the perlop page, but don't quite see through it yet.


Another method is to use the in-place edit variable along with the @ARGV array
although this method does not really edit the file "in-place".


{ local ( $^I, @ARGV ) = ( '', <*.log> );

    while ( <> ) {
       s{\[\d+/\d+/\d+\s\d+:\d+\]\s}{};
        next unless /^<.*>/;
        print;
    }
}

What does $^I do? "The current value of the inplace-edit extension. Use undef to disable inplace editing. (Mnemonic: value of -i switch.)" isn't really helpful, I'm afraid.


Thanks for the many suggestions!

H.


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