Big Thanks !

My log file looks like below

2004-03-26 @ 00:00:01 [EMAIL PROTECTED] -- 10881 10864 hyb01
:INFORMATIONAL
2004-03-26 @ 00:00:01 [EMAIL PROTECTED] -- 10881 10864 hyb01 Function
(up) returned (0) return code. -- seconds -- 1:INFORMATIONAL
2004-03-26 @ 00:00:01 [EMAIL PROTECTED] -- 10878 10862 hyb01
Script/function sql_er_lagonly_init started at Fri Mar 26 00:00:01 CST 2004
-- seconds -- 1:INFORMATIONAL
2004-03-26 @ 00:00:01 [EMAIL PROTECTED] -- 10881 10864 hyb01
Returning return code 0:INFORMATIONAL
2004-03-26 @ 00:00:01 [EMAIL PROTECTED] -- 10881 10864 hyb01
Script/function sql_er_status_final ended at Fri Mar 26 00:00:01 CST 2004 --
seconds -- 1:INFORMATIONAL
2004-03-26 @ 00:00:01 [EMAIL PROTECTED] -- 10878 10862 hyb01
Script/function sql_er_lagonly started at Fri Mar 26 00:00:01 CST 2004 --
seconds -- 1:INFORMATIONAL
Variable_name   Value
Aborted_clients 5592
Aborted_connects        4
Bytes_received  58302821
Bytes_sent      658785263
Com_admin_commands      0
Com_alter_table 27
Com_analyze     0
Com_backup_table        0
Com_begin       35652978
Com_change_db   172
Com_change_master       0
Com_check       0
Com_commit      35653362
Com_create_db   0
Com_create_function     0
.
.
.
200 of these parameters
2004-03-26 @ 00:00:04 [EMAIL PROTECTED] -- 10880 10863 hyb01
DATABASE IS UP:INFORMATIONAL
2004-03-26 @ 00:00:04 [EMAIL PROTECTED] -- 10880 10863 hyb01
Script/function sql_instance_ping_final started at Fri Mar 26 00:00:04 CST
2004 -- seconds -- 4:INFORMATIONAL


Any idea on reseting %datastore will be highly appreciable.

Best Regards,


-----Original Message-----
From: Morbus Iff [mailto:[EMAIL PROTECTED] 
Sent: Monday, March 29, 2004 4:45 PM
To: Sagar, Sanjeev; [EMAIL PROTECTED]
Cc: Sagar, Sanjeev
Subject: Re: File content question



Where are you getting lost? I don't recall your previous messages, so I'm
not sure what "like before" means... Anyway, here's an untested rewrite.
Untested. Mmhm.

The biggest problem is making sure to reset %datastore
when you come across another <bunch of text lines> that
don't match your variable names. You didn't provide enough
data for that instance, so I've considered it irrelevant.

  #!/usr/bin/perl
  use warnings;
  use strict;

  my $infile = "/tmp/test1.log";
  my $outfile = "/tmp/mysqltats.out";
  my %datastore; # where we keep variables.

  open (INFILE, "<$infile")   || die "cannot open $infile: $!\n";
  open (OUTFILE, ">$outfile") || die "cannot open $outfile: $!\n";

  while (<INFILE>) {
    chomp;

    next if /^\*+/; # changed from your example. you were
                    # checking if there was *+ anywhere in
                    # a string, which could potentially match
                    # the value of a variable. ^\&+ is stronger.

    # etc., etc. this sorta syntax is more
    # readable than if/else statements. the same
    # can be done with "next unless [condition]".
    next if /^anotherexample/;

    # key: beginning of line up to first whitespace.
    # value: everything else after the first whitespace.
    my ($key, $value) = /^(.*)\s(.*)$/;

    # you may want to throw in more tests here to make
    # sure that $key is what you expect: ie., letters and
    # underscores only, less than 15 characters, lowercase,
    # etc., etc. pro-actively checking for sanity helps.
    #
    # next if $key !~ /[\w_-]*/;
    # next if length($key) > 15;
    # $key = lc($key);

    # store the key/value into a hash, unless this
    # key has already been seen (ie., the first instance
    # takes precedent. remove the "unless ..." if you'd
    # like the final value of a duplicated key instead.
    $datastore{$key} = $value unless $datastore($key);
  }

  # print out a specific key value.
  print "The value of 'Bob' is $key{"Bob"}\n";

  # or loop through 'em all.
  foreach (keys %datastore) {
     print "Variable: $_ // Value: $datastore{$_}\n";
  }

  close (OUTFILE); close (INFILE);


-- 
Morbus Iff ( i put the demon back in codemonkey )
Culture: http://www.disobey.com/ and http://www.gamegrene.com/ Spidering
Hacks: http://amazon.com/exec/obidos/ASIN/0596005776/disobeycom
icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to