Tuesday, October 9, 2007

@Transform as Forall-loop

I didn't think this fit into the "Complex transforms".

To use @Transform as Forall loop, simply add a @Do inside @Transform, do whatever you want with the items, and have a return-value for @Transform (@Nothing,"", etc).

Example:

list := "Tommy|Valand" : "Ford|Prefect";

@Transform( list ; "item" ; @Do(
    html := html : "<tr><td>" :
    @Word( item ; "|" ; 1 ) : "</td><td>" :
    @Word( item ; "|" ; 2 ) : "</td></tr>";

    item
    )
);
@Implode( "<table>" : html : "</table>" ; "" )


The above example isn't good, as using @For actually results in less code:

list := "Tommy|Valand" : "Ford|Prefect";

@For( i := 1 ; i <= @Elements(list) ; i := i + 1;
    html := html : "<tr><td>" :
    @Word( list[i] ; "|" ; 1 ) : "</td><td>" :
    @Word( list[i] ; "|" ; 2 ) : "</td></tr>";
);
@Implode( "<table>" : html : "</table>" ; "" )


When concatenating large strings, you'll get better performance connecting the "snippets" in a list ( list := list : item ), than using regular string-concatenation ( string := string + item )

0 comments: