Greetings Monks,

I'm parsing a file that's tab-delimited, except it has a bunch of extra white space around the values that I'd like to remove. I can't just use split on white space, because there's mixed values (some floats, that I'd like to remove the white space from, and some strings that can have white space).

My first attempt was this:

#!/bin/perl use warnings; use strict; use Data::Dump qw(pp); while(<DATA>){ my @points = map{ s/\s+// } split("\t", $_); print "\n\@points =\n", pp \@points; #More code here } __DATA__ 0.000 12 0.232 13 11 text that c +an have space 1.000 13 0.534 14 12 More text t +hat would be ok 2.000 14 0.876 15 13 yet more te +xt

But I get this:

@points = [1] @points = [1] @points = [1]

Which definitely makes me think I have a context error going. What's the appropriate way to do this? (I know that the s/\s+// will remove the white space in the text too, I'm ok with that for now, I'd like to remove it from around the floats first.)

Effective use of map has always been my Everest, and with your help I will summit it this time!


Thanks in advance for your wisdom!


In reply to Splitting on tabs then removing extra white space with map by c4onastick

Title:
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, details, 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, summary, 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.