Perl - get first "word" from input string
I am trying to write a Perl program that reads in lines from a text file, and, for each line, extract the first "word" from the line, and perform a different action based on the string that gets returned.
The main loop looks like this:
while(<AXM60FILE>) {
$inputline = $_;
($start) = ($inputline =~ /\A(.*?) /);
perform something, based on the value of string in $start
}
The input file is actually a parameter file, with the parameter_name and parameter_value, separated by a colon (":"). There can be spaces or tabs before or after the colon.
So, the file looks (for e开发者_StackOverflow社区xample) like the following:
param1: xxxxxxxxxxxx
param2 :xxxxxxxxxxxxx param3 : xxxxxxxxxxxxxxxxx param4:xxxxxxxxxxxxx
That "($start) = ($inputline =~ /\A(.*?) /);" works ok for the "param2" example and the "param3" example where the 1st word is terminated by a blank/space, but how can I handle the "param1" and "param4" situations, where the parameter_name is followed immediately by the colon?
Also, what about if the "whitespace" is a tab or tabs, instead of blank/space character?
Thanks, Jim
This will cover all of your cases and then some:
my ($key, $value) = split /\s*:\s*/, $inputline, 2;
(Or, in English, split $inputline
into a maximum of two elements separated by any amount of whitespace, a colon and any amount of whitespace.)
($start) = $inputline =~ /\A([^:\s]+)/;
This will match anything except whitespace and :
at the beginning of the line.
Or using split
:
($start) = split /[:\s]+/, $inputline, 2;
精彩评论