Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: pulling information from a delineated text file using a subroutine

by Ryszard (Priest)
on Jan 16, 2003 at 07:14 UTC ( [id://227344]=note: print w/replies, xml ) Need Help??


in reply to pulling information from a delineated text file using a subroutine

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.

  • Comment on Re: pulling information from a delineated text file using a subroutine
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://227344]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found