in reply to Extracting selected fields form file record
ehm ...
> > spaces before and after
the spaces before are IMHO best dealt by stripping them before splitting. °
$line =~ s/^\s+//;
Even your pseudo python code can't do this in a one-liner with split (IMHO).
But more importantly your definition of "field" is fuzzy now.
Please clarify
- how do you allow empty fields?
- are all whitespace characters as separator allowed (like tab...)?
- are multiple whitespace characters as separator allowed?
update
provided there are no "empty fields" and "multiple whitespaces" are allowed as separators:
You can use a regex like /(\S+)/g ( \S is non-whitespace, the opposite of \s)
Debugger demo:
DB<35> $line = " a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 \n" DB<36> x ($line =~ /(\S+)/g)[0,1,3,5,7,9] 0 'a0' 1 'a1' 2 'a3' 3 'a5' 4 'a7' 5 'a9' DB<37>
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
update
°) or by using the magic of ' ' soonix showed us here.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Extracting selected fields form file record (updated)
by rsFalse (Chaplain) on Feb 07, 2022 at 12:27 UTC | |
Re^2: Extracting selected fields form file record (updated)
by Anonymous Monk on Feb 07, 2022 at 12:32 UTC | |
by eyepopslikeamosquito (Archbishop) on Feb 08, 2022 at 11:40 UTC | |
by haukex (Archbishop) on Feb 08, 2022 at 12:25 UTC | |
by choroba (Cardinal) on Feb 08, 2022 at 13:07 UTC |
In Section
Seekers of Perl Wisdom