in reply to Re: using join with a print ref statement
in thread using join with a print ref statement

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

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

    I'm almost positive that I didn't write it exactly like that because I almost never write not as an exclamation mark. Anyhow, you could omit that $found value because your last skips right out of the while. The other use you use for $found is irritating. I much prefer setting the return value explicitly and letting that take the place of $found. I copied my code, fixed it up and commented the changes.

    sub search { my %args = @_; # Turned into a scalar. An array reference will be # stored here if the value being searched for was # found. my $return_record; local *PARTS_DB; open PARTS_DB, $args{filename} or die "Cannot open file $args{fil +ename}: $!"; while (my $record = <PARTS_DB>) { chomp $record; my @record = split /\|/, $record; next unless $record[PART] eq $args{part} and $record[REV] eq $args{rev}; # Change this from assigning to @return_record # so now it's an array reference. Also add a last; # call; $return_record = \ @record; last; } close PARTS_DB or die "Can't close PARTS_DB $args{filename}: $!"; # This returns whatever was found which may be nothing return $return_record; }

    Seeking Green geeks in Minnesota

      My mistake I credited the wrong code to you. I apologize. Here is the code as you originally posted for me.
      # #!/usr/bin/perl -w # # use strict; # use Data::Dumper; # use diagnostics -verbose; # # use constant PART => 1; # use constant REV => 2; # # 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, # filename => './file.db' ); # # print ref $searchresult # ? "Located Part Number $part: @{$searchresult}[1 .. 7]\n" # : "Your Part Number ($part) Rev ($rev) could not be found...\n"; # # sub search { # my %args = @_; # my @return_record; # # local *PARTS_DB; # open PARTS_DB, $args{filename} or die "Cannot open file $args{fi +lename}: $!"; # while (my $record = <PARTS_DB>) { # chomp $record; # my @record = split /\|/, $record; # next unless $record[PART] eq $args{part} # and $record[REV] eq $args{rev}; # # @return_record = @record; # } # close PARTS_DB or die "Can't close PARTS_DB $args{filename}: $!" +; # return \ @return_record; # }
      This is how it stands at this time with the changes. As you can see I do not use found at all in the new version.