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