For partial matches, an alternative approach would be to treat the text file as a database table using DBD::CSV and query with the SQL 'LIKE' operator.

#!perl use strict; use DBI; # set up database my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { csv_tables => { customer => { f_file => "customers.txt" , col_names => ['NAME','TELNO','EMAIL'], } }, csv_sep_char => " ", }) or die $DBI::errstr; print "Type 'q' to exit\n"; my @field = ('Name','TelNo','Email'); my $i=0; my $input; while (1) { do { # get input $i = 0 if $field[$i] eq ''; print $field[$i]."? "; chomp($input = <STDIN>); ++$i; } until ($input) ; --$i; last if (lc $input eq 'q'); # query database my $fld = $field[$i]; print "Search for $fld contains '$input'\n"; my $sth = $dbh->prepare("SELECT * FROM customer WHERE $fld CLIKE ?"); # ignore case $sth->execute('%'.$input.'%'); my $count; while (my @f = $sth->fetchrow_array){ ++$count; print "$count $fld : $f[$i] -> $f[0] $f[1] $f[2]\n"; } print "Customer record not found. \n" unless ($count); $input=''; }
poj

In reply to Re: The Art of Hashing by poj
in thread The Art of Hashing by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.