Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching 1

Status
Not open for further replies.

merc58

Technical User
Aug 18, 2008
4
I have an array @field = ("A H","2 H","8 S","K S",...,"Q S");
I print the top 5 elements thus -

for $k (0..4) {
print $field[$k];
print "\n";

then for each of the first 5 element, do a pattern match.
If the first element is "K S", match "S" with SAM, for "A H"
match "H" with HENRY .. and so on always match the second character and print output : example "A H" = HENRY, "Q S" = SAM ...

 
maybe you want something the likes of

Code:
#!/usr/bin/perl

my @field = ("H S", "A B", "B A");
my %map = ('S' => 'Sam', 'B' => 'Bernhard');

foreach my $i (0..2) {
  foreach my $key (keys %map) {
    if ($field[$i] =~ /$key$/) {
      print $map{$key},"\n";
    }
  }
}

prints
Code:
Sam
Bernhard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top