T?mas gu?mundsson <[EMAIL PROTECTED]> wrote:
> 
> I need help ( you figure ?)
> 
> #!/usr/bin/perl
> use strict;
> use Cwd;
> 
> my $phone_book = qw{
     ^

THat should be '@phone_book' (or '%phone_book', really.)

A list-in-scalar-context (my $var = qw/foo bar/;) evaluates to
the last element of the list.

So $phone_book contains the value "8901", and the rest of 
your list is discarded.... generating a warning if you turn
on warnings:

  $ perl -we '$foo = qw/foo bar/'
  Useless use of a constant in void context at -e line 1.

Which is one more reason to use -w.

>   Fred 0123
>   John 4567
>   Bill 8901
> };
> my $selection = 1;
> do {
>  print "\n  Please select one choice";
>  print "\n  1. Enter name.";
>  print "\n  2. Quit.\n";
> 
> chomp(my $selection = <STDIN>);

You've already declared '$selection'.

When you declare a lexical variable (with 'my' or 'our'),
it will be visible for the rest of the enclosing scope.

And when you declare two variables with the same name, the second 
one temporarily "hides" the first:

  my $foo = 1;
  
  { # curlies create a new 'scope'

      my $foo = 2;
      print $foo;  # 2 
  }

  print $foo; # 1

If we make that into a do-while loop, we'll get the problem 
you've encountered.  The variable checked in the 'until' 
expression is *not* the one declared inside the loop.

  my $foo = 1;

  do { my $foo = 2 } until $foo == 2;  # infinite loop:
                                       # this is the OUTER $foo,
                                       # which is always 1.

So you should use:
 
  chomp ($selection = <STDIN>);

> if ($selection eq "1") {
>  chomp(my $number = <STDIN>);
>  print "$number  number is : $phone_book \n"

Looks like you want to take a name from STDIN, and look up the 
associated phone-number.  And any time you are thinking of 
'looking something up', phonebook style, you want a %hash.

  my %phone_book = qw( Fred 0123 John 4567 Bill 8901 );

And then to do the lookup:

  print "$number number is : $phone_book{$number}.\n";

> }
> } until ($selection eq "2");


HTH
-- 
Steve

perldoc -qa.j | perl -lpe '($_)=m("(.*)")'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to