----- Original Message ----- From: "Hendrik Maryns" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <beginners@perl.org>
Sent: Wednesday, March 16, 2005 5:35 PM
Subject: inplace editing



Hi,

some time ago I asked for some help to eliminate certain lines from a file. Someone suggested a solution with
{ local ( $^I, @ARGV ) = ( '', <*.log> );

For unix systems, I think you can do inplace editing w/out specifying an extension for a backup file, (and thus no backup is created). On a Windows machine, this is not the case. The line above would need to be be something like:


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

Here, it would create a backup file of the original with the extension .bak.


now when I run this I get the error "Can't do inplace edit without backup at lexmorf_ruisweg2.pl line 12."


So I'm not sure that I understand what $^I does (I read about the -i switch), and I'm rather sure I don't understand what assigning <*.log> to @ARGV does. If I get it right, the <> later on reads from <*.log>, but don't I have to open the files in some way first?

$^I does in a script what -i does on the command line. It tells perl that you are going to do inplace editing with some extension added as a suffix to your original file. <*.log> is the glob operator, and, in this instance, finds all files in the current working directory with an extension of .log and assigns them to @ARGV (so that when using the angle input operator in the while loop, it will get its files to edit from @ARGV). <> later on does not read from <*.log>, but from @ARGV.


Here's my code so far:

#! C:\Program Files\Perl\bin perl

use strict;
use warnings;

{ local ( $^I, @ARGV ) = ( '', <*.log> );
while ( <> ) {
s{\[\d+/\d+/\d+\s\d+:\d+\]\s}{}; #remove timestamp
next unless /^<.*>|^\*/; #remove information line
next if /^<.*>\s!/; #remove bot commands
s/\*(.*)\s/<$1> / if /^\*/;  #replace *nick with <nick>
print;
    }
}

__END__


The log files contain data of the following form:

[23/02/2005 19:20] =-= SnoopyD is now known as SnoopyDke
[23/02/2005 19:21] <SnoopyDke> in zen vel zekers als ze hem ni gestroopt hemme :p
[23/02/2005 19:21] <lieveke-> ej ma hebde da geleze van diene man die zijn ma zo had gestroopt ?
[23/02/2005 19:21] <lieveke-> en dan zo met da vel over hem rond liep ?
[23/02/2005 19:21] <lieveke-> iiiii
[23/02/2005 19:21] <lieveke-> :p
[23/02/2005 19:22] -->| nutty ([EMAIL PROTECTED]) has joined #chat2chat
[23/02/2005 19:23] -->| magali ([EMAIL PROTECTED]) has joined #chat2chat
[23/02/2005 19:23] <lieveke-> eh SnoopyDke :p
[23/02/2005 19:23] <lieveke-> hebde da geleze
[23/02/2005 19:23] <lieveke-> :p
[23/02/2005 19:23] <lieveke-> !sex Pietje
[23/02/2005 19:23] <Textbot> lieveke- sleurt Pietje eens mee de bosjes in om er donder en bliksem mee te maken...*rrrrrrrrrr*


Thanks for any help, H.

--
Hendrik Maryns

Interesting websites:
www.lieverleven.be (I cooperate)
www.eu04.com European Referendum Campaign
aouw.org The Art Of Urban Warfare




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