in reply to Re: using join with a print ref statement
in thread using join with a print ref statement
diotalevi's code start's here:#!/usr/bin/perl -w use strict; use Data::Dumper; use diagnostics -verbose; print "Enter the Part Number you wish to search for: "; my $part = <STDIN>; chomp($part); print "Enter the Revison for ($part): "; my $rev = <STDIN>; chomp($rev); my $searchresult = &search(part => "$part", rev => "$rev"); if (defined $searchresult) { print "Located Part Number $part: @$searchresult[1 .. 7]\n"; }else { print "Your Part Number ($part) Rev ($rev) could not be found...\n +"; } # This routine will accept a part number as an anonymous # hash, and search thru a text file returning the entire # record (pipe delineated) of the 1st occurence sub search { my %args = @_; my $retval; local *FH; open (FH, './fai.txt') || die "Cannot open file: ($!)"; my @records = <FH>; chomp (@records); foreach my $line (@records){ my @fields = split(/\|/, $line); if ($args{part} eq $fields[0] && $args{rev} eq $fields[1]){ $retval = \@fields; #last; } close FH; return $retval; } }
Kerry#!/usr/bin/perl -w use strict; use Data::Dumper; use diagnostics -verbose; use constant PART => 0; use constant REV => 1; print "Enter the Part Number you wish to search for: "; my $part = <STDIN>; chomp $part; print "Enter the Revision for ($part): "; my $rev = <STDIN>; chomp $rev; my $searchresult = search( part => $part, rev => $rev, filename => './file.db', ); print $searchresult ? "Located Part Number: @{[join ':', @$searchresult ]}\n" : "Your Part Number ($part) Rev ($rev) could not be found....\n" ; sub search { my %args = @_; my $found = 0; my @record; open PARTS_DB, $args{filename} or die "Cannot open file $args{fil +ename}: $!"; while (!$found && my $record = <PARTS_DB>) { chomp $record; @record = split /\|/, $record; if ($record[PART] eq $args{part} && $record[REV] eq $args{rev +}) { $found = 1; last; } } close PARTS_DB; return $found ? \@record : undef; }
"Yet what are all such gaieties to me
Whose thoughts are full of indices and surds?"
quotes the Lama
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: using join with a print ref statement
by diotalevi (Canon) on Jan 20, 2003 at 18:57 UTC | |
by Bismark (Scribe) on Jan 20, 2003 at 19:46 UTC | |
by diotalevi (Canon) on Jan 20, 2003 at 21:13 UTC |