in reply to pulling information from a delineated text file using a subroutine

#!/usr/bin/perl -w use strict; sub get_part_num { my $part; while (!$part || $part =~ /[^0-9]/) { print "Part Number? "; chomp( $part = <STDIN> ); } return $part; } sub search { my ($file, $part) = @_; open my $db, "<$file" or die "Could not read file: $!"; while (<$db>) { chomp; my @fields = split(/\|/, $_); print "Part $part found: \n\t", join("\n\t", @fields[1..$#fields]), "\n" if $fields[0] eq $part; } } search( 'fai.txt', get_part_num() );

  • Comment on Re: pulling information from a delineated text file using a subroutine
  • Download Code

Replies are listed 'Best First'.
Re: Re: pulling information from a delineated text file using a subroutine
by Hofmator (Curate) on Jan 16, 2003 at 10:00 UTC
    What happens if you want part number 0 ?

    -- Hofmator

      I didn't really consider this option as I suppose I would never assign a product to ID 0. Zero isn't really a number, therefore why would I assign a product to a not-really-a-number ID? :P

      Anyway, if you really did need part 0, then just change while (!$part || $part =~ /[^0-9]/) { to while (!defined $part || $part =~ /[^0-9]/) {. The only thing this will also change is if the user hits enter without entering anything, the program will exit. That's fine by my standards