Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Turning a questionable string into a number

by bradcathey (Prior)
on Jun 29, 2017 at 17:43 UTC ( [id://1193851]=perlquestion: print w/replies, xml ) Need Help??

bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

I'm reading in a tab delimited text file exported from Excel. The 12th "position" contains a simple 3 or 4 digit number. For some reason that number is coming in as a string, or something I'm not familiar with, and unusable as a number. This is from Dumper:

$VAR1 = [ '1130',

My database query was returning zip so I tested for the veracity of the uniq_id in line 78. The error is:

Argument "\01\01\03\00\0" isn't numeric in multiplication (*) at ./map +tobuildforums.pl line 78.

I did a hex dump and the number looks fine:

1130 = 31 31 33 30

Here's the code:

... use List::MoreUtils qw(uniq); open($fh, '<', 'filetoparse.txt') or die "error $!"; chomp(my @lines = <$fh>); close $fh; my @old_ids; foreach my $line (@lines) { @data = split(/\t/, $line); push @old_ids, $data[12]; } my @uniq_ids = uniq (@old_ids); #get unique ids #step through unique ids for new id using old ids for my $i (0..$#uniq_ids) { print $uniq_ids[$i] * 1; #line 78 of my code to test for a number my $stmt = "SELECT * FROM temp_topics WHERE old_id = ?"; my $topic = $dbh->selectrow_hashref($stmt, undef, $uniq_id[$i]); etc. }

Thoughts on how to handle this?

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Turning a questionable string into a number
by choroba (Cardinal) on Jun 29, 2017 at 19:15 UTC
    Turn on strict.

    my @uniq_ids = uniq (@old_ids); # un +iq_ids # ~~~~~~~~ my $topic = $dbh->selectrow_hashref($stmt, undef, $uniq_id[$i]); # un +iq_id # ~~~~~~~

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Turning a questionable string into a number
by tybalt89 (Monsignor) on Jun 29, 2017 at 19:12 UTC

    "\01\01\03\00\0" is certainly not a number, it's a bottom nibble masked, NULL-terminated string.

Re: Turning a questionable string into a number
by haukex (Archbishop) on Jun 29, 2017 at 19:23 UTC

    Based on what you've shown us that is quite strange. Are you sure you're grabbing the correct field with $data[12], and have you looked at the hex dump of the entire line? Could you provide us with enough data to reproduce this, like an entire line of input from your file? Have you by any chance reduced the code that you're showing us a little too much, like perhaps you use the ^, |, or & operators somewhere (the latter two maybe accidentally instead of || resp. &&)? See also Short, Self-Contained, Correct Example. Update: Oh yes, and please turn on $Data::Dumper::Useqq=1; or use Data::Dump instead.

      Good questions, but the only thing I haven't given you is a self-contained example that you could run. Apologies. However, I just resaved the file out as unicode 8-bit and everything worked just fine. So, problem solved. A big thanks to all who took the time to response.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I just resaved the file out as unicode 8-bit and everything worked just fine.

        Ah yes, Windows does like UTF-16...

        use warnings; use strict; use Encode 'encode'; my $in = "1130\t"; my $str = encode('UTF-16', $in, Encode::FB_CROAK); $str=~s/^\xFE\xFF|\t//g; # remove BOM & tab print $str*1, "\n"; __END__ Argument "\01\01\03\00\0" isn't numeric in multiplication (*) at - lin +e 7. 0

        An alternative for you would also be open($fh, '<:raw:encoding(UTF-16)', 'filetoparse.txt') ... (Updated to add :raw as per choroba's reply, thanks!)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1193851]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-16 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found