Well, let's see:

The -w after #!/usr/bin/perl will give you warning messages...  That's where the 
uninitialized variable stuff comes from.

When I tried the code, it also complained about assigning a value to a read-only var 
(the $$1 = $2).

Here's a solution I came up with that uses a hash to store the names (good for 
scalability for longer lists).

01      #!/usr/bin/perl -w
02
03      use strict;
04      use diagnostics;
05      my %names;
06
07      print '$names{\'James\'}: ' . $names{'James'} . "\n";
08
09      open("FP", "< config");
10
11      while(<FP>) {
12              chomp;
13              /^(\w+)\s+=\s+\"(.*)\"/;
14      #        print "$1 is $2\n";     # Debug
15              $names{$1} = $2;
16      }
17       
18      close(FP);
19
20      print '$names{\'James\'}: '.$names{'James'}."\n";
21
22      exit 0;

-- G

begin  Peter Jay Salzman quotation:
> this stuff is way above my head.
> 
> here is a program (that works) which reads a text file with lines of the form
> 
>       James = "William"
>       Leonard = "DeForest"
>       Montgomery = "James"
> 
> and sets the variable $James to "William", and so on.  note that line 3 is
> commented out.
> 
> 1             #!/usr/bin/perl -w
> 2             use diagnostics;
> 3             #my $James;
> 4
> 5             open("FP", "< config");
> 6             while(<FP>) {
> 7                     chomp;
> 8                     /^(\w+)\s+=\s+\"(.*)\"/;
> 9                     $$1 = $2;
> 10            }
> 11            close(FP);
> 12            print ": $James\n";
> 
> the only error message is that this code produces is that "$James" might to a
> typo because it appears only once in the code.  fair enough.  so i uncomment
> line 3.   NOW the error message is that $James is uninitialized.
> 
> i don't completely grok scoping in perl, but this seems ridiculous.   why would
> perl complain that the variable is uninitialized?
> 
> pete
> 
> -- 
> "Coffee... I've conquered the Borg on coffee!"               [EMAIL PROTECTED]
>        -- Kathryn Janeway on the virtues of coffee           www.dirac.org/p

Reply via email to