Re: data manipulation
From: Bob (bNOoONb_at_not.pilbara.net.au)
Date: 10/10/03
- Next message: Bernard El-Hagin: "Re: Error returned while invoking a ".exe" from a perl script"
- Previous message: Haresh: "Error returned while invoking a ".exe" from a perl script"
- In reply to: Gunnar Hjalmarsson: "Re: data manipulation"
- Next in thread: Gunnar Hjalmarsson: "Re: data manipulation"
- Reply: Gunnar Hjalmarsson: "Re: data manipulation"
- Reply: Tad McClellan: "Re: data manipulation"
- Reply: John W. Krahn: "Re: data manipulation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 10 Oct 2003 16:08:56 +0930
"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:bm5fel$j9uep$1@ID-184292.news.uni-berlin.de...
> Bob wrote:
> >
> > -> push @chunks, $chunk if $chunk;
> > -> $chunk = '';
> >
> > After reading the push function description from "learning Perl" I
> > am failing to understand exactly what is happening here.
>
> It adds an element to the array @chunks with what's been stored in
> $chunk from previous iterations, and then it empties $chunk.
>
> The line
>
> push @chunks, $chunk if $chunk;
>
> can also be written
>
> if ($chunk) {
> push @chunks, $chunk;
> }
>
> My suggestion means that the whole log file ends up in memory in the
> array @chunks. I thought that made the code easier to understand, but
> it should be noted that if the log file is really big, it's not a good
> approach. In that case, you'd better do in the loop with respective
> 'generation' of $chunk whatever you want to do with it, and refrain
> from storing the whole file in an array.
>
> HTH
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
Thanks for the explanation, And I already understood the implication of the
whole file being in memory. In this case, it is not a problem, the log is
generally ony 3-5Meg but never over 50Meg.
For those interested, I have included the full script below
Regards,
B
#!/usr/bin/perl
#
# NAME qmail-qreadto
# purpose - look thru the qmail-qread logs for particular messages
#
use strict;
use warnings;
if (@ARGV == "0" ) {
print "\n\tqmail-qreadto \{ email to search for\} \n";
print "\t\texample\: qmail-qreadto me\@example.net \n\n";
exit 0
}
my ($Log, @Logs);
my $file1 = "/var/log/qmail-qread";
my $file2 = "/var/log/qmail-qread1";
if (-e $file2 ) {
open (MyFILE, $file2);
} else {
open (MyFILE, $file1);
}
while (<MyFILE>) {
if (/^\d{1,2}/) {
push @Logs, $Log if $Log;
$Log = '';
}
$Log .= $_;
}
close MyFILE;
push @Logs, $Log;
print grep {/@ARGV/} @Logs;
exit 0;
- Next message: Bernard El-Hagin: "Re: Error returned while invoking a ".exe" from a perl script"
- Previous message: Haresh: "Error returned while invoking a ".exe" from a perl script"
- In reply to: Gunnar Hjalmarsson: "Re: data manipulation"
- Next in thread: Gunnar Hjalmarsson: "Re: data manipulation"
- Reply: Gunnar Hjalmarsson: "Re: data manipulation"
- Reply: Tad McClellan: "Re: data manipulation"
- Reply: John W. Krahn: "Re: data manipulation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|