Escaped $ in Strings has a strange behavior

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Escaped $ in Strings has a strange behavior

David Anthoff

I get

 

julia> x = "I have \$100 in my account"

"I have \$100 in my account"

 

Shouldn’t the backlash not appear in the output? If then do

 

println(x)

 

the backlash doesn’t show, but I would have expected that it also doesn’t show if I just eval the string.

 

Am I missing something basic here?

 

Thanks,

David

Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

c.d. mclean

in the terminal:

julia> a = "i have \$100 ..."                                                                                                                                    
"i have \$100 ..."
 
julia> aa = eval(a)                                                                                                                                              
"i have \$100 ..."
 
julia> a == aa                                                                                                                                                   
true
 
julia> b = println(a)                                                                                                                                            
i have $100 ...
 
julia> b == a                                                                                                                                                    
false


so, more "stuff" is happening
within the println() function ...

Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

Yichao Yu
On Mon, Nov 7, 2016 at 10:06 PM, cdm <[hidden email]> wrote:
>
> in the terminal:
>
> julia> a = "i have \$100 ..."
> "i have \$100 ..."
>
> julia> aa = eval(a)
> "i have \$100 ..."

^^ FWIW, this is a no-op

>
> julia> a == aa
> true

^^ Which should make this not very surprising.

>
> julia> b = println(a)
> i have $100 ...

`println` prints it, and does not return the string it prints

>
> julia> b == a
> false
>

^^ Which is why this does not mean anything at all.

>
>
> so, more "stuff" is happening
> within the println() function ...

No. println/print output the string, show/display prints it as an
object and do so in a way that shows more detail about the string
itself.
Note that the quote isn't part of the string either so it shouldn't be
surprising that the character `$` is printed as `\$` when showing a
quoted string.

>
>
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

c.d. mclean


On Monday, November 7, 2016 at 7:13:03 PM UTC-8, Yichao Yu wrote:
> so, more "stuff" is happening
> within the println() function ...

No. println/print output the string, show/display prints it as an
object and do so in a way that shows more detail about the string
itself.
Note that the quote isn't part of the string either so it shouldn't be
surprising that the character `$` is printed as `\$` when showing a
quoted string.

which was my point, print() and println() are doing "stuff" to the string,
else they would return the string as defined and as represented by
show() ... 
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

Yichao Yu
On Mon, Nov 7, 2016 at 10:16 PM, cdm <[hidden email]> wrote:

>
>
> On Monday, November 7, 2016 at 7:13:03 PM UTC-8, Yichao Yu wrote:
>>
>> > so, more "stuff" is happening
>> > within the println() function ...
>>
>> No. println/print output the string, show/display prints it as an
>> object and do so in a way that shows more detail about the string
>> itself.
>> Note that the quote isn't part of the string either so it shouldn't be
>> surprising that the character `$` is printed as `\$` when showing a
>> quoted string.
>
>
> which was my point, print() and println() are doing "stuff" to the string,
> else they would return the string as defined and as represented by
> show() ...

fwiw, show does more stuff on the string than either print or println.
None of them return anything even though print and println does not do
any stuff on the string.
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

c.d. mclean
On Monday, November 7, 2016 at 7:23:10 PM UTC-8, Yichao Yu wrote:
fwiw, show does more stuff on the string than either print or println.
None of them return anything even though print and println does not do
any stuff on the string.

so in the course of what was defined above,
was "a" defined as an "object" or a "string" ?
 
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

Yichao Yu
On Mon, Nov 7, 2016 at 10:28 PM, cdm <[hidden email]> wrote:

> On Monday, November 7, 2016 at 7:23:10 PM UTC-8, Yichao Yu wrote:
>>
>> fwiw, show does more stuff on the string than either print or println.
>> None of them return anything even though print and println does not do
>> any stuff on the string.
>
>
> so in the course of what was defined above,
> was "a" defined as an "object" or a "string" ?
>

Both? I don't see the difference. I'm only saying that `show` does not
simply write the string to the stream, but prints the representation
of the string object instead, which is the quoted string.
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

c.d. mclean
In reply to this post by c.d. mclean
to your point (in the same session):

julia> show(b)                                                                                                                                                   
nothing


On Monday, November 7, 2016 at 7:28:38 PM UTC-8, cdm wrote:
On Monday, November 7, 2016 at 7:23:10 PM UTC-8, Yichao Yu wrote:
fwiw, show does more stuff on the string than either print or println.
None of them return anything even though print and println does not do
any stuff on the string.

so in the course of what was defined above,
was "a" defined as an "object" or a "string" ?
 
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

Yichao Yu
On Mon, Nov 7, 2016 at 10:31 PM, cdm <[hidden email]> wrote:
> to your point (in the same session):
>
> julia> show(b)
> nothing

Yes? the return value of print/println/show has nothing to do with the
object being printed.

>
>
>
> On Monday, November 7, 2016 at 7:28:38 PM UTC-8, cdm wrote:
>>
>> On Monday, November 7, 2016 at 7:23:10 PM UTC-8, Yichao Yu wrote:
>>>
>>> fwiw, show does more stuff on the string than either print or println.
>>> None of them return anything even though print and println does not do
>>> any stuff on the string.
>>
>>
>> so in the course of what was defined above,
>> was "a" defined as an "object" or a "string" ?
>>
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

c.d. mclean
In reply to this post by Yichao Yu

On Monday, November 7, 2016 at 7:31:52 PM UTC-8, Yichao Yu wrote:

Both? I don't see the difference. I'm only saying that `show` does not
simply write the string to the stream, but prints the representation
of the string object instead, which is the quoted string.

julia> dump(a)                                                                                                                                                   
String "i have \$100 ..." 


i hope to find a list of decorators,
in addition to double quote and
back slash and single quote,
which are canonically "un-decorated"
by print()/println() ...

many thanks
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

Yichao Yu
On Mon, Nov 7, 2016 at 10:48 PM, cdm <[hidden email]> wrote:

>
> On Monday, November 7, 2016 at 7:31:52 PM UTC-8, Yichao Yu wrote:
>>
>>
>> Both? I don't see the difference. I'm only saying that `show` does not
>> simply write the string to the stream, but prints the representation
>> of the string object instead, which is the quoted string.
>
>
> julia> dump(a)
> String "i have \$100 ..."
>
>
> i hope to find a list of decorators,
> in addition to double quote and
> back slash and single quote,
> which are canonically "un-decorated"
> by print()/println() ...

Just to be clear. print/println does not "un-decorate" anything at
all. It's `show`/`display`/`dump` "decorate" (escape/quote) it.

>
> many thanks
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

c.d. mclean


On Monday, November 7, 2016 at 7:51:34 PM UTC-8, Yichao Yu wrote:
Just to be clear. print/println does not "un-decorate" anything at
all. It's `show`/`display`/`dump` "decorate" (escape/quote) it.

perhaps the help docs are not clear then ... ?

  print(x)
 
  Write (to the default output stream) a canonical (un-decorated) text representation of a value if there is one, otherwise call show. The representation used
  by print includes minimal formatting and tries to avoid Julia-specific details. 
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

Yichao Yu
On Mon, Nov 7, 2016 at 10:55 PM, cdm <[hidden email]> wrote:

>
>
> On Monday, November 7, 2016 at 7:51:34 PM UTC-8, Yichao Yu wrote:
>>
>> Just to be clear. print/println does not "un-decorate" anything at
>> all. It's `show`/`display`/`dump` "decorate" (escape/quote) it.
>
>
> perhaps the help docs are not clear then ... ?
>
>   print(x)
>
>   Write (to the default output stream) a canonical (un-decorated) text
> representation of a value if there is one, otherwise call show. The
> representation used
>   by print includes minimal formatting and tries to avoid Julia-specific
> details.

The doc string says that the function prints the form that is
`un-decorated` i.e. not decorated, different from show, which does
`decorate` it.
Reply | Threaded
Open this post in threaded view
|

Re: Escaped $ in Strings has a strange behavior

David Anthoff
Ah, thanks for the answer, that does make sense.

> -----Original Message-----
> From: [hidden email] [mailto:julia-
> [hidden email]] On Behalf Of Yichao Yu
> Sent: Monday, November 7, 2016 8:02 PM
> To: Julia Users <[hidden email]>
> Subject: Re: [julia-users] Re: Escaped $ in Strings has a strange behavior
>
> On Mon, Nov 7, 2016 at 10:55 PM, cdm <[hidden email]> wrote:
> >
> >
> > On Monday, November 7, 2016 at 7:51:34 PM UTC-8, Yichao Yu wrote:
> >>
> >> Just to be clear. print/println does not "un-decorate" anything at
> >> all. It's `show`/`display`/`dump` "decorate" (escape/quote) it.
> >
> >
> > perhaps the help docs are not clear then ... ?
> >
> >   print(x)
> >
> >   Write (to the default output stream) a canonical (un-decorated) text
> > representation of a value if there is one, otherwise call show. The
> > representation used
> >   by print includes minimal formatting and tries to avoid
> > Julia-specific details.
>
> The doc string says that the function prints the form that is `un-decorated`
> i.e. not decorated, different from show, which does `decorate` it.