in reply to Re: Trying to make a search
in thread Trying to make a search

Since we're already picking nits and playing reductionists; here's removing a little more redundancy. Hopefully without crossing over and becoming obscure (versions involving map()/grep()-combos deliberately left as an excersice for the reader ;D)
#!/usr/bin/perl -w

  use strict;

  print "Letter? ";
  my $letter = <STDIN>;   
  chomp $letter;
  open DATA, "books.txt" or die "open(books.txt) returned: $!\n";
  foreach my $line (<DATA>) {
    print $line if $line =~ /^$letter/i;
  }  
  close DATA;

Replies are listed 'Best First'.
RE: Re: Trying to make a search
by Alokito (Novice) on Jun 05, 2000 at 10:19 UTC
    Grep is not obscure. It is much clearer than dumb loops that are really just low level implementations of list operations.
    #!/usr/bin/perl -w use strict; print "Letter? "; chomp(my $letter = <STDIN>); open DATA, "books.txt" or die "open(books.txt) returned: $!\n"; print grep {/^$letter/io} <DATA>;
    Of course, I kind of like something like
    perl -e 'print grep {/^a/io} <>' /usr/dict/words
    Try it, you'll like it too!