Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Trouble matching string with decimals

by Laurent_R (Canon)
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).

Replies are listed 'Best First'.
Re^2: Trouble matching string with decimals
by deshdaaz (Novice) on Jul 22, 2013 at 23:55 UTC
    Hi, Yes, I exactly see
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045722]
and the questions are moot...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-05-23 09:38 GMT
Find Nodes?
    Voting Booth?