
japhy
Enthusiast
May 30, 2000, 8:17 PM
Post #2 of 5
(494 views)
|
|
Re: Help shortening long if/else statement
[In reply to]
|
Can't Post
|
|
Use a hash. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> open FILE, $file or die "can't open $file"; while (<FILE> ) { # note -- there's no need to put this in # an array and then go through the array # just use a while loop like this if (/database(\d):\s+$db/) { $current = $1; # quotes aren't needed } elsif (/$current \w+):\s+(.*)/) { $DATA{$1} = $2; } } close FILE; # now the %DATA hash looks like # $DATA{datafile} = 'data.txt'; # $DATA{heading1} = 'Misc1'; # etc... </pre><HR></BLOCKQUOTE> To learn how to use hashes, read the 'perldata' section of the Perl documentation, available on this web site at the "Perl Documentation" link.
|