Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Extracting selected fields form file record (updated) by LanX
in thread Extracting selected fields form file record by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-11-30 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?