I am trying to search a text file by reading the text file into my script, then tokenise the file, then search the token for $search. For reference there are 7 elements in the @database array.
sub search { # Load the flatfile into a data structure. open(DB, $datafile); {local $/; $_ = <DB>} close(DB); # Start printing template print $templatehead; # Get ready for interetting datafile $recnum = 0; RECORD: while (m/\G(.*?(?<!\\)(\\\\)*)\Q$dataseperator\E/gs) { # Get the next field and unescape it. $data = $1; $data =~ m/^\n?(.*?)\n?\\?$/s; $data = $1; $data =~ s@\\\\@\\@; push @data, $1; if ($#data >= $#database) { # Get what we are searching for.... the reason we are here. $search = &getparam("search"); #gets a CGI::param() foreach $field (@data) { if(-1 != index($field,$search)) { &printfield(@data); @data = (); $recnum++; next RECORD; } } } } print $templatetail; exit(); }
An example of the data in $datafile is,
Test|Subject|test@subject.com|Male|Perl-Database|good|on| My|Tester|my@tester.com|Female|Perl-Database|very good|on| Blank|Worker|blank@worker.com|Male|MySql-Database|the best!|off| Someone|Somewhere|someone@somewhere.com|Female|Mysql-Database|great|of +f| Adding|Test|add@test.com|Male|Perl-Database|Great product!|on|

In reply to Searching a text file by monoxide

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.