Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: This looks like whitespace in my CSV but doesn't seem to be

by Anonymous Monk
on Sep 30, 2012 at 08:09 UTC ( [id://996474]=note: print w/replies, xml ) Need Help??


in reply to This looks like whitespace in my CSV but doesn't seem to be

You've got blurry vision
#!/usr/bin/perl -- use strict; use warnings; use Text::CSV; use Data::Dump; open my $io, '<', \q{ "My Company","Gavin","Henry ","ghenry@ghenry.co.uk"," 1.00" }; my $csv = Text::CSV->new( { binary => 1, eol => $/ } ); while ( my $row = $csv->getline($io) ) { my @fields = @$row; next unless $fields[4]; dd \@fields; $fields[4] =~ s/\s+//g; print $fields[4], "\n"; dd \@fields; } __END__ ["My Company", "Gavin", "Henry ", "ghenry\@ghenry.co.uk", " 1.00"] 1.00 ["My Company", "Gavin", "Henry ", "ghenry\@ghenry.co.uk", "1.00"]

Replies are listed 'Best First'.
Re^2: This looks like whitespace in my CSV but doesn't seem to be
by ghenry (Vicar) on Sep 30, 2012 at 08:27 UTC

    That gives me:

    [ "My Company", "Gavin", "Henry\xC2\xA0", "ghenry\@ghenry.co.uk", "\xC2\xA1.00", ]

    which isn't whitespace after all. Thanks for the Data::Dump tip. I've always used Data::Dumper. Swapping for that produces:

    $VAR1 = [ 'My Company', 'Gavin', 'Henry ', 'ghenry@ghenry.co.uk', ' 1.00' ]; $VAR1 = [ 'My Company', 'Gavin', 'Henry ', 'ghenry@ghenry.co.uk', ' 1.00' ];

    i.e.

    next unless $fields[4]; print Dumper \@fields; $fields[4] =~ s/\s+//g; print Dumper \@fields;
    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!

      \xC2\xA0 is a Unicode non-breaking space encoded into UTF-8. If you decode your UTF-8 string into a native Perl Unicode string (see Encode), and then add the /u modifier to your regular expression, that ought to enable \s to remove non-ASCII whitespace.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        That will be why Vim isn't allowing me either to do :%s/\s//g in that field.

        Excellent catch!

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!

        I need to get a newer perl as I'm on 5.10.1 and /u throws an error.

        Cheers though, will look how I can do this.

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!
      I don't work with multi-byte characters or UTF-8 files. But Perl has an amazing ability to deal with these things.

      From the open() docs, it could be that all you need to do is open the file in the right encoding mode and that could enable even Perl 5.10.X to work. I don't know for sure, but this idea is worth a try.

      open(my $fh, "<:encoding(UTF-8)", "filename") || die "can't open UTF-8 encoded filename: $!";

        Of course Marshall!!!

        That was it, all solved and s/^\s// works as I'd expect. Thanks so much. Very basic of me not to do.

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!

      Or is that hex for whitespace? Will check...

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://996474]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found