Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Continuation of string literals

4 views
Skip to first unread message

Chris Cross

unread,
Oct 19, 2009, 10:02:51 PM10/19/09
to
The following works:

write("The value is: ", this_value , " will _
be displayed on screen" )

but this wont:

write("The value is: ", this_value ,_
"will not.")

It appears that in order to be able to use Icon expressions within
strings that have been "continued" with an underscore, the said
expression must /not/ immediately precede the underscore. Rather, each
"continued" line must begin and end with a quoted string.

Can someone confirm this observation as being the correct Icon
behavior, or refute it and set me straight.
TIA...
--
Chris

Patrick Scheible

unread,
Oct 20, 2009, 12:20:16 AM10/20/09
to
Chris Cross <sidney.r...@gmail.com> writes:

Maybe I'm not sure what you're trying to do. this_value is a
variable, not a string literal.

The underscore at the end of a line inside a string literal continues
it on the next line. If it's not inside string literal, you can break
the line between any two tokens:

write( "The value is: ", this_value,

"will not" )

or

write( "The value is: ",

this_value, "will not" )

or even

write(
"The value is: ",
this_value,

"will not"
)

-- Patrick

Chris Cross

unread,
Oct 20, 2009, 12:35:59 AM10/20/09
to
On Oct 19, 10:20 pm, Patrick Scheible <k...@zipcon.net> wrote:

Ok! Bad example! Let's re-write it as follows:

write("The value is: ", myVar , " will _


be displayed on screen" )

but this wont:

write("The value is: ", myVar ,_
"will not.")

So I'm trying to determine whether or not variable expansion will
occur inside an underscore continued string. As I said in my original
post, Case1 works! Case2 does not! However, I may be doing something
wrong and not even know it. So I simply want to confirm this behavior.

Marc Espie

unread,
Oct 20, 2009, 3:46:56 AM10/20/09
to
In article <9af81cd0-c39d-4951...@f18g2000prf.googlegroups.com>,

Chris Cross <sidney.r...@gmail.com> wrote:
>Ok! Bad example! Let's re-write it as follows:
>
>write("The value is: ", myVar , " will _
> be displayed on screen" )
>
>but this wont:
>
>write("The value is: ", myVar ,_
> "will not.")
>
>So I'm trying to determine whether or not variable expansion will
>occur inside an underscore continued string. As I said in my original
>post, Case1 works! Case2 does not! However, I may be doing something
>wrong and not even know it. So I simply want to confirm this behavior.

You're confused. Your use of the vocabulary is wrong. A string literal is just
that: a string that ISN'T interpreted, set between quotes: "like this".
An a variable is something different. There's NO variable expansion within
string literals in icon. A write such as
write("The value is: ", var, " or something");
has three arguments:
"The value is: " a string literal
var a variable
" or something" another string literal.

There's no need for a _ continuation to simply glue function arguments
together. As the previous poster pointed out, as long as your function
call isn't complete (ending parenthesis on the start line), you're fine
and you can continue across lines as you want.

_ is only useful if you want to create a very long string over several
lines.

Such as:

var := "There's no need for a _ continuation to simply glue function arguments_
together. As the previous poster pointed out, as long as your function_
call isn't complete (ending parenthesis on the start line), you're fine_
and you can continue across lines as you want."

Chris Cross

unread,
Oct 20, 2009, 8:27:07 AM10/20/09
to
On Oct 20, 1:46 am, es...@lain.home (Marc Espie) wrote:
> In article <9af81cd0-c39d-4951-bc71-a9d4db780...@f18g2000prf.googlegroups.com>,

> Chris Cross <sidney.reilley...@gmail.com> wrote:
>
> >Ok! Bad example! Let's re-write it as follows:
>
> >write("The value is: ", myVar , " will _
> > be displayed on screen" )
>
> >but this wont:
>
> >write("The value is: ", myVar ,_
> > "will not.")
>
> >So I'm trying to determine whether or not variable expansion will
> >occur inside an underscore continued string. As I said in my original
> >post, Case1 works! Case2 does not! However, I may be doing something
> >wrong and not even know it. So I simply want to confirm this behavior.
>
> You're confused. Your use of the vocabulary is wrong. A string literal is just

No! I'm not confused! Yes perhaps my Icon vocabulary is not up to par
yet. Your tone in this post sucks, BTW!

Let me explain what I'm after with a more lucid example for you:

write("<html><head><title>the Print Everything CGI Form</
title><isindex>_
<style type=\"text/css\"> .blue {color:rgb(0,128,255);}_
</style>_
</head><body><h2 class=\"blue\">Mini CGI Tutorial for Icon
hackers</h2>_
<p>Form using GET method:_
<p><form method=\"GET\" action=\"http://localhost/cgi-bin/
print_ev-Icon.cgi\">_
<p>Field 1<input name=\"FIELD1\"> Field 2<input name=
\"FIELD2\"> <input type=\"submit\" value=\"Send to CGI\">_
</form>_
<p>Form using POST method:_
<form method=\"POST\" action=\"http://localhost/cgi-bin/
print_ev-Icon.cgi\">_
Field 1<input name=\"FIELD1\">_
Field 2<input name=\"FIELD2\">_
<input type=\"submit\" value=\"Send to CGI\">_
</form></body></html>_
<p>This is an <b>ISINDEX</b> query<br> and your input was:
<b>", !g_args , "</b>")

The above is a snippet from a small CGI script that I'm playing with
in an effort to manipulate the CGI with Icon. You will notice that in
the last line of this long, underscored "continued" string, is the
Icon expression `!g_args'.

I want the Icon interpreter/compiler to evaluate this expression and
substitute the contents thereof, so that it may be returned by the web
server. Are you with me so far?

[sidebar] In another post to this c.l.i. I commented about Perl's
"here document" (Google it) and asked if there was an Icon equivalent.
No answers were forthcoming, but in the interim, I had discovered the
Icon underscore. Perhaps there is still an Icon equivalent?[/sidebar]

Sooo! I understand what a string is - as well as a variable. My
observations so far on the use of variables in the context of an
"underscore-continued" string has been that:
1. the variable/expression is indeed evaluated ONLY if does NOT
immediately precede the underscore.

I was simply asking if that behavior is correct. Bottom line: I want
to get away from having to type /a lot/ of
write("...")
statements when doing CGI stuff.

and Marc.... if you persist with your sarcastic, arrogant tone in
replying to my posts, you WILL be "killfiled" by me. Have a nice day!

Patrick Scheible

unread,
Oct 20, 2009, 12:49:38 PM10/20/09
to
Chris Cross <sidney.r...@gmail.com> writes:

No, !g_args is *not* in the string literal. The string literal ends
with the double quote just before !g_args.

Unlike most shell scripts, in Icon no variable expansion occures
within string literals.

You use the _ at the end of a line *within* a string literal to extend
it over several lines. Outside a string literal, you can break lines
anywhere you want (not in the middle of a token) without a
continuation character.

You are calling write() with three arguments, 1. a long string
literal, that you have divided between lines using _ at the end of
lines. 2. !g_args, an expression that evaluates to a string. 3. a
short string literal.

> I want the Icon interpreter/compiler to evaluate this expression and
> substitute the contents thereof, so that it may be returned by the web
> server. Are you with me so far?

I think so. It looks like it should work as you have written it.



> [sidebar] In another post to this c.l.i. I commented about Perl's
> "here document" (Google it) and asked if there was an Icon equivalent.
> No answers were forthcoming, but in the interim, I had discovered the
> Icon underscore. Perhaps there is still an Icon equivalent?[/sidebar]

No, there's no Icon equivalent. The problem "here documents" are
solving in scripting languages doesn't really exist in Icon.
Scripting languages have rudimentary parsers which evaluate the script
line-by-line, and it's an unusual situation that requires special
syntax for a statement to continue for more than one line of source.
Also, in shell scripting languages, you stick variables into the
string and the shell expands them, and then there are more complicated
ways of quoting that (you hope) will expand what you want expanded and
not expand what you don't want expanded. In Icon, nothing gets
expanded within strings, and you build up the string you want from
literal strings and string expressions.

> Sooo! I understand what a string is -

It doesn't seem like you do, quite. A string literal is what's inside
the quotation marks. An expression can evaluate to a string and be an
argument to a write() function without being a string literal.
Expressions, including expressions that evaluate to strings, can be
formatted across lines freely.

> as well as a variable. My
> observations so far on the use of variables in the context of an
> "underscore-continued" string has been that:
> 1. the variable/expression is indeed evaluated ONLY if does NOT
> immediately precede the underscore.

I think you're making it more complicated than it really is. The
underscore as continuation character is an error outside a string
literal. You don't need an underscore (or anything else) to continue
statements from line to line outside string literals. A newline
(outside a string literal) is just white space.

> I was simply asking if that behavior is correct. Bottom line: I want
> to get away from having to type /a lot/ of
> write("...")
> statements when doing CGI stuff.

One write() statement can have as many arguments as you want. Some
arguments will be string literals, and some will be expressions that
evaluate to strings. The expressions can be formatted to fit your
lines breaking them anywhere you want. Long string literals that
extend over one line in the source code get continued with the
underscore at the end.

> and Marc.... if you persist with your sarcastic, arrogant tone in
> replying to my posts, you WILL be "killfiled" by me. Have a nice day!

It looked to me like he was trying to be helpful.

-- Patrick

Marc Espie

unread,
Oct 20, 2009, 12:56:20 PM10/20/09
to
In article <c2e37972-6845-43a7...@g22g2000prf.googlegroups.com>,

Chris Cross <sidney.r...@gmail.com> wrote:
>On Oct 20, 1:46 am, es...@lain.home (Marc Espie) wrote:
>>
>> You're confused. Your use of the vocabulary is wrong. A string literal is just
>
>No! I'm not confused! Yes perhaps my Icon vocabulary is not up to par
>yet. Your tone in this post sucks, BTW!

I persist, you're confused. You're mixing vocabulary from various languages,
and using vocabulary that doesn't pertain to Icon.

The previous remark was factual. If you take it as a personal insult, fine,
see if I care.

>and Marc.... if you persist with your sarcastic, arrogant tone in
>replying to my posts, you WILL be "killfiled" by me. Have a nice day!

Look, I took five minutes in explaining what was wrong with your perception
of things. Not more. That's not arrogance. That's terse. Next time, I won't
even take the time answering, so you won't have to killfile me.

carolet

unread,
Oct 20, 2009, 3:40:49 PM10/20/09
to

To save typing let's assume two short strings for a moment. You have

write("string 1", !g_args, "string 2")

One problem that you have here is that !g_args does not produce all of its
values in this situation. You need to make it generate all of its value.
Something like this:

write("string 1")
every write(!g_args)
write("string 2")

This is the important point. The underscore only has an effect inside a
quoted string, you don't seem to have understood that yet.

In your very long string you have three options.
The first is to write it as a really long unbroken string. The problem with
this is that it is hard to read.
Secondly break it into shorter strings. For example

write("<html><head>",
"<title>the Print Everything CGI Form</ title><isindex>",
"<style type=\"text/css\"> .blue {color:rgb(0,128,255);}",
"</style></head><body>",


"<h2 class=\"blue\">Mini CGI Tutorial for Icon

hackers</h2>",
"<p>Form using GET method:")


I've shortened what you had for simplicity. I would be inclined to do it
like this myself.
Note also that each item item is on a separate line, there is no need for
anything to indicate that the expression continues on to the next line.

Thirdly you can use the underscore to break the string. If you do that the
underscore replaces a quote-comma-quote, so you are only saving the typing
of two characters for each break. I rarely use the underscore tobreak a
string as I think it looks untidy, but each to his own.

By the way, the string that you are writing out has no newline characters in
it, so it will be printed as one long line. As you are worrying about long
lines, maybe you should consider adding some, eg

write("<html>\n<head>\n",
"<title>the Print Everything CGI Form</ title>\n<isindex>")

>You don't need an underscore (or anything else) to continue
> statements from line to line outside string literals. A newline
> (outside a string literal) is just white space.
>
>> I was simply asking if that behavior is correct. Bottom line: I want
>> to get away from having to type /a lot/ of
>> write("...")
>> statements when doing CGI stuff.
>
> One write() statement can have as many arguments as you want. Some
> arguments will be string literals, and some will be expressions that
> evaluate to strings. The expressions can be formatted to fit your
> lines breaking them anywhere you want. Long string literals that
> extend over one line in the source code get continued with the
> underscore at the end.
>
>> and Marc.... if you persist with your sarcastic, arrogant tone in
>> replying to my posts, you WILL be "killfiled" by me. Have a nice day!
>
> It looked to me like he was trying to be helpful.

Yes I thought he was trying to be helpful too.

>
> -- Patrick

--

CaroleT


Steve Wampler

unread,
Oct 23, 2009, 1:13:04 PM10/23/09
to
On Mon, 19 Oct 2009 19:02:51 -0700, Chris Cross wrote:

> The following works:
>
> write("The value is: ", this_value , " will _
> be displayed on screen" )
>
> but this wont:
>
> write("The value is: ", this_value ,_
> "will not.")

Hi Chris,

I'm not sure if you're happy with the explanations you've
seen and so thought I'd give a slightly modified version.

First, as Patrick said, Icon's write() function takes
an arbitrary number of arguments, each of which (in the
simple case!) should evaluate to a string. There is
no construction of a single string from those arguments,
they are simply written out, in order. In particular,
"," is an argument separator in Icon, not a "string
concatenation" operation. So in your first example,
write is passed three (separate) arguments: a literal
string, whatever string is the value of the this_value
variable, and a final literal string.

An underscore may be used to continue a *literal* string
(only!) across line boundaries. In the first example,
the underscore is within the last literal string and
so continues it across the line boundary. In the second
example, the underscore appears *outside* any literal
string, where it is a perfectly valid (if VERY odd)
variable name. So, you have a variable followed by a
string literal with no operator in between. That's
a syntax error.

Incidentally, I'm not a fan of using _ in string literals.
Modern computers are fast enough that it's rare that the
cost of using string concatenation instead of an embedded
underscore makes any difference. So you example could
also be written:

write("The value is: ", this_value, "will "||

"not")


Chris Cross

unread,
Oct 23, 2009, 4:58:29 PM10/23/09
to
On Oct 23, 11:13 am, Steve Wampler <swamp...@noao.edu> wrote:

[snip]

> Incidentally, I'm not a fan of using _ in string literals.
> Modern computers are fast enough that it's rare that the
> cost of using string concatenation instead of an embedded
> underscore makes any difference. So you example could
> also be written:
>
> write("The value is: ", this_value, "will "||
> "not")

You and CaroleT share the same opinion of using the underscore for
string continuation. For some reason, I assumed that the underscore
was a "line" continuation operator. Not!!

I agree with caroleT that simply breaking up a long stream of HTML
code into multi comma-separated string literals, wrapped in one write
(), is the way to go.

I was asking about the underscore thing in context of having Icon
render a whack of HTML, to pass on to a web server. Perl has a
shortcut for quoting a long stream of text across multiple lines. I
thought that the underscore was doing the same thing. Again... Not!!

Anyway, I've moved on. Now looking at backtracking a bit. It's slowly
coming together. Thanks for the input.
--
Chris

0 new messages