Page 1 of 1

Perl to PHP. Opening Files.

Posted: Tue Nov 12, 2002 1:56 pm
by andy7t
Hello,
I need to do this:

Opens: map.txt
this contains a list of numbers from 1-1020
foreach number Opens: /map/$NUMBER.txt
then it decides which image to print based on the contents of $number.txt
e.g if it's your userid in line1 it will print a blue block.

Now, i have never written a PHP script before, but i do write Perl Scripts. I can do it in Perl, but it takes up far to much resources. So my host suggested this place, and to use PHP. This is the script in Perl:

Code: Select all

open (ALL, "+<$olduserlines&#1111;10]/land/all.txt");
	$lockfail = &lock("ALL");
	if ($lockfail)
	&#123;
		print("Error opening the landfile.\n");
	&#125;
	@accounts = <ALL>;
	close (ALL);
	print("<table width=369 BORDER=0 CELLSPACING=0 CELLPADDING=0 bgcolor=black> <tr><td>\n");
	

		foreach $accounts (@accounts)
		&#123;
			chop $accounts if ($accounts =~ /\n$/);
			


open (ALLY10, "+<$place/land/$accounts.txt");
	$lockfail = &lock("ALLY10");
	if($lockfail)
	&#123;

	&#125;
	@userlines = <ALLY10>;
	close (ALLY10);
	foreach $userlines (@userlines)
	&#123;
		chop $userlines if ($userlines =~ /\n$/);
	&#125;



#GET THE IMAGE COMMANDS
if($userlines&#1111;0] eq $userid)
&#123;
$imageoutput = "blue";
&#125;


if($userlines&#1111;0] ne $userid)
&#123;
$imageoutput = "mapred";
&#125;

if($lockfail)
&#123;
$imageoutput = "black";

&#125;

print("<font color=WHITE face="Arial Narrow" size=-7><a href=zoom.cgi?$accounts><img src="$imagedir/$imageoutput.gif" border=0></a>\n");&#125;
Any ideas in PHP? REMEMBER IT MUST USE LOW RESOURCES!

Posted: Tue Nov 12, 2002 2:07 pm
by volka
just some suggestions for your perl script:

Code: Select all

@accounts = <ALL>;
   close (ALL);
   ...
   foreach $accounts (@accounts)
   &#123;
   ...
looks like you read the whole file into one array (equal to $arr = file('filename'); in php). You certainly can read a file line by line which will use lesser ressources if the file is large.

Code: Select all

$lockfail = &lock("ALLY10");
...
   @userlines = <ALLY10>;
   close (ALLY10);
   foreach $userlines (@userlines)
same thing.

Code: Select all

foreach $userlines (@userlines)
&#123;
    chop $userlines if ($userlines =~ /\n$/);
&#125;
why not doing this once at the beginning (creating an array of strings without \n) ? This will decrease the costs from M*N to M (M=#accounts, N=#userlines).
Of course this can be done in PHP,too ;) But I suggest you try the above first. Probably PHP doesn't use less ressources than PERL (maybe wrong)