Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Trouble matching string with decimals

by rjt (Deacon)
on Jul 22, 2013 at 19:40 UTC ( #1045707=note: print w/ replies, xml ) Need Help??


in reply to Trouble matching string with decimals

if( $voltages[$count] == "0.940V" )           {  #  Did not work

Are you reading @voltages from a file, by any chance? If so, you probably have newlines at the end of each string. Use chomp to get rid of them.

It's also helpful to print out the value to make sure it is what you think it is:

print "<$voltages[$count]>\n";

If you have trailing space or newlines or anything, it will become quite obvious.

Update: A couple of additional thoughts

Instead of ==, use eq, to do a string comparison, even though Perl will numify both arguments.

Do you really just need to find all @voltages that match, exactly, "0.940V"? If so, the simple eq will suffice. If not, the regexp /^(\d+\.\d+)V$/ will match any non-negative floating point voltage, with the number (but not the "V") in $1 if it matched.

Finally, going through the array by number looks a little suspect to me in this case; can you tell us more about what your "processing" entails? There may be a more elegant solution to what you're trying to achieve.


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 20:33 UTC
    Hi, Yes that @voltages is built by reading from a file. I did try printing it out and it prints exactly what I have in my first post.(with each on new line without me adding \n in my print statement) I had also tried the chomp before posting it here. If I do chomp as below nothing is printed. (chomping it all?)
    chomp $voltages[$count]; print "<$voltages[$count]>\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-09-30 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (372 votes), past polls