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: Beginner:
Simple array splitting

 



Siei
New User

May 2, 2013, 12:41 PM

Post #1 of 6 (485 views)
Simple array splitting Can't Post

I have an apache access log that I have split off the connect requests from so that each index in the array has something similar to this.

195.16.40.200 - - [10/Mar/2004:15:15:59:07 -500] CONNECT login.icq.com:433 HTTP/1.0 200 - - -


I'm trying to get an array of just the part starting with CONNECT and ending with HTTP/1.0 and can't seem to get it.


BillKSmith
Veteran

May 2, 2013, 1:05 PM

Post #2 of 6 (482 views)
Re: [Siei] Simple array splitting [In reply to] Can't Post


Code
use strict; 
use warnings;
use Data::Dumper;
my @array = (
'195.16.40.200 - - [10/Mar/2004:15:15:59:07 -500] CONNECT login.icq.com:433 HTTP/1.0 200 - - -',
);
my @new_array = map{/(CONNECT.*HTTP\/1.0)/} @array;
print Dumper \@new_array;

Good Luck,
Bill


Siei
New User

May 2, 2013, 1:45 PM

Post #3 of 6 (477 views)
Re: [BillKSmith] Simple array splitting [In reply to] Can't Post

Thank you that worked admirably. From there I took them and using hashes checked for unique keys now I have just a list of unique ones from the original array. Is there an easy way to count how many times a particular entry appears?

For instance how many times login.icq.com:433 appears in my original array? Or will I need to use anonymous hashes to keep such a count?


Laurent_R
Veteran / Moderator

May 2, 2013, 2:53 PM

Post #4 of 6 (473 views)
Re: [Siei] Simple array splitting [In reply to] Can't Post

Try something like this:


Code
my $count = scalar grep {/login\.icq\.com:433/} @new_array;



BillKSmith
Veteran

May 3, 2013, 10:38 AM

Post #5 of 6 (458 views)
Re: [Siei] Simple array splitting [In reply to] Can't Post

Keep the count at the same time you get the keys.


Code
use strict; 
use warnings;
use v5.14.0;
use Data::Dumper;
my @array = (
'195.16.40.200 - - [10/Mar/2004:15:15:47:07 -500] CONNECT login.icq.com:433 HTTP/1.0 200 - - -',
'195.16.40.200 - - [10/Mar/2004:15:15:59:07 -500] CONNECT login.icq.com:433 HTTP/1.0 200 - - -',
'195.16.40.200 - - [10/Mar/2004:15:16:59:07 -500] CONNECT login.icq.com:453 HTTP/1.0 200 - - -',
);
my @new_array = map { s/.*CONNECT(.*)HTTP\/1.0.*/$1/r } @array;
my %counts;
$counts{$_}++ foreach @new_array;
foreach my $access (keys %counts) {
print "$access $counts{$access}\n";
}


OUTPUT:

Code
 login.icq.com:433  2 
login.icq.com:453 1

Good Luck,
Bill


Kenosis
User

May 3, 2013, 1:45 PM

Post #6 of 6 (454 views)
Re: [BillKSmith] Simple array splitting [In reply to] Can't Post

Building on your excellent solution, the following uses named captures to do both in one iteration:


Code
use strict;  
use warnings;
use v5.14.0;
use Data::Dumper;
my @array = (
'195.16.40.200 - - [10/Mar/2004:15:15:47:07 -500] CONNECT login.icq.com:433 HTTP/1.0 200 - - -',
'195.16.40.200 - - [10/Mar/2004:15:15:59:07 -500] CONNECT login.icq.com:433 HTTP/1.0 200 - - -',
'195.16.40.200 - - [10/Mar/2004:15:16:59:07 -500] CONNECT login.icq.com:453 HTTP/1.0 200 - - -',
);

my %counts;
my @new_array = map { /(?<request>CONNECT(?<domain>.*)HTTP\/1.0)/; $counts{$+{domain}}++ if $+{domain}; $+{request}} @array;
foreach my $access (keys %counts) {
print "$access $counts{$access}\n";
}


 
 


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

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