Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You would definitely want more than just the pattern you are searching for, especially when processing a form. I will normally also search for some anchors in the form fields.

Example 1 - With anchor fields
use strict; use warnings; use Data::Dumper; my @forms = ( { name => 'Name: Roger', address => 'Address: Melbourne', phone => 'Telephone: 12345', }, { name => 'Name: Roger', address => 'Address:', phone => 'Telephone: 12345', }, { name => 'Name: Roger', address => 'Address: Melbourne', phone => 'Telephone: 54321', }, ); my $m_name = 'Roger'; my $m_address = ''; # means don't care my $m_phone = '12345'; for my $form (@forms) { print Dumper($form); if ($form->{name} =~ /Name:\s*?$m_name/ && $form->{address} =~ /Address:\s*?$m_address/ && $form->{phone} =~ /Telephone:\s*?$m_phone/) { print "Match\n"; } else { print "Not match\n"; } }
And the output -
$VAR1 = { 'address' => 'Address: Melbourne', 'phone' => 'Telephone: 12345', 'name' => 'Name: Roger' }; Match $VAR1 = { 'address' => 'Address:', 'phone' => 'Telephone: 12345', 'name' => 'Name: Roger' }; Match $VAR1 = { 'address' => 'Address: Melbourne', 'phone' => 'Telephone: 54321', 'name' => 'Name: Roger' }; Not match

Ok, another example without the anchors -

Example 2 - Without anchor fields
use strict; use warnings; use Data::Dumper; my @forms = ( { name => 'Roger', address => 'Melbourne', phone => '12345', }, { name => 'Roger', address => '', phone => '12345', }, { name => 'Roger', address => 'Melbourne', phone => '54321', }, ); my $m_name = 'Roger'; my $m_address = ''; # means don't care my $m_phone = '12345'; for my $form (@forms) { print Dumper($form); my ($t_name, $t_address, $t_phone) = taint_fields($m_name, $m_address, $m_phone); if ($form->{name} =~ /$t_name/ && $form->{address} =~ /$t_address/ && $form->{phone} =~ /$t_phone/) { print "Match\n"; } else { print "Not match\n"; } } sub taint_fields { return map { $_ ||= '.*' } @_; }

In reply to Re: a word of warning about /$pattern/ by Roger
in thread a word of warning about /$pattern/ by dada

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found