in reply to Case insensitive string comparison
Your code seems ok to me:
What am I doing differently from what you're doing?c:\@Work\Perl\monks>perl -wMstrict -le "my @strings = ( 'SMS,SMS1,20190811', 'SMS,SMSh,20190811', 'SMS,SMSH,20190811', 'SMS,SMSx,20190811', 'SMS,SMSi,20190811', 'SMS,SMSX,20190811', 'SMS,SMSI,20190811', ); ;; for my $s (@strings) { my $ref_s = \$s; print qq{'$$ref_s' matches} if $$ref_s =~ /SMSi/i || $$ref_s =~ /SMSI/i || $$ref_s =~ /SMSh/i || $$ref_s =~ /SMSH/i || $$ref_s =~ /SMS1/ ; } " 'SMS,SMS1,20190811' matches 'SMS,SMSh,20190811' matches 'SMS,SMSH,20190811' matches 'SMS,SMSi,20190811' matches 'SMS,SMSI,20190811' matches
Update 1: Here's a variation showing assignment via reference (and aliasing). Again, I think it works the way I think you think it should work. (Maybe take a look at Short, Self-Contained, Correct Example.)
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @strings = ( 'SMS,SMS1,20190811', 'SMS,SMSh,20190811', 'SMS,SMSH,20190811', 'SMS,SMSx,20190811', 'SMS,SMSi,20190811', 'SMS,SMSX,20190811', 'SMS,SMSI,20190811', ); ;; for my $s (@strings) { my $ref_s = \$s; $$ref_s = 'SMSblk' if $$ref_s =~ /SMSi/i || $$ref_s =~ /SMSI/i || $$ref_s =~ /SMSh/i || $$ref_s =~ /SMSH/i || $$ref_s =~ /SMS1/ ; } ;; dd \@strings; " [ "SMSblk", "SMSblk", "SMSblk", "SMS,SMSx,20190811", "SMSblk", "SMS,SMSX,20190811", "SMSblk", ]
Update 2: BTW: I'd tend to write something like this a bit differently:
And maybe also throw in some kind of boundary assertion like \b so the final regex might look likec:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @strings = ( 'SMS,SMS1,20190811', 'SMS,SMSh,20190811', 'SMS,SMSH,20190811', 'SMS,SMSx,20190811', 'SMS,SMSi,20190811', 'SMS,SMSX,20190811', 'SMS,SMSI,20190811', ); ;; for my $s (@strings) { my $ref_s = \$s; $$ref_s .= ' is SMSblk' if $$ref_s =~ /SMS[iIhH1]/; } ;; dd \@strings; " [ "SMS,SMS1,20190811 is SMSblk", "SMS,SMSh,20190811 is SMSblk", "SMS,SMSH,20190811 is SMSblk", "SMS,SMSx,20190811", "SMS,SMSi,20190811 is SMSblk", "SMS,SMSX,20190811", "SMS,SMSI,20190811 is SMSblk", ]
/ \b SMS[iIhH1] \b /x
to prevent a string like 'SMS,xSMSHx,20190811' from matching.
Give a man a fish: <%-{-{-{-<
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Case insensitive string comparison (updated x2)
by Marshall (Canon) on Jun 28, 2020 at 01:40 UTC | |
by AnomalousMonk (Archbishop) on Jun 28, 2020 at 03:16 UTC | |
by Marshall (Canon) on Jun 29, 2020 at 08:23 UTC |
In Section
Seekers of Perl Wisdom