CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to pattern match and fish out a entry from a hash

 



cosmokramer
Novice

Jun 29, 2012, 11:07 AM

Post #1 of 3 (1558 views)
how to pattern match and fish out a entry from a hash Can't Post

Hope someone can help me with this.
I have a file which has a list of lines. I want to see if those lines exist in a hash. The lines in the file may have wildcards to match multiple entries in the hash.
Assume that $var1 is a line from my file. I want the last line in this code to print out a YES
I do know that $var1 is not ready(after the substitutions) for checking if it exists in the hash with this code below.I just want to know how to do it.


Code
my %data = (); 
$data{b_slash_car_bit_slash_tebla_bit} = b/car[10]/tebla[9]
$data{b_slash_car_bit_slash_tesla_bit } = b/car[10]/tesla[0] b/car[9].tesla[1];

my $var1 = "b/car[*]/te*a";
$var1 =~ s/\//_slash_/g;
$var1 =~ s/\[\*\]/\_bit_/g;

if (defined $data{$var1}) {print "YES";} else {print "No";}



BillKSmith
Veteran

Jul 2, 2012, 12:07 PM

Post #2 of 3 (1362 views)
Re: [cosmokramer] how to pattern match and fish out a entry from a hash [In reply to] Can't Post

I can accomplish your objective in a different way. Convert your $var into a regular expression and then use it to grep the keys of %data.


Code
   

use strict;
use warnings;
my %data = (
b_slash_car_bit_slash_tebla_bit => 'b/car[10]/tebla[9]',
b_slash_car_bit_slash_tesla_bit => 'b/car[10]/tesla[0] b/car[9].tesla[1]',
);

my $var = "b/car[*]/te*a";
$var =~ s/\//_slash_/g;
$var =~ s/(?<! \[ ) \* (?! \])/.*/xg;
$var =~ s/ \[ [*] \] /.*/xg;

print +(grep {/$var/} keys %data) ? 'Yes' : 'No', "\n";

Good Luck,
Bill

(This post was edited by BillKSmith on Jul 2, 2012, 12:10 PM)


cosmokramer
Novice

Jul 3, 2012, 10:11 AM

Post #3 of 3 (1317 views)
Re: [BillKSmith] how to pattern match and fish out a entry from a hash [In reply to] Can't Post

Thanks Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives