Re: Array size issue
by choroba (Cardinal) on Oct 15, 2012 at 17:14 UTC
|
You are printing all the lines each time. It means the output look like this: line1
line1
line2
line1
line2
line3
These are printed as three groups (containing 1, 2 and 3 lines). The last group has 3 lines, because you push when @matches has 2 elements.
| [reply] [d/l] [select] |
Re: Array size issue
by Anonymous Monk on Oct 15, 2012 at 17:13 UTC
|
You don't want to exit(0). You want to break out of the loop with last. Then, after the loop, not within it, you want to output your results.
Take some lines of the source file and walk through a printout of the code, using your fingertips. Ask yourself the questions that you ask the computer to make, and do what you have told the computer to do in response to them. The flaws in your present design will quickly become clear to you as they will best with no other way. | [reply] |
|
while (<$tailf>) {
chomp;
my $line = $_;
if($line =~ m/ on $ipaddress/)
{
if (scalar(@matches) <= 3){
push(@matches, $line);
}
}
last;
}
foreach $line (@matches){
print "$line\n";
}
| [reply] [d/l] |
|
| [reply] |
Re: Array size issue
by betterworld (Curate) on Oct 15, 2012 at 20:17 UTC
|
open my $tailf, "tail -f $logFile |" or die;
for (1,2) {
my $line = '';
$line = <$tailf> until $line =~ / on $ipaddress/;
push @matches, $line;
}
(Okay, you'd have to include some check for eof so it won't get into an endless loop if the input is too short. But I won't change it because it's so nice and short :) | [reply] [d/l] |
Re: Array size issue
by blue_cowdawg (Monsignor) on Oct 15, 2012 at 17:55 UTC
|
if($line =~ m/ on $ipaddress/)
{
if (scalar(@matches) <= 2){
push(@matches, $line);
}
else {
exit(0);
}
foreach $line (@matches){
print "$line\n";
}
}
This is where your trouble is. You probably meant to write it thusly:
if($line =~ m/ on $ipaddress/)
{
if (scalar(@matches) <= 2){
push(@matches, $line);
}
else {
foreach $line (@matches){
print "$line\n";
}
exit(0);
}
}
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] [d/l] [select] |
|
| [reply] |
|
Sorry.... could have fooled me considering the logic in your original post.
If I break your code down into pseudo-code it reads like this:
open a pipe to the tail -f command for a logfile (unnamed)
iterate over the pipe
if less than 3 files are in array
if the line matches a (undetermined) IP address
push the line into an array
otherwise
Iterate over the contents of the array
Part of the art of getting good answers on Perl Monks is being concise about your questions and not leaving what you are after up to the interpretation of the reader.
Once again I will attempt to extrapolate what you are after. You code will work better if you don't put the print lines in and change the exit for last.
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] |
|
|
Re: Array size issue
by smile4me (Beadle) on Oct 15, 2012 at 22:25 UTC
|
Is there a reason you are using "tail -f" in your file handle, as opposed to "tail -n 10" or similar. In my system, the script hangs waiting for additional input to the file. Here's an alternative idea for you to consider:
my @list;
my $file = q[/path/to/some/file];
my $regex = qr[your pattern];
open my $fh, "tail $file |" or die;
while (<$fh>) {
chomp;
say "testing: $_";
push @list, $_ if ( $_ =~ $regex);
last if (@list >= 2);
}
local $"="\n";
say "found: @list";
| [reply] [d/l] |
Re: Array size issue
by blue_cowdawg (Monsignor) on Oct 15, 2012 at 17:15 UTC
|
The problem is I should be getting 2 lines but instead I get 6 lines? Any ideas what I'm doing wrong? to run it: ./script string
There must be six lines in the logfile you are tailing. Default behavior of the tail command is to list the last 10 lines in a file. This isn't really an array size issue at all.
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] |
|
| [reply] |
|
$ tail -f multtable.txt
0
9
18
27
36
45
54
63
72
81
The code that generated that was:
for ix in 0 1 2 3 4 5 6 7 8 9
do
for iy in 0 1 2 3 4 5 6 7 8 9
do
n=`expr ${ix} '*' ${iy}`
echo $n >> multtable.txt
done
done
That makes 100 lines. I do a "tail -f" and I see the last 10 lines all day long until someone writes to it. While I can't comment on the data set you are working with I can tell you about the behavior of most *nix commands.
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] [d/l] [select] |