Some matching in arrays

S

Sandman

I am doing some logfile parsing (of an online chat function) and have a
script in particular that sums events that are being searched for. In
short:

Logfile structure:

JOIN 12:34 Lucille!12.34.43.21 joined
KICK 12:35 Smith!23.45.170.23 kicked Lucille
JOIN 12:36 Lucilla!12.34.43.21 joined
KICK 12:37 Smith!23.45.170.23 kicked Lucilla

(note the difference in name of Lucilla/Lucille)

So, when summing up the presence of a particular user, I grep the file
and deal with the result in a foreach:

forach (@lines){
chomp;
($kind,$time,$host,@text)=split " ";
$thetext = join " ", @text;
($nick,$ip)=split "!", $host;
push @nicks, $nick;
$sum{$ip}{$kind}++;
}

That's a short version. Now, the @lines array contains information I've
grep:ed for, and as such, Lucille can be found with different nicks,
which I save in @nicks (well, I save all of them, not only the differing
ones).

So, in my summary, I want summarize how many times Lucille has been
kicked. Now, the key is the IP, not the nick, and the IP of the kickee
isn't present in the kick line, which is why I save the nicks. What I am
looking for is something like this, in pesudo:

if $thetext contains "kicked (any of the nicks in @nicks)"

How would you do it?
 
G

gnari

Sandman said:
I am doing some logfile parsing (of an online chat function) and have a
script in particular that sums events that are being searched for. In
short:

Logfile structure:

JOIN 12:34 Lucille!12.34.43.21 joined
KICK 12:35 Smith!23.45.170.23 kicked Lucille
JOIN 12:36 Lucilla!12.34.43.21 joined
KICK 12:37 Smith!23.45.170.23 kicked Lucilla

(note the difference in name of Lucilla/Lucille)

So, when summing up the presence of a particular user, I grep the file
and deal with the result in a foreach:

forach (@lines){
chomp;
($kind,$time,$host,@text)=split " ";
$thetext = join " ", @text;
or:
($kind,$time,$host,$thetext)=split " ",$_,3;
($nick,$ip)=split "!", $host;
or even something like:
($kind,$time,$nick,$ip,$thetext)=/^(\S+) (\S+) (\w+)!(.*?) (.*)$/;

So, in my summary, I want summarize how many times Lucille has been
kicked. Now, the key is the IP, not the nick, and the IP of the kickee
isn't present in the kick line, which is why I save the nicks.

$ipofnick{$nick}=$ip;
$kicked{$ipofnick{$1}}++ if $thetext=~/kicked (\w+)/;


gnari
 
B

Bob Walton

Sandman said:
I am doing some logfile parsing (of an online chat function) and have a
script in particular that sums events that are being searched for. In
short:

Logfile structure:

JOIN 12:34 Lucille!12.34.43.21 joined
KICK 12:35 Smith!23.45.170.23 kicked Lucille
JOIN 12:36 Lucilla!12.34.43.21 joined
KICK 12:37 Smith!23.45.170.23 kicked Lucilla

(note the difference in name of Lucilla/Lucille)

So, when summing up the presence of a particular user, I grep the file
and deal with the result in a foreach:

forach (@lines){
chomp;
($kind,$time,$host,@text)=split " ";
$thetext = join " ", @text;
($nick,$ip)=split "!", $host;
push @nicks, $nick;
$sum{$ip}{$kind}++;
}

That's a short version. Now, the @lines array contains information I've
grep:ed for, and as such, Lucille can be found with different nicks,
which I save in @nicks (well, I save all of them, not only the differing
ones).

So, in my summary, I want summarize how many times Lucille has been
kicked. Now, the key is the IP, not the nick, and the IP of the kickee
isn't present in the kick line, which is why I save the nicks. What I am
looking for is something like this, in pesudo:

if $thetext contains "kicked (any of the nicks in @nicks)"

How would you do it?

Well, you want to look up something in @nicks. Therefore, @nicks should
have been a hash (because hashes are perfect for looking things up in --
that's why they're there), keyed on $nick and storing $ip. Then, given
a $nick, you could quickly and easily determine the corresponding $ip.
You can use the exists() function to determine if a given $nick is
present. The thing you will lose with a hash is the ordering and the
duplicates. But those don't seem to be important for the task you are
describing.
 
T

Tore Aursand

JOIN 12:34 Lucille!12.34.43.21 joined
KICK 12:35 Smith!23.45.170.23 kicked Lucille
JOIN 12:36 Lucilla!12.34.43.21 joined
KICK 12:37 Smith!23.45.170.23 kicked Lucilla

forach (@lines){
chomp;
($kind,$time,$host,@text)=split " ";
$thetext = join " ", @text;
($nick,$ip)=split "!", $host;
push @nicks, $nick;
$sum{$ip}{$kind}++;
}

That's a short version. Now, the @lines array contains information I've
grep:ed for, and as such, Lucille can be found with different nicks,
which I save in @nicks (well, I save all of them, not only the differing
ones).

So, in my summary, I want summarize how many times Lucille has been
kicked. Now, the key is the IP, not the nick, and the IP of the kickee
isn't present in the kick line, which is why I save the nicks.

Isn't it easier to count this while reading the logfile?

my %kicked = ();
foreach ( @lines ) {
# ...
if ( $thetext =~ m,\s+kicked\s+(.*)$, ) {
$kicked{$1}++;
}
}
 
S

Sandman

Bob Walton said:
Well, you want to look up something in @nicks. Therefore, @nicks should
have been a hash (because hashes are perfect for looking things up in --
that's why they're there), keyed on $nick and storing $ip. Then, given
a $nick, you could quickly and easily determine the corresponding $ip.
You can use the exists() function to determine if a given $nick is
present. The thing you will lose with a hash is the ordering and the
duplicates. But those don't seem to be important for the task you are
describing.

Yes, I think that's the best suggestion. I should to something like this:

$nicks{$nick}++;
$kicks++ if exists $nicks{$text[1]};

Right?
 
S

Sandman

Tore Aursand said:
Isn't it easier to count this while reading the logfile?

my %kicked = ();
foreach ( @lines ) {
# ...
if ( $thetext =~ m,\s+kicked\s+(.*)$, ) {
$kicked{$1}++;
}
}

Then I would end up with a hash of nicks that has been kicked, not necessarily
those related to Lucille/Lucilla.

I think Bob Waltons suggestion hit the spot.
 
T

Tore Aursand

Then I would end up with a hash of nicks that has been kicked, not
necessarily those related to Lucille/Lucilla.

Do you want to know how many times only those two has been kicked, or
should you be able to tell how many times _someone_ has been kicked?

My approach deals with both of these.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top