in reply to Re^3: using join with a print ref statement
in thread using join with a print ref statement
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.# #!/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; # }
Kerry#!/usr/bin/perl -w use strict; use Data::Dumper; use diagnostics -verbose; use constant PART => 0; use constant REV => 1; #This is where the user will enter the Part Number and Revision Level print "Enter the Part Number you wish to search for: "; my $part = <STDIN>; chomp $part; while (!$part){ # while $PartNumber is empty keep asking print "PartNumber? "; $part = <STDIN>; chomp($part); } print "Enter the Revision for ($part): "; my $rev = <STDIN>; chomp $rev; while (!$rev){ # while $rev is empty keep asking print "Revision? "; $rev = <STDIN>; chomp($rev); } my $searchresult = search( part => $part, rev => $rev, filename => './file.db', ); print ref $searchresult ? "Located Part Number: @{[join ':', @$searchresult ]}\n" # print th +e part number and the records : "Your Part Number ($part) Rev ($rev) could not be found....\n"; #p +rint if part number or rev is invalid # subroutine that takes the arguments from my $searchresult and checks # the text file for a matching part number and revision level sub search { my %args = @_; my $retval; 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}; close PARTS_DB or die "Can't close PARTS_DB $args{filename}: +$!"; return \ @record; } }
"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^5: using join with a print ref statement
by diotalevi (Canon) on Jan 20, 2003 at 21:13 UTC |