Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Trouble matching string with decimals

by deshdaaz (Novice)
on Jul 22, 2013 at 19:37 UTC ( #1045705=perlquestion: print w/ replies, xml ) Need Help??
deshdaaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, This is simple(and also very embarrassing) question. Below is my is sample array @voltages.
0.940V 0.940V 0.940V 0.940V 0.740V 0.740V 0.740V 0.740V
I am just trying to compare the variables that match 0.940V data and process. None of my comparison/match strings below are working. Not sure why. Thanks for reading.
for my $count (0..100) { if( $voltages[$count] == "0.940V" ) { # Did not work if( $voltages[$count] =~ /^0\.9[0-4]+V$/ ) { # Did not work if( $voltages[$count] =~ /94/ ) { # Did not work if( $voltages[$count] =~ /^0.*V$/ ) { #Did not work if( $voltages[$count] =~ /V$/ ) { # Even this did no +t work Do something }

Comment on Trouble matching string with decimals
Select or Download Code
Re: Trouble matching string with decimals
by rjt (Deacon) on Jul 22, 2013 at 19:40 UTC
    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.

      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";
Re: Trouble matching string with decimals
by BrowserUk (Pope) on Jul 22, 2013 at 19:41 UTC

    Did you read your array in from a file? Did you forget to chomp the newlines?

    Try adding this before your loop:

    chomp @voltages; for my $count ( ... ) { ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Trouble matching string with decimals
by choroba (Abbot) on Jul 22, 2013 at 19:48 UTC
    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:

    Try printing what really is in the array:

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

    Update: Added the \r\n strings.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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.

Re: Trouble matching string with decimals
by Anonymous Monk on Jul 22, 2013 at 20:08 UTC

    This seems to work allright.

    if( $voltages[$count] =~ /94/ );
Re: Trouble matching string with decimals
by Laurent_R (Parson) on Jul 22, 2013 at 20:55 UTC

    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).

      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: perlquestion [id://1045705]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (13)
As of 2014-09-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (143 votes), past polls