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

reading and assigning files to variables

Status
Not open for further replies.

perlone

Programmer
Joined
May 20, 2001
Messages
438
Location
US
Hi,

I have a file called news.txt and it contains like this:

1. Message 1
2. Message 2

open(F, "news.txt");
@news= <F>;
close(F);

I want to assign the whole message to a variable. So I did the following:

foreach $message (@news) {
$news = qq($message);
}

But the variable $news is reading only the first line. But I want to read the whole text and assign them to one
variable which is $news. Any ideas?

-Aaron
 
Arron:

Try this:

[tt]
foreach $message (@news) {
$news .= $message;
}
[/tt]

I think that is what you mean.

The . is the concatenation symbol.

Hope this helps,

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Thanks alot, vcherubini. It' works like a charm :-)
 
Little problem: It works but it displays like this:

Justing Testing out the News Board! Server will reset 12:00 ever Zones.

But I want them to displays each message in each new lines like this:

Justing Testing out the News Board!
Server will reset 12:00 ever Zones.

This is also how it looks inside the news.txt. Do you know how to display each them in new line? I did tried perl's &quot;\n&quot; but it didn't quite worked.

-Aaron
 
Try this

foreach $message (@news) {
$news .= $message;
$news .= &quot;\n&quot;;
}


 
There are two good ways to do this, neither of which includes loops or the (expensive) concatenation operator.
Code:
my $news = join '',<F>;
or
Code:
undef $/;
my $news = <F>;
$/ = &quot;\n&quot;;

The first method is the more extensible (the '' can be changed to <BR> if you want to keep line breaks in web output). The second method is faster.

Hope this helps,

brendanc@icehouse.net
 
sophisticate:

If the news.txt file contained the following text:
[tt]
Justing Testing out the News Board!
Server will reset 12:00 ever Zones.
[/tt]

Wouldn't:
[tt]my $news = <F>;[tt]
Work just the same?

I tried it and it worked on my Activestate version of Perl.
Just wondering?
-Vic

vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
It shouldn't. What should happen is that you would get the first line of the file instead of the whole thing.

The $/ special variable is the record separator (typicall equal to a newline) which is what a filehandle in list context is divided with... So, if you undef the $/ variable and read the filehandle, there's nothing to split the array and everything gets assigned to the first element... Then you can safely assign that element to a scalar.

Hope this helps,

brendanc@icehouse.net
 
Make sure that you have those statements between the open(F,) and close(F) statements. If you've already closed the filehandle, neither of the options I presented will work.

brendanc@icehouse.net
 
I did the following:


open(F, &quot;$basedir/$email/news.cgi&quot;);
@news = <F>;
foreach $message (@news) {
$news .= $message;
$news .= &quot;\n&quot;;
}
close(F);
 
It works but the new line thing is not working at all (&quot;\n&quot;);
 
If you like you can send me your code ill be happy to look at it and give it a try, ill be glad to spare a few minutes to help you.

PS. Send me a example data file as well to make things easier.

ilija_b@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top