Web Hosting Talk







View Full Version : Language Roundup: Which is the best?


JustinH
12-19-2003, 11:08 PM
This is a bit of a followup to my PHP vs Perl thread, with a twist. I'm going to layout some rules and using the language of your choice, you'll need to write out the code. The goal: which language can do it in the fewest lines of code.

Rules

Shebang lines and open and close tags (<?php ?>) don't count.
One line per end tag. Once the tag is completed (via a semicolon or any other means), go to the next line.
Start and end brackets don't count as lines of code. This way, alternative syntax (if... endif) won't play a role.
There will be two winners, one for the least lines of code, and the second for the least amount of characters in the code (less spaces).
No OS-level functions (this means, system(), shell() and related functions are off limits.)

The Code

Create a script that will open a local file located at /usr/bin/file.fil, check to see if the file contains the word "hello" (without quotes). If it does, print to the screen "Hello was found", if it isn't there, write the word "hello" to the beggining of the file, and print to the screen "Hello was added to the file".

Off we go :).

carrotweb
12-19-2003, 11:14 PM
[soapbox]

I'm pretty sure a perl programmer will win this one, but does the least amount of code really equate the best? I'm a software developer by day (Java and gasp, Visual Basic). With the type of work I do, the lines of code is immaterial. Clarity and reuse of code is paramount since other developers of all levels will be looking at and working with my code.

[end soapbox]

That said, carry on. I look forward to seeing the results.

JustinH
12-19-2003, 11:22 PM
Originally posted by carrotweb
[soapbox]

I'm pretty sure a perl programmer will win this one, but does the least amount of code really equate the best? I'm a software developer by day (Java and gasp, Visual Basic). With the type of work I do, the lines of code is immaterial. Clarity and reuse of code is paramount since other developers of all levels will be looking at and working with my code.

[end soapbox]

That said, carry on. I look forward to seeing the results.

In terms of execution speed the size of the code is pretty much indifferent. With scripting lanuages (Perl, JSP, ASP, PHP), it depends primarily on the speed of the interpreter. That being said, if this were an issue of execution speed, Perl, PHP and ASP.NET would be in the running (although, something this small would really be 1000ths of milliseconds) by themselves.

It's not really an issue of that, it's more an issue of ease of coding. If for example, you can do something in Perl in 500 lines and the same in ASP would take 1000 lines, it would show how much money could be saved in Perl by saving time in the coding process.

And I agree... Perl is probably gunna whoop up on this test.

zealotx
12-19-2003, 11:34 PM
Ill get it started:

<?php
if(strpos(file_get_contents("/usr/bin/file.fil"), "hello") !== false)
{
print "Hello was found";
}
else
{
system("echo hello > /usr/bin/file.fil");
print "Hello was added to the file";
}
?>

hiryuu
12-19-2003, 11:49 PM
Well, heck, if system() is on the table...


#!/bin/sh
( grep -q 'hello' /usr/bin/file.fil && echo 'Hello was found' ) ||
( echo 'hello' > /usr/bin/file.fil && echo 'Hello was added to the file' )

JustinH
12-19-2003, 11:52 PM
Hey now... no OS specific funtions. Has to be done purely in the language itself.

brendandonhu
12-20-2003, 12:20 AM
PHP - 7 lines

<?php
if (!eregi("hello",file_get_contents("/usr/bin/file.fil")))
{ file_put_contents("file.txt","Hello" . file_get_contents("/usr/bin/file.fil"));
echo "Hello was added to the file"; }
else { echo "Hello was found"; }
?>

brendandonhu
12-20-2003, 12:21 AM
Wait... thats 6 lines actually :D
Lines 3 and 4 are one line of code, either the forum or notepad broke them up.

zealotx
12-20-2003, 01:01 AM
file_put_contents is (PHP 5 CVS only) not exactly production...

anyways:

<?php
if(strpos(file_get_contents("/usr/bin/file.fil"),"hello")!==false)
{
print "Hello was found";
}
else
{
$pipe = fopen("/usr/bin/file.fil","w");
fwrite($pipe,"hello",strlen("hello"));
fclose($pipe);
print "Hello was added to the file";
}
?>


Seven lines.

brendandonhu
12-20-2003, 01:40 AM
Wasn't sure if I could use file_put_contents or not, but I figured I would because it IS part of PHP now.