Re: append and &

new topic     » goto parent     » topic index » view thread      » older message » newer message

nice tutorial, thanks... I've been getting mighty confused on indexing into
the tables i've been creating.


george
----- Original Message -----
From: "Irv Mullins" <irvm at ellijay.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: append and &


>
> On Thu, 21 Mar 2002 06:43:31 -0500
> George Walters <gwalters at sc.rr.com> wrote:
>
> >
> > Can someone explain the difference between these two statements
> > -----------------------------
> > sequence a
> > a = "a"
> > for i = 1 to 10
> >     a = append(a,"a")
> > end for
> > ----------------------------
> > sequence a
> > a = "a"
> > for i = 1 to 10
> >     a = a & "a"
> > end for
> > ----------------------------
>
> Ignoring the fact that neither example works (you need a 'do' in there!:),
> the first creates an 'array' of ten 1-character strings:
"a","a","a","a"......
> while the second simply appends characters to the original string:
> "aaaaaaaaaaa"
>
> You can print the second as a string: puts(1,a), but you have to index
into the first before you can print an element.
> print a[2] => "a"
>
> More interesting would be:
> a = "Ant"
> a &= "Cat"
> a &= "Dog"
> a &= "Rat"
> This does a standard string concatenation: puts(1,a) => "AntCatDogRat"
>
> However:
> a = "Ant"
> a = append(a,"Cat")
> a = append(a,"Dog")
> a = append(a,"Rat")
>
> does NOT result in what I would expect: an array of 3 strings
["Ant","Cat","Dog","Rat"],
> instead, it creates a string containing nested sub-strings:
> ["Ant{"Cat","Dog","Cat"]"],
> so trying to print a[2] gives 'n', not "Cat" as expected. i.e:
> a[1] => 'A'
> a[2] => 'n'
> a[3] => 't'
> -- ready for the surprise?
> a[4] => "Cat"
> a[5] => "Dog"....
>
> To create an array of strings, you need to declare the first as follows:
> a = {"Ant"} -- note the braces
> a = append(a,"Cat")
> a = append(a,"Dog")...
>
> now, puts(1,a[2]) => "Cat"
>
> The exact same results can be had from:
> a = {"Ant"}
> a &= {"Cat"}
> a &= {"Dog"} ...
>
> Regards,
> Irv
>
>
>
>

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu