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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange problem with join() and newline characters ... 1

Status
Not open for further replies.

ryorinin07

Programmer
May 9, 2002
3
US
... Part of a script I wrote involves reading a file into an array then putting that array into one character string. Since I wasn't sure whether there was a shortcut to do that (just read a file into a scalar, no array), here's what I did:

open(f, &quot;$filename&quot;); @file = <f>; close(f);
$data = join(/\n/, @file);

... and this would usually work with no problems. But I've noticed on occasion that the newline characters will get a '1' tacked onto them, so if on one run it outputs:

This is the file,
with good newlines
and no oddities.

... then the problem occurs some other time and I get ...

This is the file,
1with good newlines
1and no oddities.

Needless to say, this really messes up things when I try to use the script to send back some HTML code. I was thinking it might have something to do with the /\n/ in the call to join(), but I don't see why that would cause problems some of the time, and not others. Can anyone explain why this is happening? I'm using the latest version of ActivePerl, if that helps. Thanks.
 
Code:
open(f, &quot;$filename&quot;);
@file = <f>;
close(f);
$data = join(/\n/, @file);

# ...&quot;the script to send back some HTML code&quot;....
# then i guess u just
print $data
# after ?

# here another way of doing so
# good practice to place a die below
open(f, &quot;$filename&quot;) || die &your_dead_subroutine;
 while (<f>) {
  print &quot;<td colspan=1>a line content $_</td>&quot;;
 }
close(f);

# drains less ...
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Yeah, I just print $data after that. ... I probably should put a die statement after that open(), though. :)

Oh, and I put the file data into a variable because I do a lot of parsing and whatnot with the data, then send back the finished result.
 
can u post your code ?
my native language is code [lol] ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
$/ = undef;
open(f, &quot;$filename&quot;);
$file = <f>;
close(f);

$/ is perls input record separator. It normally is set to the newline characher. By setting it to undef you can assign the entire contents of the filehandle to a string.

 
Wow, that makes things easier, doesn't it? Thanks. Still, I'd like to figure out why it is that 1's keep getting added ... anyway, once I get more time I'll post the code here. Right now, all I can provide is a sort of paraphrase ...

#!/usr/bin/perl
#then some code, then ...

$file = &quot;foo.bar&quot;;
open (f, &quot;$file&quot;) || die &quot;Can't open file.&quot;;
@file = <f>; close(f);

$data = join(/\n/, @file);

#then some code to change a few words in the $data, then ...

print $data;

... but since the join() statement uses the given character to stick all the elements of the array together, perhaps I should insert some code that gets rid of the trailing newline characters in said elements?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top