Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > expression eats newline

Reply
Thread Tools

expression eats newline

 
 
Gerard Oberle
Guest
Posts: n/a
 
      07-31-2003
The following Perl script

print ((time/86400) % 4), " foo foo foo foo\n";
print "bar\n";

produces the output

0bar

What happened to " foo foo foo foo\n"?

print 12, " foo\n";

prints '12 foo' and a newline. Ergo, ((time/86400) % 4) should just
be another numeric expression, just like 12, shouldn't it? Or am I
missing something really stupid?

Thanks in advance.
- Jerry Oberle
perl -e 'printf "mailto%c%s%c%s%cchase%ccom%c", 58, "Gerard", 46,
"Oberle", 64, 46, 10;'
 
Reply With Quote
 
 
 
 
Purl Gurl
Guest
Posts: n/a
 
      07-31-2003
Gerard Oberle wrote:

(snipped)

> The following Perl script


> print ((time/86400) % 4), " foo foo foo foo\n";
> print "bar\n";


> produces the output


> 0bar


> What happened to " foo foo foo foo\n"?



You are neglecting Perl command formats, which are
typically, command() as with print().

Your parentheses are the problem. Perl is doing
precisely what your syntax indicates to do.

Compare these to your syntax:

print time/86400 % 4, " foo foo foo foo\n";
print "bar\n";

print (((time/86400) % 4), " foo foo foo foo\n");
print "bar\n";


Purl Gurl
--

#!perl -w

print ((time/86400) % 4), " foo foo foo foo\n";
print "bar\n";


PRINTED RESULTS:
________________

print (...) interpreted as function at test.pl line 3.
Useless use of a constant in void context at test.pl line 3.
0bar
 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      08-01-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

http://www.velocityreviews.com/forums/(E-Mail Removed) (Gerard Oberle) wrote in
news:(E-Mail Removed) om:

> The following Perl script
>
> print ((time/86400) % 4), " foo foo foo foo\n";


Perl has a rule when it comes to interpreting statements like this:

"if it looks like a function, it IS a function."

So, because of your parentheses, it is as if you wrote:

$x = print( (time/86400)%4 );
$x, " foo foo foo foo\n";

The solution is either to add a + before the first parenthesis:

print +((time/86400) % 4), " foo foo foo foo\n";

or to add another set of parentheses:

print (((time/86400) % 4), " foo foo foo foo\n");


For your future reference, comp.lang.perl is a defunct newsgroup. General
perl questions should be posted to comp.lang.perl.misc; you'll get a better
response there.
- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPynqOmPeouIeTNHoEQIu7ACghrgjRzXwWjijpWA+ihwOUK SSZQEAnRAr
dlcsK2oa9GH1M3p1WYE8AUVW
=2btu
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Purl Gurl
Guest
Posts: n/a
 
      08-01-2003
Gerard Oberle wrote:

(Topic is use of parentheses with Perl print)

(snipped)

> As I suspected, I was missing something stupid.


Not really stupid. It is very rare to read Perl code
which employs C style parentheses with a print command.
Almost all books and reference sources omit parentheses
within examples of print commands. Most do not know of
this until a rather quirky problem pops up.


> As for comp.lang.perl being defunct, I am not sure how one can
> determine that. groups.google.com still carries it, and there are
> lots of recent postings, so I assume people are still using it.


Many if not most news servers carry this group and alt.perl as well.
Humors me when reading those proclaiming this group does not exist.
Quite the oxymoron, this is, posting to a group which does not exist.


Purl Gurl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Regular expression that doesn't recognize newline Antonio ASP .Net 0 01-19-2005 09:23 AM
Re: ASP.NET eats memory too much George Ter-Saakov ASP .Net 0 06-30-2003 03:02 PM
Re: ASP.NET eats memory too much Kevin Spencer ASP .Net 0 06-30-2003 03:01 PM



Advertisments