Michael Alipio wrote: > > My Log file looks like this: > Each session contains at most four lines and then it > is separated by newline. I need to grab each session > and put each key/value pair in hash. > > > Start=2006-10-03.09:09:51 > IP=192.168.0.14 > User=alex > End=2006-10-03.09:14:10 > > Start=2006-10-03.09:52:12 > IP=192.168.0.15 > End=2006-10-03.09:53:56 > > Start=2006-10-03.09:55:21 > IP=192.168.0.16 > User=mike > > Start=2006-10-03.09:55:38 > IP=192.168.0.17 > End=2006-10-03.09:56:20 > > > I'm just following an example given to me.. however at > some point, I failed to make it work. > > > > my $logfile = shift @ARGV; > open LOGFILE, $logfile or die $!; > while (<LOGFILE>){ > chomp; > push @data, $_;
You should be using paragraph mode and extracting your data here in the while loop. > } > $logdata = "@data"; > > local $/ = ""; Paragraph mode works by separating each paragraph based on multiple newlines however you have removed all newlines from the data so there is just one large paragraph left. > my $fh = new IO::Scalar \$logdata; > while (my $record = <$fh>) { > my %extr; > for (qw(Start IP User End)){ > $extr{$_} = ''; > $extr{$_} = $1 if $record =~ /$_=(.*)/; > } > print "Start:$extr{Start} IP:$extr{IP} > User:$extr{User} End:$extr{End}"; > } It looks like you want something like: use warnings; use strict; my $logfile = shift; open LOGFILE, '<', $logfile or die "Cannot open '$logfile' $!"; $/ = ''; while ( <LOGFILE> ) { my %extr = ( Start => '', IP => '', User => '', End => '', /^(Start|IP|User|End)=(.+)/mg ); print "Start:$extr{Start} IP:$extr{IP} User:$extr{User} End:$extr{End}"; } __END__ However, if you really want to slurp your entire file into a string and process it like that then something like this may work: use warnings; use strict; use IO::Scalar; my $logfile = shift; open LOGFILE, '<', $logfile or die "Cannot open '$logfile' $!"; my $logdata = do { local $/; <LOGFILE> }; my $fh = new IO::Scalar \$logdata; $/ = ''; while ( <$fh> ) { my %extr = ( Start => '', IP => '', User => '', End => '', /^(Start|IP|User|End)=(.+)/mg ); print "Start:$extr{Start} IP:$extr{IP} User:$extr{User} End:$extr{End}"; } __END__ John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>