Re: help required regarding saving values in a sequence during a loop

Daniel Dekany <[email protected]> Wed, 16 Jun 2004 18:12:02 +0200
Newsgroups gmane.comp.text.fmpp
Message-ID <[email protected]>
Wednesday, June 16, 2004, 11:52:36 AM, Asad Niazi wrote:

> Hello!
> I have a problem regarding saving values in a sequence (using the add/set 
> fmpp pphash) during a loop (inside the <#list> ). I intended to increase the 
> index by increasing the counter x e.g. index = x, where x is increasing in 
> every run of the loop.
>
> The actual problem is that i dont understand the difference of "add" and 
> "set" fmpp pphash when they are used to add values in a sequence.

As the documentation says, "add" inserts a new item, while "set"
replaces an already existing item at the specified index.

> For your help let me outline what i am trying to do: e.g the Writable
> seqeunce will be NamedNumber it will have 2 subelements or "keys";

Sequences has no "keys". Only hashes has keys. Sequences has indexes.

> "Name" and "Number" they can store values for lot of "names" and
> "numbers" (which makes it a sequence)
>
> What I have currently done is that i have tried different combinations of 
> "set" and "add" on this writable sequence.
>
> <@pp.add        seq=NamedNumber index=x value={"Name": whatevervalue} />
>
> should this work??

Yes... as far as the variable values are correct, i.e. x is an integer
with correct value, etc.

> or should i use??
>
> <@pp.set        seq=NamedNumber index=x key="Name"      value=whatevervalue />

It's surely bad, as sequences has no keys.

> somehow both of these are not working. As i told you before i am not very 
> clear with the difference of set and add pphashes and the use of index 
> parameter in them.

It seems that the root of the problem that you don't understand what are
sequences and what's a hashes. You may should read the FreeMarker Manual
about this, if you didn't. Anyway... I try to explain it now. Both data
structure is a collection. They store multiple values of *any* type.
Now, to access (read/modify/delete/etc.) a value in a collection somehow
you must use the unique address of the value (unique in the collection).
In the case of hashes addresses are strings that are called keys. In the
case of sequences addresses are integer numbers that are called indices.
The indices has the restriction that they are continuos and always
starting from 0, so for example if the sequence length is 4, then the
available indexes are 0, 1, 2, 3 (so if you "pp.add" a value with index
2, then the the size will grow to 5, and the index of the values after 2
will increase by 1).

Now, in your case you just want to address words (the names) with an
integer index, so you don't need hashes at all (seems that you tried to
build a sequence of hash values... you can, but I guess you don't need
to). So what you want could work somehow like this:

  <#assign NamedNumber = pp.newWritableSequence()>
  <@pp.add seq=NamedNumber value="Foo" />
  <@pp.add seq=NamedNumber value="Bar" />
  <@pp.add seq=NamedNumber value="Baaz" />
  Now test it:
  <#list NamedNumber as n>
  - ${n}
  </#list>
  ${NamedNumber[1]}

(See the @pp.add documentation about the meaning of missing "index"
parameter.) And this outputs:

  Now test it:
  - Foo
  - Bar
  - Baaz
  Bar

Of course, in the above template could be implemented without writable
sequences as well:

  <#assign NamedNumber = ["Foo", "Bar", "Baaz"]>

> I am desperately looking for help in this matter. I ll be looking forward to 
> your reply. Thanking in anticipation.

Well, *if* you didn't do it so till now, use the FMPP and the FreeMarker
Manual intensively... they are quite detailed, it's all described there.

> Best Regards
> Asadullah Khan Niazi

BTW, which country?

-- 
Best regards,
 Daniel Dekany




-------------------------------------------------------
This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND