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

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

Replies are listed 'Best First'.
Re: Re^3: using join with a print ref statement
by Bismark (Scribe) on Jan 20, 2003 at 19:46 UTC
    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.