Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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 studying the Monastery: (4)
As of 2024-04-24 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found