I 4m 1337 h4x0r5 !!!

Electric Amish

Elite Member
Oct 11, 1999
23,578
1
0
I just wrote my first Perl script!!! :D

Notfred helped me in the beginning by writing me a script that took data from an HTML form and wrote it out to a .txt file.

I just wrote my own script to read the file back in and display it. :)

YAY! for me. ;)

amish
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
Hehe... someone has caught the PERL bug :).

Wait until you figure out how to do all those repitive tasks with 1 PERL script.

I have on that goes through my mp3 directory tree and makes playlists for every single album I have and puts them in a list directory, so I have easy access to my mp3 playlists...
 

ElFenix

Elite Member
Super Moderator
Mar 20, 2000
102,406
8,585
126
script kiddies unite! (or untie, as that other thread suggests)
 

Electric Amish

Elite Member
Oct 11, 1999
23,578
1
0
DAMN!!

I just wrote another script that builds a webpage with and HTML form on it that includes a drop-down box that contains info out of the .txt file. :D

amish
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
#!/usr/bin/perl

print "Content-Type: text/html \n\n<table>";
open FILE, "<outfile.txt";
while (<FILE> ) {
$_ =~ s/,/<\/td<td>/g;
print "<tr><td>$_</td></tr>\n"
}
print "</table>";

close FILE;

# I haven't tested this, but it should run :)

Mind if I see your code Amish?

BTW, congrats :)

edit: damn smileys.
 

MichaelD

Lifer
Jan 16, 2001
31,528
3
76
Originally posted by: notfred
#!/usr/bin/perl

print "Content-Type: text/html \n\n<table>";
open FILE, "<outfile.txt";
while (<FILE> ) {
$_ =~ s/,/<\/td<td>/g;
print "<tr><td>$_</td></tr>\n"
}
print "</table>";

close FILE;

# I haven't tested this, but it should run :)

Mind if I see your code Amish?

BTW, congrats :)

edit: damn smileys.


It looks menacing! :Q What does it do?
 

Electric Amish

Elite Member
Oct 11, 1999
23,578
1
0
Originally posted by: notfred
#!/usr/bin/perl

print "Content-Type: text/html \n\n<table>";
open FILE, "<outfile.txt";
while (<FILE> ) {
$_ =~ s/,/<\/td<td>/g;
print "<tr><td>$_</td></tr>\n"
}
print "</table>";

close FILE;

# I haven't tested this, but it should run :)

Mind if I see your code Amish?

BTW, congrats :)

edit: damn smileys.

Yeah, it looks basically just like that....
rolleye.gif
;)

#!/usr/bin/perl

# name of output file
$output = 'outputfile.txt';

#loads CGI module
use CGI;

if (CGI:: param('name1') eq'')
{
$name = CGI:: param('name2');
}
else
{
$name = CGI:: param('name1');
}

$Found = 0;

# Opens the file for input, or dies if it can't open the file.
open(IN, $output)
|| die ("can't open $output");

#assign data to an array
@raw_data=<IN>;
close(IN);

print "Content-type: text/html\n\n";
print "<HTML><BODY>";

foreach $Line (@raw_data)
{
if ($Line ne "\n")
{
(@Team)=split(/\,/,$Line);
if ($name eq $Team[1])
{
$Found = 1;
for($count=0;$count<20;$count++)
{
if ($count eq 0)
{
print "<b>$Team[$count]</b>
";
}
else
{
print "$Team[$count]
\n";
}
}
print "
\n";
}
}
}

if ($Found eq 0)
{
print "Name Not Found";
}

print "</BODY></HTML>";

amish :)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: MichaelD
Originally posted by: notfred
#!/usr/bin/perl

print "Content-Type: text/html \n\n<table>";
open FILE, "<outfile.txt";
while (<FILE> ) {
$_ =~ s/,/<\/td<td>/g;
print "<tr><td>$_</td></tr>\n"
}
print "</table>";

close FILE;

# I haven't tested this, but it should run :)

Mind if I see your code Amish?

BTW, congrats :)

edit: damn smileys.


It looks menacing! :Q What does it do?

Opens up a comma seperated value file and ouputs the information from the file in a HTML table for a webserver. If EA sends me an one of his ouptut files I'll run it and make sure it works like it's supposed to :)
 

Hyperblaze

Lifer
May 31, 2001
10,027
1
81
Congrats on your PERL script

What would we do without Linux and perl :)

Actually...I can live without perl, I'm a PHP fanatic.

But can't live without linux.


 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Hope you don't mind, but I'm going to critique your code :)

#!/usr/bin/perl # This line looks ok :)

# name of output file
$output = 'outputfile.txt';

#loads CGI module
use CGI;
# if you do this:
use CGI qw:)standard);
# instead, it will load the CGI routines into the standard namespace, so you can jsut use "param" instead of "CGI::param"

if (CGI:: param('name1') eq'')
# this does the same thing, but is shorter: unless (param('name1'))
{
$name = CGI:: param('name2');
}
else
{
$name = CGI:: param('name1');
}

$Found = 0;

# Opens the file for input, or dies if it can't open the file.
open(IN, $output)
|| die ("can't open $output");

# When opening files for input, you should precede the filename with "<" which indicates your opening files read only.

#assign data to an array
@raw_data=<IN>;
close(IN);

# Assigning an entire file to an array this way in generally considered bad practice, because you might have a very large file,
# and if you load the whole thing into memory at once, it takes up a lot of memory. However, there are some cases where you
# really don't have much of a choice.

print "Content-type: text/html\n\n"; # if you use the qw:)standard) thing I added up at the top, then you can jsut do "print header" as
# there's a header function in the CGI module.
print "<HTML><BODY>";

# If you want two line breaks, do this: \n\n
# \n followed by an actual carriage return gets really messy looking.


foreach $Line (@raw_data)
{
if ($Line ne "\n")
{

# The whole commented out block can be replaced with this:

if ($Line =~ m/^$name,/) {
$Found =1;
$Line =~ s/^($name),/<b>$1<\/b>/;
print "$Line\n";
}
# (@Team)=split(/\,/,$Line);
# if ($name eq $Team[1])
# {
# $Found = 1;
# for($count=0;$count<20;$count++)
# {
# if ($count eq 0)
# {
# print "<b>$Team[$count]</b>";
# }
# else
# {
# print "$Team[$count]\n"; }
# }
# print "\n";
# }
}
}
if ($Found eq 0)
# I'd do this instead:
unless ($Found)
{
print "Name Not Found";
}

print "</BODY></HTML>";



# I think that's all correct. :)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Electric Amish
if ($Line =~ m/^$name,/) {
$Found =1;
$Line =~ s/^($name),/<b>$1<\/b>/;
print "$Line\n";


Will you explain this to me, please?

amish

Ok, sure (just got back from a class.... work and school is all I do...)

You split up $Line into an array so that you could check the first element against $name.
what $Line =~ m/^$name,/ does is do the same comparison without breaking up the variable first.

This gets into a regular expression tutorial really quickly (which can get quite nasty), but the basica idea is that m// is used to match text. (whatever's between the /'s). the ^ represents the beginning of the line, and the ',' is a reguilar old comma. When you insert $name in the middle, you're checking to see if $Line matches "beginning of the line, immediately followed by $name, immediately followed by a comma". If it does match, then you continue your if statement.

the line: $Line =~ s/^($name),/<b>$1<\/b>/; does a similar thing, except it's a substitution instead of a match. s/// <- the first set of /s is the same as a match, but after it finds a match, it replaces that text with whatever's in between the last two /'s. The parenthesis are used for grouping, so that you can reuse matched text later in the expression. What's inside the first set of parenthesis becomes $1 for later use, what's inside a second set of parenthesis would become $2, etc.

I did do one thing wrong though, I forgot to remove all the rest of the commas from the line. that could be achived by $Line =~ s/,//g; <- replaces commas with nothing. the "g" at the end forces the substitution to be applied to the entire string, rather than jsut the first comma (which will happen if you leave off the g).

This is better covered in the prel regular expresion documentation.

btw, the output from the little script at the top of the page looks like this:

<table><tr><td>Week 1</td><td>GUY'S NAME</td><td>49ers</td><td>Redskins</td><td>Packers</td><td>Ravens</td>
<td>Dolphins</td><td>Colts</td><td>Chiefs</td><td>Bears</td><td>Bills</td><td>Eagles</td><td>Chargers</td><td>Buccaneers</td>
<td>Raiders</td><td>Rams</td><td>Cowboys</td><td>Steelers</td><td>35</td><td>Mon Sep 2 19:52:24 2002
</td></tr>
<tr><td>
</td></tr>
<tr><td>Week 1</td><td>OTHER GUY'S NAME</td><td>49ers</td><td>Redskins</td><td>Packers</td><td>Ravens</td><td>Dolphins</td><td>Colts</td>
<td>Browns</td><td>Vikings</td><td>Bills</td><td>Eagles</td><td>Chargers</td><td>Buccaneers</td><td>Raiders</td><td>Rams</td>
<td>Cowboys</td><td>Steelers</td><td>36</td><td>Mon Sep 2 19:56:52 2002
</td></tr>
<tr><td>
</td></tr>
</table>