Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing string information...

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
0
0
US
Hi,

I have the following strings and I need to parse the information into variables like month, date, time, node, process, time_to_insert(1391/1324 from below strings).

Sep 7 00:05:01 a3 asdf: 1 p1 --> Time_To_Insert=1391
Sep 14 00:05:01 a3 asdf: 1 p1 --> add=1324

I used the split function but it does not work when the day is between 1 and 10. I am wondering if there is an efficient way of parsing the whole information using only one statement in perl.

Thanks

 
I also did it using substr() function. Is there any other efficient way?
 
Regex?
Code:
#!perl
use strict;
use warnings;

my $str = 'Sep  7 00:05:01 a3 asdf: 1 p1 --> Time_To_Insert=1391';
my %h;
my @keys = qw(month day_of_month time process time_to_insert);
@h{@keys} = 
    $str =~ /^(\w+)\s+(\d+)\s+(\d+:\d+:\d+).*\s+(\S+)\s+-->\s+.*=(\d+)$/;
print "$_ => $h{$_}\n" for @keys;

[b]Output:[/b]
month => Sep
day_of_month => 7
time => 00:05:01
process => p1
time_to_insert => 1391
I'm not sure what in your string represents 'node', so I didn't include that.

You say, "I used the split function but it does not work when the day is between 1 and 10." I suspect you were splitting on a single space instead of one or more spaces. If you do the latter, this should work fine. E.g.,
Code:
#!perl
use strict;
use warnings;

my $str = 'Sep  7 00:05:01 a3 asdf: 1 p1 --> Time_To_Insert=1391';

my @temp = split([b]/\s+/[/b], $str);
my ($month, $day_of_month, $time, $process, $time_to_insert) = @temp[0,1,2,6,8];
$time_to_insert =~ s/^.*=//;
print "\$month: $month
\$day_of_month: $day_of_month
\$time: $time
\$process: $process
\$time_to_insert: $time_to_insert\n";

[b]Output:[/b]
$month: Sep
$day_of_month: 7
$time: 00:05:01
$process: p1
$time_to_insert: 1391




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top