CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Comparing line against line in a foreach loop

 



Warren Bell
Deleted

Jan 22, 2001, 10:49 PM

Post #1 of 8 (1146 views)
Comparing line against line in a foreach loop Can't Post

I'm trying to proccess a log file for the same IP more than a certain number of occurences. If the same IP shows up more than 3 times I want it to be pushed into an array like @repeatip. So at the end of proccessing the log file I have the array @repeatip with all the IP's that showed up more than 3 times in the log file.

I know how to do everything else I just can't figure out how to compare IP's against themselves in the foreach lop. Also, how would I detect one group of IP's and push it into @repeatip and then move on to different IP's in the same foreach loop?

For example if the log file had:

1.2.3.4
1.2.3.4
1.2.3.4
1.2.3.4

2.3.4.5
2.3.4.5
2.3.4.5
2.3.4.5

TIA



japhy
Enthusiast

Jan 23, 2001, 7:10 AM

Post #2 of 8 (1137 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

When you want the count of something, you want to use a hash.


Code
my (%count,@repeat); 

open IP, $IP_file or die "can't read $IP_file: $!":
while (<IP>) {
chomp;
push @repeat, $_ if ++$count{$_} == 4;
}
close IP;



Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Warren Bell
Deleted

Jan 23, 2001, 9:33 AM

Post #3 of 8 (1133 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

Ok, this is what I have. I probably don't need my first part if I can have yours look for each line that contains 'from any ip number' (like 'from 1.2.3.4') and only push the IP into the repeat array if it occurs more than 3 times.

$tcplog = '/var/log/iplog';

open(FILE, "$tcplog") || die "Unable to open $tcplog: $!";
@log = <FILE>;
close(FILE);

foreach $line (@log) {
# used to get all IP's from the log file into an array so I can proccess them later to find out which ones occur more than 3 times.
if ($line =~ /from (\d+\.\d+\.\d+\.\d+)/) {
push(@ips,$1);
}

}

# How can I eliminate the above part and just have yours push just the IP's into the array if there is more than 3 of the same IP in the file?

my (%count,@repeat);

open(IP, $IP_file) || die "can't read $IP_file: $!":
while (<IP>) {
chomp;
push @repeat, $_ if ++$count{$_} == 4;
}
close(IP);

Thanks again



japhy
Enthusiast

Jan 23, 2001, 10:03 AM

Post #4 of 8 (1132 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

DO NOT READ FILES INTO ARRAYS.

That is inefficient, and leads to more inefficient code.

As per your modification, it's very simple:


Code
my (%count, @repeated); 

open IP, $ip_file or die "can't read $ip_file: $!";
while (<IP>) {
next unless /^from (\d+\.\d+\.\d+\.\d+)/;
push @repeated, $1 if ++$count{$1} == 4;
}
close IP;

There you go.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


modified
Deleted

Jan 23, 2001, 1:17 PM

Post #5 of 8 (1128 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

If you can't read files into arrays, how do extract the information you need from them?



japhy
Enthusiast

Jan 23, 2001, 3:41 PM

Post #6 of 8 (1127 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

You loop over the file line by line. It's much more efficient in the long run.


Code
open FILE, $filename or die "can't open $filename: $!"; 
while (<FILE>) {
# do something with $_
}
close FILE;

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


modified
Deleted

Jan 23, 2001, 4:28 PM

Post #7 of 8 (1124 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

I'm just wondering, is there any reason why that's better?



japhy
Enthusiast

Jan 23, 2001, 4:30 PM

Post #8 of 8 (1123 views)
Re: Comparing line against line in a foreach loop [In reply to] Can't Post

You will understand why it is better when you open a large file (on the order of megabytes).

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives