Here is a rather verbose solution:
#!/usr/bin/perl -w use strict; use Data::Dumper; print "Enter the part you which to search for: "; my $part = <STDIN>; chomp($part); my $searchresult = &search(part => $part); if (defined $searchresult) { print "Located part No. $part: $$searchresult[1]\n"; } else { print "Your part ($part) could not be found...\n"; } # This routine will accept a part number as an anonymous # hash, and seach thru' a text file returning the entire # record (pipe delimited) of the 1st occurrence sub search { my %args = @_; my $retval; local*FH; open (FH, './parts.txt') || die "Cannot open file: ($!)"; my @parts = <FH>; foreach my $line (@parts) { my @fields = split(/\|/, $line); if ($args{part} eq $fields[0]) { $retval = \@fields; last; } } close FH; return $retval; }

There are a number of problems with this kind of design (including, but not limited to:)

  1. Enforcement of primary key integrity (there is none)
  2. Locking of the file during updates
  3. Scalability of the file
IMHO, this would be considered a one off solution, not really production quality. One step toward production quality would be to stuff it all into a database!

Putting this kind of information in a database is quite useful as the data can but cut up in any particular way, normalised (part number, description, who ordered the part), the data can be backed up using standard database backup methods, it can be rolledback (if your DB engine supports it) etc etc etc.


In reply to Re: pulling information from a delineated text file using a subroutine by Ryszard
in thread pulling information from a delineated text file using a subroutine by Bismark

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.