Forum Moderators: open

Message Too Old, No Replies

Reading text files using HTML

         

GhostPig

10:50 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



'lo all,

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.

PatomaS

11:47 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Well

In deed it is possible, but not with HTML or any variant, you need another language, perl or php for example.

You can use recent versions of Macrmedia Flash too

Bye

GhostPig

12:02 am on Mar 5, 2004 (gmt 0)

10+ Year Member



Thanks for that.

Do you know of any rough guides for the PHP route? I do have Flash too.. but I've not done anything with that yet...

lexipixel

7:40 am on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd do it with Perl...

#!/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]

lexipixel

7:43 am on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




... and delete the lines regarding Name2, Name3, etc if all you're trying to do is produce a "Top Player / Score" list from a log file...

GhostPig

9:56 am on Mar 5, 2004 (gmt 0)

10+ Year Member



Thanks for that - very kind of you!

However, having never used perl it means that I'm clueless of how to integrate into a page - I'm off to do some reading on the matter.

Ta muchly

Mike12345

10:17 am on Mar 5, 2004 (gmt 0)

10+ Year Member



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>) {

Just wanted to clarify for ease of use.

lexipixel

11:02 am on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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]

Mike12345

11:50 am on Mar 11, 2004 (gmt 0)

10+ Year Member



Just if Ghostpig has no experience with perl, then how would he/she know (or anyone new to the forum)that the smiley represents >) just thought id mention to avoid confusion.

Nothing against you Lexipixel.

tedster

9:59 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just went in and turned off the graphic smile faces -- you can do it too by checking the box "Turn off graphic smile faces for this post" below the text area where you create a post.

It's a good idea when your posting code -- seems there's almost always a smile combination in most snippets.

brucec

1:40 am on Mar 12, 2004 (gmt 0)

10+ Year Member



GhostPig,
It is also possible with PHP and I believe it is easier. All you need to do is have a PHP script that reads the name value, which would be "name1" in name1=ghost pig and the value is "ghost pig" or "ghost%20pig"

You can use $name as the variable and put it anywhere in the PHP script.

I hope this sheds some additional light.

lexipixel

2:51 am on Mar 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Are you saying it turns --> :)
yellow graphical smiley faces?
I don't see them at all.

I didn't realize what you were talking about, YES, it is a good idea to turn off graphic smiley's when code is posted if that's what happens.