Array weirdness with referencing

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Array weirdness with referencing

Adam de Zoete-2
Having a problem with arrays and referencing, can anyone help out? Am I
doing something wrong?


var('lines') = (:'foo','bar ','no','"fun"','with',"'arrays'");
iterate($lines, local('e'));
  local('line_entry') = @#e;
  #line_entry -> trim;
  #line_entry -> beginswith('"') && #line_entry -> endswith('"')
        ? #line_entry -> replace('"','');
  #line_entry -> beginswith("'") && #line_entry -> endswith("'")
        ? #line_entry -> replace("'",'');
/iterate;
$lines;

=>

array: (arrays), (bar), (no), (fun), (with), (arrays)

What happened to my foo? In theory I would of thought this would work,
but I should be using foreach!

Many thanks,

Adam

--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Fletcher Sandbeck-3
On 4/16/09 at 3:12 PM, [hidden email] (Adam de Zoete) wrote:

>Having a problem with arrays and referencing, can anyone help
>out? Am I doing something wrong?
>
>
>var('lines') = (:'foo','bar ','no','"fun"','with',"'arrays'");
>iterate($lines, local('e'));
>local('line_entry') = @#e;
>#line_entry -> trim;
>#line_entry -> beginswith('"')   &&  #line_entry -> endswith('"')
>?   #line_entry -> replace('"','');
>#line_entry -> beginswith("'")   &&  #line_entry -> endswith("'")
>?   #line_entry -> replace("'",'');
>/iterate;
>$lines;
>
>=>
>
>array: (arrays), (bar), (no), (fun), (with), (arrays)
>
>What happened to my foo? In theory I would of thought this
>would work, but I should be using foreach!

The variable #e is already a reference.  If you simply use
#line_entry as the iterate variable then it will work as you expect.

var('lines') = (:'foo','bar ','no','"fun"','with',"'arrays'");
iterate($lines, local('line_entry'));
  #line_entry -> trim;
  #line_entry -> beginswith('"') &&  #line_entry -> endswith('"')
     ?   #line_entry -> replace('"','');
  #line_entry -> beginswith("'") &&  #line_entry -> endswith("'")
     ?   #line_entry -> replace("'",'');
/iterate;
$lines;

The problem with your code is that #line_entry is holding onto
the reference to the first element of the array.  If you add
this line right before /iterate it will detach the reference so
it doesn't hold onto it each time through the loop.

  #line_entry->detachreference;

[fletcher]

--
Fletcher Sandbeck                         [hidden email]
LassoSoft, LLC                          http://www.lassosoft.com


--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

James Harvard
In reply to this post by Adam de Zoete-2
There was a bug with array references in the past. Try explicity using ->detatchreference at the end of your code inside iterate.

Here's my test case from my bug report:

<?lassoscript

var:
 'ref'   = null,
 'var_1' = 'one',
 'var_2' = 'two',
 'var_3' = 'three',
 'var_4' = 'four',
 'names' = (array:'ref','var_1', 'var_2', 'var_3', 'var_4')
;

'<p><em>Initial values</em><br>ref = null<br>';
'var_1 = \'one\'<br>var_2 = \'two\'<br>var_3 = \'three\'<br>var_4 = \'four\'</p>';

loop: 4 ;

        $ref = @ (var:('var_' + loop_count)) ;

        '<p><em>Make $ref reference to $var_';
        loop_count;
        '</em>';

        iterate: $names, (var:'i') ;
                '<br>';
                $i;
                ' = \'';
                (var:$i);
                '\'';
        /iterate;

        '</p>';

/loop;

?>

--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Steve Upton
In reply to this post by Adam de Zoete-2
At 3:12 PM +0100 4/16/09, Adam de Zoete wrote:

>Having a problem with arrays and referencing, can anyone help out? Am I doing something wrong?
>
>
>var('lines') = (:'foo','bar ','no','"fun"','with',"'arrays'");
>iterate($lines, local('e'));
> local('line_entry') = @#e;
> #line_entry -> trim;
> #line_entry -> beginswith('"') && #line_entry -> endswith('"')
> ? #line_entry -> replace('"','');
> #line_entry -> beginswith("'") && #line_entry -> endswith("'")
> ? #line_entry -> replace("'",'');
>/iterate;
>$lines;
>
>=>
>
>array: (arrays), (bar), (no), (fun), (with), (arrays)

is this a nasty stress test on purpose?

anyway, the @ in the 3rd line means you are access the addres of the array entry rather than the array entry... doesn't it?

is that something you are doing on purpose?

regards,

Steve


--


--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Adam de Zoete-2
Steve Upton wrote:
> At 3:12 PM +0100 4/16/09, Adam de Zoete wrote:
>> Having a problem with arrays and referencing, can anyone help out? Am I doing something wrong?
>>
> is that something you are doing on purpose?

No, my mistake, more coffee required! I was assuming that as I defined
the local('line_entry') through each iteration it would automatically
re-reference #e, not realizing that #e was by reference anyway.

Thanks for setting me straight!

Adam

--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Bil Corry-3
In reply to this post by Adam de Zoete-2
Adam de Zoete wrote on 4/16/2009 9:12 AM:
> Having a problem with arrays and referencing, can anyone help out? Am I
> doing something wrong?

I know you got your answer, but since you mentioned ->forEach, I thought you might be interested to know you could have plugged your code into ->foreach:

        var('lines') = array('foo','bar ','no','"fun"','with',"'arrays'");
        $lines->foreach({
                local('line_entry') = @params->first;
                #line_entry -> trim;
                #line_entry -> beginswith('"')    &&    #line_entry -> endswith('"')
                        ?    #line_entry -> replace('"','');
                #line_entry -> beginswith("'")    &&    #line_entry -> endswith("'")
                        ?    #line_entry -> replace("'",'');
                return(true);
        });
       
        $lines;

Related, something I've been playing with is a simpler ->foreach member tag that allows you to act on every element in the array.  For example, this does more-or-less the same thing as your code above (the only difference is with my code below, a string can start with ' and end with " and have both removed):

        var('lines') = array('foo','bar ','no','"fun"','with',"'arrays'");
        $lines->each({element=string_replaceregexp(element,-find='(?s)^\\s*("|\')(.*)("|\')\\s*$',-replace='\\2')->trim&});
        $lines;


In order to try my code, you'll need to patch Lasso to support array->each.  Drop the following code into LassoStartup and restart Lasso:

        define_tag('element');
                return(@var('_array_element'));
        /define_tag;

        $__PROTOTYPES__->find('__array__')->insert('each' =
                {
                if(self->size > 0);
                        local('code') = @params->get(1);
                        var('_array_element') = null;
                        loop(self->size);
                                $_array_element = @self->get(loop_count);
                                #code->run;
                                $_array_element->detachreference;
                        /loop;
                        var_remove('_array_element');
                /if;
                });

So an easy example to see how it works, say you want to trim every item:

        var('array') = array(' a',' b   ','c');
        $array->each({element->trim});
        $array; // array: (a), (b), (c)

Or say you want to multiply every item by 5:

        var('array') = array(1,2,3,4,5);
        $array->each({element *= 5});
        $array; // array: (5), (10), (15), (20), (25)

As you can see, it's a little easier than ->foreach if you want to act on every element in the array.


- Bil


--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Steve Upton
At 3:36 PM -0500 4/16/09, Bil Corry wrote:
>Adam de Zoete wrote on 4/16/2009 9:12 AM:
>> Having a problem with arrays and referencing, can anyone help out? Am I
>> doing something wrong?
>
>I know you got your answer, but since you mentioned ->forEach, I thought you might be interested to know you could have plugged your code into ->foreach:
>

<snip>

>As you can see, it's a little easier than ->foreach if you want to act on every element in the array.
>

nice!

thanks Bil,

That's a lot like Prototype.js

Man, that Javascript, just when I think I have my head around programming I do some work in Javascript and realize how much I have to learn.

regards,

Steve


--


--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Adam de Zoete-2
In reply to this post by Bil Corry-3
Bil Corry wrote:
> As you can see, it's a little easier than ->foreach if you want to act on every element in the array.

Hi Bil,

This is very cool, I heard that many more of the tags will be open
sourced in LP9, is this how we'll be modifying/extending them?

Adam

--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


Reply | Threaded
Open this post in threaded view
|

Re: Array weirdness with referencing

Bil Corry-3
Adam de Zoete wrote on 4/17/2009 6:19 AM:
> Bil Corry wrote:
>> As you can see, it's a little easier than ->foreach if you want to act
>> on every element in the array.
>
> Hi Bil,
>
> This is very cool, I heard that many more of the tags will be open
> sourced in LP9, is this how we'll be modifying/extending them?

In Lasso 9, the process of extending built-in tags and types is actually easier.  I plan to upgrade many of my lp_ library tags into relevant L9 member tags.


- Bil


--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/