Forum Moderators: open
I was just wondering if it was possible to read items for a text file which is laid out similar to:
&name1=ghost pig&score1=118580&name2=ghost pig&score2=44030&name3=blink 182 fan&score3=15700
And reproduce the read part - say for instance I just want to read the first name out of every file, and the ignore the rest - is there anyway for me to do this?
Any help is much appreciated - thanks.
#!/usr/local/bin/perl
#
# change 'filename.txt' to
# path/name of your data file
#
$datafile = 'filename.txt';
#
open (DAT,"$datafile");
seek (DAT,0,0);
while ($line = <DAT>) {
@pairs = split(/&/,$line);
foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
if ($name eq "name1") { $name1 = ("$value");}
elsif ($name eq "score1") { $score1 = ("$value");}
elsif ($name eq "name2") { $name2 = ("$value");}
elsif ($name eq "score2") { $score2 = ("$value");}
elsif ($name eq "name3") { $name3 = ("$value");}
elsif ($name eq "score3") { $score3 = ("$value");}
}
print "Name1 = $name1, (Score1 = $score1)\n";
print "Name2 = $name2, (Score2 = $score2)\n";
print "Name3 = $name3, (Score3 = $score3)\n";
}
close (DAT);
exit;
[edited by: tedster at 9:56 pm (utc) on Mar. 11, 2004]
Mike12345 writes, "good soloution lexipixel. Ghostpig, this example works except for where you see a smiley you need to replace with just ) bracket so that line should read:while ($line = <DAT>) {
I don't see what you are telling him to change.. my code reads the same as the line you are telling him to change....
[edited by: tedster at 9:56 pm (utc) on Mar. 11, 2004]
You can use $name as the variable and put it anywhere in the PHP script.
I hope this sheds some additional light.