Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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.

Replies are listed 'Best First'.
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?
[Corion]: Meh. I think I have now the parts down that I want from a simple Javascript frontend for single-page- applications with a aPerl backend. But it seems that all Javascript templating modules either don't support two-way binding (which would be nice) or ...
[Corion]: ... don't support server-side precompilation (which means the client has to compile all templates to Javascript themselves), or are giant frameworks that expect to do everything (which is not what I want)
[Corion]: I feel that there is a talk somewhere in there, either about the structure and parts, or how I used several parts to get a complete whole.
[Corion]: Maybe I can still find something that is compatible with handlebars.js (which has JS precompilation) but allows for two-way data binding (which is great for the UI)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-03-30 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (355 votes). Check out past polls.