Some matching in arrays

Discussion in 'Perl Misc' started by Sandman, Jan 12, 2004.

  1. Sandman

    Sandman Guest

    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?
    Sandman, Jan 12, 2004
    #1
    1. Advertising

  2. Sandman

    gnari Guest

    "Sandman" <> wrote in message
    news:-berlin.de...
    > 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
    gnari, Jan 12, 2004
    #2
    1. Advertising

  3. Sandman

    Bob Walton Guest

    Sandman wrote:

    > 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.

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Jan 12, 2004
    #3
  4. Sandman

    Tore Aursand Guest

    On Mon, 12 Jan 2004 01:09:38 +0100, Sandman wrote:
    > 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}++;
    }
    }


    --
    Tore Aursand <>
    "I didn't have time to write a short letter, so I wrote a long one
    instead." -- Mark Twain
    Tore Aursand, Jan 12, 2004
    #4
  5. Sandman

    Sandman Guest

    In article <>,
    Bob Walton <> wrote:

    > Sandman wrote:
    >
    > > 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.


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

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

    Right?

    --
    Sandman[.net]
    Sandman, Jan 12, 2004
    #5
  6. Sandman

    Sandman Guest

    In article <>,
    Tore Aursand <> wrote:

    > On Mon, 12 Jan 2004 01:09:38 +0100, Sandman wrote:
    > > 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}++;
    > }
    > }


    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.

    --
    Sandman[.net]
    Sandman, Jan 12, 2004
    #6
  7. Sandman

    Tore Aursand Guest

    On Mon, 12 Jan 2004 20:38:25 +0100, Sandman wrote:
    >>> 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}++;
    >> }
    >> }


    > 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.


    --
    Tore Aursand <>
    "War is too serious a matter to entrust to military men." -- Georges
    Clemenceau
    Tore Aursand, Jan 13, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francesco Moi

    Matching words within two arrays

    Francesco Moi, Feb 20, 2005, in forum: C++
    Replies:
    1
    Views:
    321
    Thomas Matthews
    Feb 20, 2005
  2. Philipp
    Replies:
    21
    Views:
    1,102
    Philipp
    Jan 20, 2009
  3. Nick Black

    matching arrays within arrays

    Nick Black, Jan 23, 2007, in forum: Ruby
    Replies:
    3
    Views:
    132
    Nick Black
    Jan 23, 2007
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    212
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page