Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl help, i'm so close.

Status
Not open for further replies.

eitremn

Programmer
Sep 30, 2009
3
0
0
US
hello, i'm writing a perl script to take in user info and and write it to a file. my current code:

Code:
#!/usr/local/bin/perl
print "Content-Type: Text/html\n\n";

$str=$ENV{QUERY_STRING};
($first, $second, $third) = split(/&/, $str);
($dummy, $newid) = split(/=/, $first);
($dummy, $newfName) = split(/=/, $second);
($dummy, $newlName) = split(/=/, $third);

open (bowlers, "bowl.dat") or die ("file didn't open");
@bInfo = <bowlers>;
close (bowlers);

foreach(@bInfo)
{
	chomp(@bInfo);
	($id, $therest) = split(/#/, $_);
	if($newid == $id)
	{
		print "<html>\n";
		print "<head>\n";
		print "<title>Lake Side Bowling League:  Add Bowler.</title>\n";
		print "<meta http-equiv='REFRESH' content='2;url=http://tacosalad.lssu.edu/~neitrem/csci333/bowling/addbowler.html'>\n";
		print "</head>\n";
		print"<body>\n";
		print "I.D. already exists<br />\n";
		print "</body></html>\n";
		$test = 0;
		last;
	}
	else
	{
		$test =  1;
	}
}

if($test == 1)
{
	open(bowlers, ">>bowl.dat") or die("file didn't open");
	print bowlers "\n$newid#$newlName#$newfName#";
	close (bowlers);
	print "<html>\n";
	print "<head>\n";
	print "<title>Lake Side Bowling League:  Add Bowler.</title>\n";
	print "<meta http-equiv='REFRESH' content='2;url=http://tacosalad.lssu.edu/~neitrem/csci333/bowling/index.html'>\n";
	print "</head>\n";
	print"<body>\n";
	print "Bowler Added Successfully<br />\n";
	print "</body></html>\n";
}

it works fine, except for sometimes it inserts a blank line before adding the bowler.
 
it could be coming in from your form.. are you using a text area or a text field? You might want to try and use the CGI module instead of trying to roll your own.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Code:
    open(bowlers, ">>bowl.dat") or die("file didn't open");
    print bowlers "[red]\n[/red]$newid#$newlName#$newfName#";
    close (bowlers);

The line break here is probably where the blank line is coming from...

If the file doesn't exist and is created for the first time, it would end up making the first line in the file blank:

Code:
1
2 first entry added

Then when a second entry is added it would probably look like:

Code:
1
2 first entry added
3 second entry added

You could either try putting the \n at the end of the line (so the file ends with a blank line all the time), or otherwise you'd have to put the whole file into an array of lines and join them when you print them back.

Example:

Code:
open (READ, "file.txt");
my @lines = <READ>;
close (READ);

chomp @lines; # removes line breaks from ends of lines

# add a new line
push (@lines, "a new line");

# write them back
open (WRITE, ">file.txt");
print WRITE join("\n",@lines);
close (WRITE);

So in this case all the lines in @lines are joined with a \n between them, so there doesn't need to be any leading or trailing line breaks resulting in empty lines.

(of course, if the input file has an empty line in it, it'll still be an empty line when written back to the file... this approach only prevents an empty line from being at the start or end of a file).

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top