Ryan Dillinger wrote:
> Hello,

Hello,

> I have a script here, I have been going over and over.
> Every time I run it I get several errors, and I have tried to fix them
> to no avail.
> Can someone tell me what line I missed, please?
>     Thanks for your help!
> 
> #!usr/bin/perl
> use warnings;
> use strict;
> 
> %names = ();
> @raw = ();
> $fn = "";
> $in = '';
> @keys = ();
> @n = ();
> $search = '';

Because of the strict pragma you have to declare your variables before using
them.  Also the only variable you need at this scope is %names.


> while (<>) {
>    chomp;
>    @raw = split(" ", $_);
>    if ($#raw == 1) { # regular case
>       $names{$raw[1]} = $raw[0];
>    } else {  # build a first names
>       $fn = "";
>       for ($i = 0; $i < $#raw; $i++) {
>          $fn .= $raw[$i] . " ";
>  }
>  $names{$raw[$#raw]} = $fn;
> }
> }

That is way too complicated.  :-)

my %names;
while ( <> ) {
    my @raw = split;
    $names{ $raw[ -1 ] } = "@raw[ 0 .. $#raw - 1 ]";
    }

But that still has the same two problems that the original does, if the input
line has zero or one field and if you have two or more lines with the same
last name.


> while () {
>     print "\n1. Sort names by last name\n";
>     print "2. Sort names by first name\n";
>     print "3. Search for a name\n";
>     print "4. Quit\n\n";
>     print "Choose a number: ";

      chomp( my $in = <STDIN> );


>     if ($in eq '1') {
>        foreach $name (sort keys %names) {

         foreach my $name ( sort keys %names ) {


>           print "$name, $names{$name}\n";
>    }
> 
>  } elsif ($in eq '2') {
>     @keys = sort { $names{$a} cmp $names{$b} } keys %names;

      my @keys = sort { $names{$a} cmp $names{$b} } keys %names;


>     foreach $name (@keys) {

      foreach my $name ( @keys ) {


>        print "$names{$name} $name\n";
>     }
> } elsif ($in eq '3') {
>      print "Search for what? ";
>      chomp($search = <STDIN>);

       chomp( my $search = <STDIN> );

       my @keys;

Because you had @keys in file scope if you ran menu item '2' and then menu
item '3' the @keys array would have the left-over values from menu '2' and
would print out the complete list no matters what was in $search.


>      while (@n = each %names) {

       while ( my @n = each %names ) {


>       if (grep /$search/, @n) {
>         $keys[++$#keys] = $n[0];

          push @keys, $n[ 0 ];


>        }
>      }
>     if (@keys) {
>       print "Names matched: \n";
>       foreach $name (sort @keys) {

        foreach my $name ( sort @keys ) {


>          print  "  $names{$name} $name\n";
>       }
>   } else {
>       print "None found.\n";
>   }
> 
>   @keys = ();

Not required with the my @keys; line above.


> } elsif ($in eq '4') {    #quit
>    last;
> } else {
>    print "Not a good answer. 1 to 4 please.\n";
> }
> }


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to