Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Trouble matching string with decimals

by choroba (Canon)
on Jul 22, 2013 at 19:48 UTC ( #1045709=note: print w/ replies, xml ) Need Help??


in reply to Trouble matching string with decimals

Strange. Works for me:

#!/usr/bin/perl use warnings; use strict; use feature qw(say); my @voltages = ('0.940V', "0.940V\n", "0.940V\r\n", '0.740V', "0.740V\n", "0.740V\r\n"); for my $voltage (@voltages) { say "Yes 1" if $voltage == "0.940V"; say "Yes 2" if $voltage =~ /^0\.9[0-4]+V$/; say "Yes 3" if $voltage =~ /94/; say "Yes 4" if $voltage =~ /^0.*V$/; say "Yes 5" if $voltage =~ /V$/; }

Note that == is for numbers, so you can change 1 to $voltage == 0.94.

Output:

Argument "0.940V" isn't numeric in numeric eq (==) at ./1.pl line 16. Argument "0.940V" isn't numeric in numeric eq (==) at ./1.pl line 16. Yes 1 Yes 2 Yes 3 Yes 4 Yes 5 Argument "0.940V\n" isn't numeric in numeric eq (==) at ./1.pl line 16 +. Yes 1 Yes 2 Yes 3 Yes 4 Yes 5 Argument "0.940V\r\n" isn't numeric in numeric eq (==) at ./1.pl line +16. Yes 1 Yes 3 Argument "0.740V" isn't numeric in numeric eq (==) at ./1.pl line 16. Yes 4 Yes 5 Argument "0.740V\n" isn't numeric in numeric eq (==) at ./1.pl line 16 +. Yes 4 Yes 5 Argument "0.740V\r\n" isn't numeric in numeric eq (==) at ./1.pl line +16.

Try printing what really is in the array:

use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper \@voltages;

Update: Added the \r\n strings.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Trouble matching string with decimals
Select or Download Code
Replies are listed 'Best First'.
Re^2: Trouble matching string with decimals
by deshdaaz (Novice) on Jul 22, 2013 at 20:47 UTC
    Hi, When I used data Dumper, I get below. Not sure why I see these empty vars. When I print them with regular print statement in loop, they just come out as in my first post.
    $VAR1 = []; $VAR1 = [ "0.940V\r\n" ]; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = [ "0.940V\r\n" ]; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = []; $VAR1 = [];
      Yes, printing nothing (or @{ [] } ) does not print anything. Your array is sparse.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      You did not show enough code for us to figure out what's going on, but the data dumper output, with all these empty values, seems to indicate that you have something wrong in the way you populate your data structure in the first place.

      Please post the whole code or, if it is large, the smallest subset of your code that demonstrates the problem. As well as your input file if your data is coming from a file.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2015-07-28 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (253 votes), past polls