in reply to Invisible characters
One thing that could be done is split on any sequence of whitespace chars or the ",", like this:print "\"$field1\"\t\"$field2\"\n"; output... I took out "" in data input because it was too confusing a printout. "-0.500" " 4.502e-6" "-0.499" " 4.474e-6" "-0.498" " 4.458e-6" "-0.497" " 4.445e-6" "-0.496" " 4.433e-6" "-0.495" " 4.421e-6"
That would handle extra "blank type" chars like \t. It also appears that you have "'s in input that you don't want. One way to get rid of them would be s/"//g; which just deletes them all.my @fields = split /[\s,]+/;
I suspect that you will find some unprintable character as toolic suggests. Of course one way to deal with this is just modify the regex that gets rid of the " char to deal with any characters that we don't want to see: s/[^\w\.\,\-]//g;, that gets rid of anything not contained within this set which would include the ",\n chars also it possible to use "tr" for the same purpose. tr is a simple minded thing and as such it is faster than the s/// type operation. But fixing the input file is better as this "weirdness" can fester and propagate.
Finally, you can use array slice an get rid of the @fields intermediate variable. So some code with various possibilities...Hope some combination of ideas work for you.
while(<DATA>){ chomp(); #optional #s/[^\w\.\,\-]//g; tr /0-9.-e ,//dc; #another possibility.. my ($field1, $field2) = (split /[\s,]+/)[0,1]; print "\"$field1\"\t\"$field2\"\n"; } __DATA__ "-0.500, 4.502e-6" "-0.499, 4.474e-6" "-0.498, 4.458e-6" "-0.497, 4.445e-6" "-0.496, 4.433e-6" "-0.495, 4.421e-6"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Invisible characters
by lomSpace (Scribe) on Feb 24, 2010 at 19:26 UTC | |
by Marshall (Canon) on Feb 25, 2010 at 15:15 UTC |