balt has asked for the wisdom of the Perl Monks concerning the following question:
This prints the expected result:#!/usr/bin/perl -w use strict; my $string = "firstfield second field KNOWNWORD alwaysOneWord then one + or more make the last field"; my $field1 = ( split / /, $string)[0]; my $field2 = substr($string, length("$field1 "), index($string, "KNOWN +WORD") - length("$field1 ")); my @temp = split / /, substr($string, index($string, "KNOWNWORD")+len +gth("KNOWNWORD")); my $field3 = $temp[1]; my $field4 = join ' ', @temp[2..$#temp]; print "field1: $field1\n"; print "field2: $field2\n"; print "field3: $field3\n"; print "field4: $field4\n";
field1: firstfield field2: second field field3: alwaysOneWord field4: then one or more make the last field
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help perlifying this string parse-o-rama
by hdb (Monsignor) on Oct 22, 2015 at 12:48 UTC | |
|
Re: Help perlifying this string parse-o-rama
by choroba (Cardinal) on Oct 22, 2015 at 12:27 UTC | |
|
Re: Help perlifying this string parse-o-rama
by BrowserUk (Patriarch) on Oct 22, 2015 at 12:26 UTC |