Johnson, 

I'm sorry I never got an earlier chance to respond to this. =(

What happens in your code (shortened below)
[% c = 0; FOREACH var = s.split(";"); array.$c = var; c = c + 1; END; %]
is that you wind up with an array.1 and array.2 both being references
to the same variable. Basically, TT (and perl) reuse the same variable
storage each time through the loop, and when you assign the variable
directly it winds up saving not the value but a reference to the
variable's storage. Storage that is getting reused over and over.

So you wind up with every value in the array linked to the same single
piece of storage and so they are all the same. Worse, if you change
one, you change them all. That can be a good thing when you expect it
but obviously it isn't what you want most of the time.

The push method properly creates new storage and clones the value,
doing what you wanted.

And before you ask, yes it surprised me that your code had that
effect since I would have expected it to work too. I dunno if
something can be done about it, I kinda feel like it violates
the "least surprise" principle but I don't have enough feel for
the Stash.pm methods to know why it happens that way.

--mark

> -----Original Message-----
> From: Johnson Colin (KnowledgePool) [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, April 30, 2002 3:23 AM
> To: [EMAIL PROTECTED]
> Subject: RE: [Templates] Problem with split (...maybe)
> 
> 
> Mark
> 
> Thanks for your help with this, I'm not sure I understand 
> exactly what I was
> doing wrong because I was getting the right value in val when 
> I printed it
> out, it just wasn't getting put onto the list properly.  
> Also, if I use a
> push (thanks Andy - didn't know you could do that) onto the 
> list it does
> work using exactly the same loop:
> 
> [%- FOREACH val = list_string.split(';') -%] 
>       [%- mylist.push(val) -%]
>       [%# mylist.$counter = val -%] 
>       [%#- counter = counter + 1 -%]
> [% END -%] 
> 
> Does this mean that I cannot use [% mylist.$counter = val -%] 
> as a way of
> populating a list?
> - at least I don't think there is a problem with split now!
> 
> Thanks for the help
> 
> Colin
> 
> -----Original Message-----
> From: Mark Mills [mailto:[EMAIL PROTECTED]] 
> Sent: 2002-mm-29 17:29
> To: Johnson Colin (KnowledgePool); [EMAIL PROTECTED]
> Subject: RE: [Templates] Problem with split (...maybe)
> 
> 
> You aren't using split wrong, you are using "val" wrong. =) You are
> suffering from the classic error of putting the same 
> reference into an array
> with each pass through your FOREACH loop.
> 
> I'd recommend you do:
> [% mylist = list_string.split(';') -%]
> [% FOREACH val = mylist %]
>   data[[% val %]] [[% loop.count %]]
> [% END -%]
> 
> --mark
> 
> > I think I may have found a bug when using the split 
> function because I 
> > believe that I am using it according to theTemplate 
> Documentation, but 
> > I could be doing something really stupid here. 
> Unfortunately I do not 
> > have the knowledge of the Template code to find out if it 
> really is a 
> > bug - I have checked the split function in Stash.pm and this is 
> > returning an array correctly and this is about as far as I 
> can go.  If 
> > anyone can see something really daft in this code, let me know.
> >
> > [%- FOREACH val = list_string.split(';') -%] 
> >         data[[% val %]] [[% counter %]] 
> >         [% mylist.$counter = val -%] 
> >         [%- counter = counter + 1 -%]
> > [% END -%] 
> >
> > I can, however, create an array correctly like this...
> > [% mylist_too = list_string.split(';') -%] 
> > If anyone can see anything wrong in the code, it would be much
> > appreciated. 
> 
> _______________________________________________
> templates mailing list
> [EMAIL PROTECTED]
> http://www.template-toolkit.org/mailman/listinfo/templates
> 


Reply via email to