Sorry I could not remember your name to give you more credit for the help you gave me. I understand your code much better now than I did last week. The first code here is how I originally had it and the code below that is diotalevi's with a few minor changes. Everything works exactly as I want it to now. I understand fully that there are much more efficient ways to do this. In time..in time.
#!/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; } }
diotalevi's code start's here:
#!/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; }
Kerry
"Yet what are all such gaieties to me
Whose thoughts are full of indices and surds?"
quotes the Lama

In reply to Re: Re: using join with a print ref statement by Bismark
in thread using join with a print ref statement 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.