Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day MrSnrub,

I see you have a lot of responses already. Here's my take on this.

You can trim the data by capturing everything that isn't leading or trailing whitespace with this regular expression:


You can use that directly inside a map like this:

say for map { (/\A\s*(.*?)\s*\z/) } split /\|/, $str;

You can use it inside a subroutine called from map like this:

say for map { clean($_) } split /\|/, $str; sub clean { ($_[0] =~ /\A\s*(.*?)\s*\z/)[0] }

It's good that you've tried a variety of cases with whitespace in different positions in your test input $str. What you don't have is leading whitespace at the start of $str or trailing whitespace (following non-whitespace) at the end of $str.

Here's my tests using both of the options I presented with some additional test data.

$ perl -Mstrict -Mwarnings -E ' my $str = " \t item0 with leading whitespace |item1 | item2| item3 | extra item with embedded whitespace \t \n \n\t |item4| | itemN with trailing whitespace \t "; say "---- WITHOUT SUBROUTINE ---"; say ">$_<" for map { (/\A\s*(.*?)\s*\z/) } split /\|/, $str; say "---- USING SUBROUTINE ---"; say "<$_>" for map { clean($_) } split /\|/, $str; sub clean { ($_[0] =~ /\A\s*(.*?)\s*\z/)[0] } ' ---- WITHOUT SUBROUTINE --- >item0 with leading whitespace< >item1< >item2< >item3< >extra item with embedded whitespace< >item4< >< >itemN with trailing whitespace< ---- USING SUBROUTINE --- <item0 with leading whitespace> <item1> <item2> <item3> <extra item with embedded whitespace> <item4> <> <itemN with trailing whitespace>

-- Ken

In reply to Re: How to call a function on each item after a split? by kcott
in thread How to call a function on each item after a split? by MrSnrub

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [choroba]: Good morning!
    marto waves

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2018-05-24 09:17 GMT
    Find Nodes?
      Voting Booth?