Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: Use split

Expand Messages
  • perl_beg
    Hi David, here is my piece of code. I got the first part working that you were helping me with originally much thanks. I tried to include the next part of the
    Message 1 of 15 , Apr 30, 2008
    • 0 Attachment
      Hi David, here is my piece of code. I got the first part working
      that you were helping me with originally much thanks.

      I tried to include the next part of the code to capture the date in
      the logfile. For example - "Tue Apr 20 2008" but when I compare what
      is in the file ($datestr =~ @array) of course it's not working
      properly. Any suggestions would be great.


      my $datestr = UnixDate("today","%a %b %e %Y");


      %abuser_cnt = ();

      my $file="/var/log/app.log";

      open (INFILE, "<$file") || die "Cannot open file";
      while (<INFILE>) {
      chomp;
      if ( $_ =~ m/-j\s?[0-9]?/){
      my @array = ($day, $mth, $daynum, $year) = (split(/
      \s+/,$_))[0,1,2,4];
      if ($datestr =~ @array ) {
      my ($day, $mth, $daynum, $year, $user) = (split(/\s+/,$_))
      [0,1,2,4,7];
      $abuser_cnt{$day,$mth,$daynum,$year,$user}++;
      }
      }
      }

      @abusers = sort keys %abuser_cnt;

      foreach $user (@abusers) {
      print "$user\n";
      }
    • david wright
      Hello perl_beg, ... I think your looking for this: ($datestr =~ /@array/) additionally, some of the logic in your code can condensed. ... this doesn t make
      Message 2 of 15 , May 2, 2008
      • 0 Attachment
        Hello perl_beg,

        > when I compare what is in the file ($datestr =~ @array)

        I think your looking for this: ($datestr =~ /@array/)

        additionally, some of the logic in your code can condensed.

        > my @array = ($day, $mth, $daynum, $year) =
        > (split(/
        > \s+/,$_))[0,1,2,4];
        > if ($datestr =~ @array ) {
        > my ($day, $mth, $daynum, $year, $user) =
        > (split(/\s+/,$_))
        > [0,1,2,4,7];

        this doesn't make sense to me. why not just split once?

        > $abuser_cnt{$day,$mth,$daynum,$year,$user}++;

        I'm not sure what you want to do here...

        I believe you could refactor to somdthing along these lines:

        while (<INFILE>) {
        chomp;
        if (m/-j\s?[0-9]?/){
        my ($day, $mth, $daynum, $year, $user) = (split(/\s+/,$_))[0,1,2,4,7];
        $abuser_cnt{$user}++ if /$datestr/;
        }
        }

        foreach $user (sort keys %abuser_cnt) {
        print "$user\n";
        }


        Best of luck,
        +Dave


        --- On Wed, 4/30/08, perl_beg <perl_beg@...> wrote:

        > From: perl_beg <perl_beg@...>
        > Subject: [PBML] Re: Use split
        > To: perl-beginner@yahoogroups.com
        > Date: Wednesday, April 30, 2008, 1:17 PM
        > Hi David, here is my piece of code. I got the first part
        > working
        > that you were helping me with originally much thanks.
        >
        > I tried to include the next part of the code to capture the
        > date in
        > the logfile. For example - "Tue Apr 20 2008" but
        > when I compare what
        > is in the file ($datestr =~ @array) of course it's not
        > working
        > properly. Any suggestions would be great.
        >
        >
        > my $datestr = UnixDate("today","%a %b %e
        > %Y");
        >
        >
        > %abuser_cnt = ();
        >
        > my $file="/var/log/app.log";
        >
        > open (INFILE, "<$file") || die "Cannot
        > open file";
        > while (<INFILE>) {
        > chomp;
        > if ( $_ =~ m/-j\s?[0-9]?/){
        > my @array = ($day, $mth, $daynum, $year) =
        > (split(/
        > \s+/,$_))[0,1,2,4];
        > if ($datestr =~ @array ) {
        > my ($day, $mth, $daynum, $year, $user) =
        > (split(/\s+/,$_))
        > [0,1,2,4,7];
        > $abuser_cnt{$day,$mth,$daynum,$year,$user}++;
        > }
        > }
        > }
        >
        > @abusers = sort keys %abuser_cnt;
        >
        > foreach $user (@abusers) {
        > print "$user\n";
        > }
        >
        >
        > ------------------------------------
        >
        > Unsubscribing info is here:
        > http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo!
        > Groups Links
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.