Search and replace using a map

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

Search and replace using a map

CoMedia
Hi List,

I'm pretty sure this can be done (I even think I have done it myself years ago, but who knows where...?), but I cannot get my tired head wrapped around this.

What I want to achieve is a search and replace within a string (derrivated from a RSS feed, so I cannot influence the input). The search and replace elements are in a map.

        Local('source'='The color is blue');
        Local('color'=(Map: 'blue'='#0000ff', 'red'='#ff0000', 'green'='#00ff00'));

Of course I can do

        #source->(Find: #color);

but this only works if the string #source contains exactly the color name, nothing else.

I guess I'll have to iterate through all the elements of #source, like

        Iterate: #source, Local('temp');
                ...
        /Iterate;

What should be the ...?

I know it must be simple, but I have already tried every tag I could think of.

-- Alfred
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace using a map

Viaduct Productions
string->replace

http://localhost/Reference.LassoApp?[String-%3EReplace]

On 27-Feb-09, at 5:07 AM, CoMedia wrote:

> What I want to achieve is a search and replace within a string  
> (derrivated
> from a RSS feed, so I cannot influence the input). The search and  
> replace
> elements are in a map.



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

CoMedia
Viaduct Productions wrote
string->replace

http://localhost/Reference.LassoApp?[String-%3EReplace]
Yeah, of course, but what are the parameters?

-- Alfred
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace using a map

Viaduct Productions
In reply to this post by CoMedia
Well go read the tag.  Do you not know what you are trying to replace?

Iterate through your map and change your original string variable by  
each colour.  The end result is your final string.

On 27-Feb-09, at 5:25 AM, CoMedia wrote:

>>
>> string->replace
>>
>> http://localhost/Reference.LassoApp?[String-%3EReplace]Yeah, of  
>> course,
>> but what are the parameters?



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

CoMedia
Viaduct Productions wrote
Well go read the tag.  Do you not know what you are trying to replace?

Iterate through your map and change your original string variable by  
each colour.  The end result is your final string.
Do you hear that strange noise? That's me, banging my head.

As a matter of fact, I did it right from the start. Just forgot consistently to add a final output tag, so nothing was output or displayed. Just too tired to see this right. Sorry to bother you, Rich!

        Local('source'='The color is red');
        Local('color'=(Map: 'blue'='#0000ff', 'red'='#ff0000', 'green'='#00ff00'));
        Iterate: #color, Local('temp');
                #source->(Replace: #temp->(Get: 1), #temp->(Get: 2));
        /Iterate;
        #source;

-- Alfred
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace using a map

Viaduct Productions
In reply to this post by CoMedia
Locals are for custom tags.  If you have built a custom tag, then use  
return(#source);  If not, change to variables.

On 27-Feb-09, at 5:47 AM, CoMedia wrote:

> As a matter of fact, I did it right from the start. Just forgot  
> consistently
> to add a final output tag, so nothing was output or displayed. Just  
> too
> tired to see this right. Sorry to bother you, Rich!
>
> Local('source'='The color is red');
>         Local('color'=(Map: 'blue'='#0000ff', 'red'='#ff0000',
> 'green'='#00ff00'));
> Iterate: #color, Local('temp');
> #source->(Replace: #temp->(Get: 1), #temp->(Get: 2));
> /Iterate;
> #source;



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

CoMedia
Viaduct Productions wrote
Locals are for custom tags.  If you have built a custom tag, then use  
return(#source);  If not, change to variables.
I don't agree. You can use locals as page variables if you don't need to (or don't want to) pass the values to another page. This is also what Fletcher said during the last Summit. You can also use locals within custom tags, but that's another story.

-- Alfred
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace using a map

Viaduct Productions
In reply to this post by CoMedia
If you want to use extra virgin olive oil to lubricate your garage  
door, then go right ahead.  Yes it will work.  But there's a reason  
various things are created, even if they can be interchanged at  
times.  The concept of a local is suggested by its name.  Localization  
is meant to be within the custom tag, and is meant to be within the  
scope of the tag, not outside.  The same goes for privatizing  
attributes of a custom type.  Global variables, session variables, and  
page variables, are indeed referred to with the same type of  
suggestion, and carry functionality based upon the scope in which they  
are used.  So to expand on what you are doing, you can park every bit  
of data you need to manage into a global variable, but that would just  
be wrong.

On 27-Feb-09, at 6:10 AM, CoMedia wrote:

>
> Viaduct Productions wrote:
>> Locals are for custom tags.  If you have built a custom tag, then use
>> return(#source);  If not, change to variables.I don't agree. You  
>> can use
>> locals as page variables if you don't need to (or don't want to)  
>> pass the
>> values to another page. This is also what Fletcher said during the  
>> last
>> Summit. You can also use locals within custom tags, but that's  
>> another
>> story.



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

Göran Törnquist-2
I was going to be quiet on this matter. Rich, you're almost right.

Three different scopes exist in LP 8.5. Local variables, Page variables
and Global variables. That is what you have described. Though, there's a
twist. Something that was added along the road when local variables came
in LP. Local variables are equivalent to page variables when defined on
the page level.

I use this fact to do two things.

Firstly I use locals exclusively on the page level to signal that this
is a variable that doesn't need to retain its value in another file.
This is just a way to handle the funkiness of the Lasso namespace. When
I do intend to use the variable in an included file, then I use the page
variable definition.

Secondly, it makes it possible to develop a custom tag on a separate
test page. When the tag code is considered free from bugs, and has all
the specified features, then I can copy from the page, and wrap it in a
custom tag definition. This avoids the problem with less than ideal
error reporting when inside a custom tag.

/Göran

Viaduct Productions wrote:

> If you want to use extra virgin olive oil to lubricate your garage
> door, then go right ahead.  Yes it will work.  But there's a reason
> various things are created, even if they can be interchanged at
> times.  The concept of a local is suggested by its name.  Localization
> is meant to be within the custom tag, and is meant to be within the
> scope of the tag, not outside.  The same goes for privatizing
> attributes of a custom type.  Global variables, session variables, and
> page variables, are indeed referred to with the same type of
> suggestion, and carry functionality based upon the scope in which they
> are used.  So to expand on what you are doing, you can park every bit
> of data you need to manage into a global variable, but that would just
> be wrong.
>
> On 27-Feb-09, at 6:10 AM, CoMedia wrote:
>
>>
>> Viaduct Productions wrote:
>>> Locals are for custom tags.  If you have built a custom tag, then use
>>> return(#source);  If not, change to variables.I don't agree. You can
>>> use
>>> locals as page variables if you don't need to (or don't want to)
>>> pass the
>>> values to another page. This is also what Fletcher said during the last
>>> Summit. You can also use locals within custom tags, but that's another
>>> story.
>
>
>
> Rich in Toronto
> ...now go get on your bike
>
>
>
> --
> 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/
>
>
>


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



--
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: Search and replace using a map

Viaduct Productions

On 27-Feb-09, at 6:46 AM, Göran Törnquist wrote:

> Firstly I use locals exclusively on the page level to signal that  
> this is a variable that doesn't need to retain its value in another  
> file. This is just a way to handle the funkiness of the Lasso  
> namespace. When I do intend to use the variable in an included file,  
> then I use the page variable definition.

OK, but you are using that treatment in recognition that it won't  
carry over.  You ARE using the functionality of a local.  I didn't  
know it wouldn't carry in/out of an include.  The flow of a page I  
consider to be in the main scope of a page, even its includes.  I'm in  
a Onefile frame of mind, so my includes are meant for that type of  
purpose.  In light of using locals for anything more than a single  
page with no includes or anything above simple, I guess locals could  
be used, but would have limitations.

> Secondly, it makes it possible to develop a custom tag on a separate  
> test page. When the tag code is considered free from bugs, and has  
> all the specified features, then I can copy from the page, and wrap  
> it in a custom tag definition. This avoids the problem with less  
> than ideal error reporting when inside a custom tag.

But that's a tool you use for development.  You can also use a text  
factory to change all that over.  That doesn't eliminate the  
possibility what I mentioned before, by shoving everything into  
globals.  Just many ways to skin a cat.

Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

Göran Törnquist-2
Viaduct Productions wrote:

>
> On 27-Feb-09, at 6:46 AM, Göran Törnquist wrote:
>
>> Firstly I use locals exclusively on the page level to signal that
>> this is a variable that doesn't need to retain its value in another
>> file. This is just a way to handle the funkiness of the Lasso
>> namespace. When I do intend to use the variable in an included file,
>> then I use the page variable definition.
>
> OK, but you are using that treatment in recognition that it won't
> carry over.  You ARE using the functionality of a local.  I didn't
> know it wouldn't carry in/out of an include.  The flow of a page I
> consider to be in the main scope of a page, even its includes.  I'm in
> a Onefile frame of mind, so my includes are meant for that type of
> purpose.  In light of using locals for anything more than a single
> page with no includes or anything above simple, I guess locals could
> be used, but would have limitations.
Well, both local and var carries over to an include when defined on the
page level. Since that actually occurs, I recognize a local to retain
its value up to the next [include] or [process]. After executing those
tags, the content of any local defined on the page level may have been
changed.

>> Secondly, it makes it possible to develop a custom tag on a separate
>> test page. When the tag code is considered free from bugs, and has
>> all the specified features, then I can copy from the page, and wrap
>> it in a custom tag definition. This avoids the problem with less than
>> ideal error reporting when inside a custom tag.
>
> But that's a tool you use for development.  You can also use a text
> factory to change all that over.  That doesn't eliminate the
> possibility what I mentioned before, by shoving everything into
> globals.  Just many ways to skin a cat.
You're so right. Just my style of developing the Lasso code. I prefer to
avoid extraneous changes before deploying the code.

>
> Rich in Toronto
> ...now go get on your bike
>
>
>
> --
> 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/
>
>
>


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



--
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: Search and replace using a map

Viaduct Productions

On 27-Feb-09, at 7:12 AM, Göran Törnquist wrote:

> Well, both local and var carries over to an include when defined on  
> the page level. Since that actually occurs, I recognize a local to  
> retain its value up to the next [include] or [process]. After  
> executing those tags, the content of any local defined on the page  
> level may have been changed.

Ok so that recognizes that they are performing in the same way.  Above  
that, not that I've tested this, but I'm guessing that any vars  
declared and manipulated in a ctag are viewable after its call, down  
the page.  While locals might be used everywhere in a page, its use is  
beneficial in segregating the internal workings of a ctag.

If locals were adopted for such the same deployment, then there could  
be some confusion as the locals inside the ctag won't make it back out  
to the page.  Locals in that instance (sorry for the pun) would be  
expected to function on a more global nature.

>>> Secondly, it makes it possible to develop a custom tag on a  
>>> separate test page. When the tag code is considered free from  
>>> bugs, and has all the specified features, then I can copy from the  
>>> page, and wrap it in a custom tag definition. This avoids the  
>>> problem with less than ideal error reporting when inside a custom  
>>> tag.
>>
>> But that's a tool you use for development.  You can also use a text  
>> factory to change all that over.  That doesn't eliminate the  
>> possibility what I mentioned before, by shoving everything into  
>> globals.  Just many ways to skin a cat.
> You're so right. Just my style of developing the Lasso code. I  
> prefer to avoid extraneous changes before deploying the code.

I do the same, but with vars, then I change the code, run it into a  
template tag wrapper.  Chasing issues in tags during development  
drives people to drink way too much beer.



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

Göran Törnquist-2
Viaduct Productions wrote:

>
> On 27-Feb-09, at 7:12 AM, Göran Törnquist wrote:
>
>> Well, both local and var carries over to an include when defined on
>> the page level. Since that actually occurs, I recognize a local to
>> retain its value up to the next [include] or [process]. After
>> executing those tags, the content of any local defined on the page
>> level may have been changed.
>
> Ok so that recognizes that they are performing in the same way.  Above
> that, not that I've tested this, but I'm guessing that any vars
> declared and manipulated in a ctag are viewable after its call, down
> the page.  While locals might be used everywhere in a page, its use is
> beneficial in segregating the internal workings of a ctag.
>
> If locals were adopted for such the same deployment, then there could
> be some confusion as the locals inside the ctag won't make it back out
> to the page.  Locals in that instance (sorry for the pun) would be
> expected to function on a more global nature.
Well, in my case it's not an issue if the local doesn't make it out to
the callee. They are assumed to contain an undefined value after I have
executed either an include or a process tag. I very rarely include files
in the middle of pages. So, in any case, I'm safe from the possible
errors that may come from this particular variable definition convention.

My includes are either located in the beginning of the file, or in the
end. The same goes for process.

/Göran

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



--
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: Search and replace using a map

jasonhuck
I use locals for all "throwaway" variables by convention. In
particular, the variable used when definiting an interate loop. My
preference would be that the local variable in that case actually
*was* limited to the scope of the container, or that there was a
standard placeholder for that value so that you didn't have to define
it at all unless necessary for disambiguation in nested loops. That's
one reason Bil made his own wrapper for iterate, so that he could use
"loop_item" inside the loop. I just use "i" (a common convention). IMO
it improves the readability of the code.

JLH


--
tagSwap.net :: Open Source Lasso Code
<http://tagSwap.net/>


--
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: Search and replace using a map

CoMedia
In reply to this post by Viaduct Productions
Viaduct Productions wrote
If you want to use extra virgin olive oil to lubricate your garage  
door, then go right ahead.  Yes it will work.  But there's a reason  
various things are created, even if they can be interchanged at  
times.  The concept of a local is suggested by its name.  Localization  
is meant to be within the custom tag, and is meant to be within the  
scope of the tag, not outside.  The same goes for privatizing  
attributes of a custom type.  Global variables, session variables, and  
page variables, are indeed referred to with the same type of  
suggestion, and carry functionality based upon the scope in which they  
are used.  So to expand on what you are doing, you can park every bit  
of data you need to manage into a global variable, but that would just  
be wrong.
LassoReference: "[Local] either defines a new local variable or returns the value of an existiing local variable. [Local] is also used to define and return the value of instance variables within custom types."

http://reference.lassosoft.com/Reference.LassoApp?[Local]

You're confusing globals with locals. Globals live in the memory of your server forever (that is, until the next restart of Lasso). Locals don't, exept if you use them in a custom type. But in that case it's actually the custom type that eats memory, not the global.

I don't happen to have a garage to lubricate, and prefer olive oil in my pasta and salad, but - as a Dutchie - will get on my bike anyway ;-)

-- Alfred
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace using a map

Viaduct Productions
In reply to this post by jasonhuck
Would that be in light of respecting memory management, or just to  
make sure the temp $i isn't tripped into later?  I use $i (#i in ctag)  
as well.


On 27-Feb-09, at 7:43 AM, Jason Huck wrote:

> I use locals for all "throwaway" variables by convention. In
> particular, the variable used when definiting an interate loop. My
> preference would be that the local variable in that case actually
> *was* limited to the scope of the container, or that there was a
> standard placeholder for that value so that you didn't have to define
> it at all unless necessary for disambiguation in nested loops. That's
> one reason Bil made his own wrapper for iterate, so that he could use
> "loop_item" inside the loop. I just use "i" (a common convention). IMO
> it improves the readability of the code.



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

Viaduct Productions
In reply to this post by CoMedia
No, I'm not.  I've known the difference for about 10 years now.  My  
point was that you COULD use globals for any storage of data, but it  
would be wrong to do so.  And locals in custom types are called  
attributes if not in a member tag (method) and are called instance  
variables.  You can even privatize them so that you can only get at  
them using a member tag, instead of calling for $myType->properties.


On 27-Feb-09, at 7:47 AM, CoMedia wrote:

> http://reference.lassosoft.com/Reference.LassoApp?[Local]
>
> You're confusing globals with locals. Globals live in the memory of  
> your
> server forever (that is, until the next restart of Lasso). Locals  
> don't,
> exept if you use them in a custom type. But in that case it's  
> actually the
> custom type that eats memory, not the global.



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

CoMedia
Viaduct Productions wrote
No, I'm not.  I've known the difference for about 10 years now.  My  
point was that you COULD use globals for any storage of data, but it  
would be wrong to do so.  And locals in custom types are called  
attributes if not in a member tag (method) and are called instance  
variables.  You can even privatize them so that you can only get at  
them using a member tag, instead of calling for $myType->properties.
I think it's time for Fletcher to chime in and speak his famous last words (in this discussion I mean).

-- Alfred
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace using a map

Viaduct Productions
In reply to this post by CoMedia
This isn't about Fletcher.  It's common in other languages as well.  
The very reason we have those types is for different practices.  Why  
have variables if we don't need them?  For fun?


On 27-Feb-09, at 7:58 AM, CoMedia wrote:

>
>
> Viaduct Productions wrote:
>> No, I'm not.  I've known the difference for about 10 years now.  My
>> point was that you COULD use globals for any storage of data, but it
>> would be wrong to do so.  And locals in custom types are called
>> attributes if not in a member tag (method) and are called instance
>> variables.  You can even privatize them so that you can only get at
>> them using a member tag, instead of calling for $myType->properties.I
>> think it's time for Fletcher to chime in and speak his famous last  
>> words
>> (in this discussion I mean).
>



Rich in Toronto
...now go get on your bike



--
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: Search and replace using a map

jasonhuck
In reply to this post by Viaduct Productions
Both, really. The fewer variables you need to create, the better. As
is often mentioned, if you find yourself needing to create a large
number of variables on a page, it's more efficient to just put
everything into a single map, or whatever data structure is most
appropriate for your needs, whether one of the built-in ones or one of
your own design (custom types).

JLH

On Fri, Feb 27, 2009 at 7:49 AM, Viaduct Productions
<[hidden email]> wrote:

> Would that be in light of respecting memory management, or just to make sure
> the temp $i isn't tripped into later?  I use $i (#i in ctag) as well.
>
>
> On 27-Feb-09, at 7:43 AM, Jason Huck wrote:
>
>> I use locals for all "throwaway" variables by convention. In
>> particular, the variable used when definiting an interate loop. My
>> preference would be that the local variable in that case actually
>> *was* limited to the scope of the container, or that there was a
>> standard placeholder for that value so that you didn't have to define
>> it at all unless necessary for disambiguation in nested loops. That's
>> one reason Bil made his own wrapper for iterate, so that he could use
>> "loop_item" inside the loop. I just use "i" (a common convention). IMO
>> it improves the readability of the code.
>
>
>
> Rich in Toronto
> ...now go get on your bike
>
>
>
> --
> 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/
>
>
>



--
tagSwap.net :: Open Source Lasso Code
<http://tagSwap.net/>


--
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/


123