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!

i'm having trouble opening a file more then once 1

Status
Not open for further replies.

NateBro

Programmer
Sep 1, 2004
233
0
0
US
i'm using a FOR loop, and in it-it opens a file, and writes the input out, only it will only do it once. why is this?


Code:
for ($i = 0; $i <= $#topic_posts; $i++){
	if ($topic_posts[$i] =~ /0ISTARTI0/){
	$Readusername = $topic_posts[$i + 2];
	$post_date = $topic_posts[$i + 1];
	$post = $topic_posts[$i + 3];
	chomp($Readusername);
	
	print getprofile();
	
	}
}	




sub getprofile {

open (DATA, "profiles/$Readusername.pro");
$username1  = <DATA>;
$username11 = <DATA>;
$username12 = <DATA>;
$username13 = <DATA>;
$username14 = <DATA>;
$username15 = <DATA>;
$username16 = <DATA>;
$username17 = <DATA>;
$username18 = <DATA>;
$username19 = <DATA>;
$username2  = <DATA>;
$username21 = <DATA>;
$username22 = <DATA>;
$username23 = <DATA>;
$username24 = <DATA>;
$username25 = <DATA>;
$username26 = <DATA>;
$username27 = <DATA>;
$username28 = <DATA>;
$username29 = <DATA>;
$username3  = <DATA>;
$username31 = <DATA>;
$username32 = <DATA>;
$username32 = <DATA>;
$username33 = <DATA>;
@username34 = <DATA>;
close (DATA);

  if ($username13 eq ""){
  $username13 = "[URL unfurl="true"]http://www.mourningsunshine.com/picts/Face.jpg";[/URL]
  }
	
	$username32 = "$username32 jhj";
	chomp($username11);
	chomp($username13);

print <<END;

## print HTML with var's in it.

END


    $return;
}

thanks for your help, i'm sorry I've been posting so much, but i seen to be having a lot of problems with this forum, I've gotten probably 10 hours of sleep this week, so i'm not sure if its just because i don't know how to do it. or that i'm just tired.

thank you sooo much for your time, i would still be try'n to figure out my first question still if it were not for all of you.

if you want to see where all your code is going check here:
 
YAY! I'm so happy!

i found out what was wrong, it was putting a space in the file name. just added..

Code:
	my $space = " ";
	my $no_space = "";
	$Readusername =~ s/$space/$no_space/;

works perfect...
thanks again for showing me how to use: =~ s/$space/$no_space/

thnaks,
~Nate_Bro
 
Code:
$string=~s/\s+//g;
replaces all spaces with no space
your code would render
Code:
"A  dog walked to the   barn"
as
Code:
"A dog walked to the   barn"

The /g modifier means do it for all spaces in the string, but the code above will render
Code:
"Adogwalkedtothebarn"

Just a pointer
--Paul




cigless ...
 
Just another pointer:
It's generally not a good idea to use DATA as the name of your filehandle when opening a file. DATA is actually a special filehandle in Perl and it's liable to confuse people when they see it being used in a different context than normal.

Also, rather than creating a pile of $usernameXX variables, you'd be better off using an array. Firstly, it saves typing (always a good thing) but more importantly, it makes your code more future-proof (i.e. you don't have to add another line for a new username variable if another user is added to your data file.

It also discourages the use of soft references. You're not using them in that code at the moment but with lots of scalars that are related and similarly named, it's all too tempting to go down that road. There's a good page on why soft references are bad here if you're interested.
 
wow thanks guys, this is all good stuff, and much needed. i learn so much from you all. and it makes it so easy to, once i know about something new all i have to do is just look it up in my book.

i'm really glad for the /g modifier! i was having to run my code through an if statement to get out all the spaces, thank you sooooooooooo much! you don't know how much you all have helped me out here!

and thanks for telling me about the soft references, i was rushing and did it, i need to just take more time and do it right, cuzz i don't want to make it a bad habit like you said.

thanks agin for all your help and time!


thank you!
~Nate_Bro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top