Ok, just not to let you think that PerlMonks are not
helpful people, I'll go on with Albannach's
recommendations and propose a more Perlish way of doing
it.
First off, you should use strict. This
prevents you from using undeclared variables, which is
usually considered bad programming practice. use strict is not enabled by default because Perl is
polite enough to not dictate what good programming practices
are.
Then, look at your subs. They all rely on
some global variable being set. This is also generally
considered as bad programming practice.
(UPDATE, following sierrathedog04's remark: of course, this only true in general. There are
cases where global variables come in handy. They should normally
be kept to a minimum)
A function should
take arguments and return a result or perform an action that
depends solely on these arguments. This allows the code to
be more generic and robust. In that case, all your
getsomething functions could have been only one
function:
sub getvalue {
my $line = shift; # get the argument
my $start = index($line, ':');
return substr($line, $start+2, length($line));
}
Which you would call like so:
print getvalue($PasswordLine);
print getvalue($MinattLine);
print getvalue($MaxattLine);
print getvalue($ExpLine);
print getvalue($GoldLine);
Actually, there are other ways to achieve this. You
should read the substr manpage to learn a
more elegant way of getting the last part of a string, and
the split manpage for an even more elegant
way of splitting a string into parts. With this new
knowledge, you'll find it's so easy that you won't want to
create a function for such a trivial task.
Back to your problem... So, you want to open a file, the
lines of which consist of a key and a value, separated by a
colon. Once the file has been read through, you want to
retrieve the values that were associated with the keys.
Keys, values, hmmm, that sounds like a job for
hashes.
Here you go: open the file and complain if something goes
wrong:
open FILE, "user/test" or die "Error: $!\n";
Loop over each line, and close the file at the end:
while ( my $line = <FILE> ) {
...
}
close FILE;
Each line consists of a key/value pair, separated by a
colon and possibly whitespace. We will store the data into a
hash, which you need to declare before the while loop.
my %data = ();
Foreach line, we'll split it on the colon, possibly with
whitespace (read perlre to learn everything about regular
expressions). But you don't want the trailing newline
character to be part of the value, do you? Use
chomp for that:
chomp $line;
my ($key, $value) = split(/\s*:\s*/, $line);
Now, stuff the value into the hash, indexing it with the
key: $data{$key} = $value;
When the loop is over, you can access the elements
separately from %data:
print $data{password};
print $data{minatt};
print $data{maxatt};
print $data{exp};
print $data{gold};
Here's the code in its entirety:
#!/usr/local/bin/perl -w
use strict;
open FILE, "user/test" or die "Error: $!\n";
my %data = ();
while ( my $line = <FILE> ) {
chomp $line;
my ($key, $value) = split(/\s*:\s*/, $line);
$data{$key} = $value;
}
close FILE;
print $data{password};
print $data{minatt};
print $data{maxatt};
print $data{exp};
print $data{gold};
Of course, since you seem to be sending all that to a web
browser, you'll need a little extra formatting. But I won't
do all the work for you! Raise your heart and good luck in
your quest...
--bwana147
|