Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Trouble matching string with decimals

by Laurent_R (Parson)
on Jul 22, 2013 at 20:55 UTC ( #1045722=note: print w/ replies, xml ) Need Help??


in reply to Trouble matching string with decimals

Even if you have a carriage return at the end of the string, several of the expressions should work, as shown in this session under the debugger:

DB<14> $volt = "0.940V\n" DB<15> print "matched\n" if $volt =~ /^0\.9[0-4]+V$/ matched DB<16> print "matched\n" if $volt =~ /94/ matched DB<17> chomp $volt DB<18> print "matched\n" if $volt eq "0.940V"; matched

You should really try with just one value and print the value with special characters that will show if your values contain something more than you think (leading space, trailing space, carriage return, whatever:

print ">$voltages[0]<\n"; # should print : >0.940V<

It might also turn out that you will not print anything, for example because you made a mistake on the array name, so that nothing will ever match because the array your are testing is simply empty, or whatever.

Finally, as already said, you should rather use the eq operator rather than the == operator for strings such as 0.940V (even though == will work by removing the trailing letter, for example 0.940V == 0.940 will return true).


Comment on Re: Trouble matching string with decimals
Select or Download Code
Re^2: Trouble matching string with decimals
by deshdaaz (Novice) on Jul 22, 2013 at 23:55 UTC
    Hi, Yes, I exactly see
    >0.940V<
    printed. Because I am removing \r\n from by grabbed string which is building the @voltages array. But I am still stuck. It's not even printing
    if( $voltages[$count] =~ /V/) {
    :(
      To me this sounds like a mistyped variable name or a misplaced my. Can you post the exact code you use for populating the array and for your compare loop?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2014-12-22 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls