#!/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:
Argument "0.940V" isn't numeric in numeric eq (==) at ./1.pl line 16. Argument "0.940V" isn't numeric in numeric eq (==) at ./1.pl line 16. Yes 1 Yes 2 Yes 3 Yes 4 Yes 5 Argument "0.940V\n" isn't numeric in numeric eq (==) at ./1.pl line 16 +. Yes 1 Yes 2 Yes 3 Yes 4 Yes 5 Argument "0.940V\r\n" isn't numeric in numeric eq (==) at ./1.pl line +16. Yes 1 Yes 3 Argument "0.740V" isn't numeric in numeric eq (==) at ./1.pl line 16. Yes 4 Yes 5 Argument "0.740V\n" isn't numeric in numeric eq (==) at ./1.pl line 16 +. Yes 4 Yes 5 Argument "0.740V\r\n" isn't numeric in numeric eq (==) at ./1.pl line +16.
Try printing what really is in the array:
use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper \@voltages;
Update: Added the \r\n strings.
In reply to Re: Trouble matching string with decimals
by choroba
in thread Trouble matching string with decimals
by deshdaaz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |