c4onastick has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Splitting on tabs then removing extra white space with map
by GrandFather (Saint) on Sep 18, 2007 at 02:24 UTC | |
Re: Splitting on tabs then removing extra white space with map
by ikegami (Patriarch) on Sep 18, 2007 at 04:16 UTC | |
by c4onastick (Friar) on Sep 18, 2007 at 06:01 UTC | |
by ikegami (Patriarch) on Sep 18, 2007 at 12:50 UTC | |
Re: Splitting on tabs then removing extra white space with map
by mhearse (Chaplain) on Sep 18, 2007 at 02:29 UTC | |
Re: Splitting on tabs then removing extra white space with map
by jwkrahn (Abbot) on Sep 18, 2007 at 03:04 UTC | |
Re: Splitting on tabs then removing extra white space with map
by djp (Hermit) on Sep 18, 2007 at 03:39 UTC | |
Re: Splitting on tabs then removing extra white space with map
by bruceb3 (Pilgrim) on Sep 18, 2007 at 06:08 UTC |