On 2012-12-28 21:32, twle...@reagan.com wrote:
I hope this is a simple fix.  I want to check the beginning characters of items in a 
hash, and compare that to a scalar variable.  I do not need for the entire value to 
match; just the first couple of characters.  Here is a simple example of what I want, but 
it does not work.  Both "if" statements produce a match.  I have read several 
web pages, but I have not found one that has exactly what I need.  Thank you for any help 
on this.
Tim


my $prefix_search_list = '03S,04S';
my @prefix_array = split /\,/,$prefix_search_list;
my %prefix_hash = map {$_ => 1 } @prefix_array;

#compare 05S to 03S and 04S
my $input_field = "05S885858"; #should not match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }
else { print "$input_field is not found\n"; }

#compare 03S to 03S and 04S
$input_field = "03S84844"; #should match
if ( $input_field =~ /$prefix_hash/ ) { print "$input_field is found in 
hash\n"; }
else { print "$input_field is not found\n"; }

#!/usr/bin/perl -wl
use strict;

my @prefixes = split /,/, '03S,04S';

my @inputs = qw( 05S885858  03S84844 );

for my $input ( @inputs ) {
  for my $prefix ( @prefixes ) {
    next if index( $input, $prefix );
    print $input, " starts with ", $prefix;
  }
}
__END__

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to