[LP8.6] iterate function

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[LP8.6] iterate function

Jon Harris
Hi

I am working with a webservice - which is sending me XML data which we are importing to local database tables.  Each record has around 70 fields, include a dozen "user defined" fields  (the code below is a subset). We are still working on the data feed, so this is in a state of flux at the moment.

For debugging, I am using an iterate function to go through a map. The pertinent code is shown below.

var('xPathMap') = map(
        'Stock_code' = '//STOCK_ITEM/STOCK_CODE/text()',
        'Stock_desc' = '//STOCK_ITEM/STOCK_DESC/text()',
        'userdefined1' = '//STOCK_ITEM/USER_DEFINED[1]/@NAME',
        'userdefined2' = '//STOCK_ITEM/USER_DEFINED[2]/@NAME',
        'userdefined3' = '//STOCK_ITEM/USER_DEFINED[3]/@NAME',
        'uservalue1' = '//STOCK_ITEM/USER_DEFINED[1]/text()',
        'uservalue2' = '//STOCK_ITEM/USER_DEFINED[2]/text()',
        'uservalue3' = '//STOCK_ITEM/USER_DEFINED[3]/text()'
) ;

iterate( var('xPathMap') , var('thisPair') ) ;
        var('thisField') = var('thisPair')->first ;
        var('thisXpath') = var('thisPair')->second ;
        var('thisValueArray') = XML_Extract(-XML=$theXML, -XPath=$thisXpath )  ;
        var('thisValue') = var('thisValueArray')->get(1)->contents ;
        var('thisField') ": " var('thisValue') '<br />' ;
/iterate ;

The technique works fine, however the issue is that the iterate function is "iterating" in a strange order, it's not the order I defined the map and it doesn't appear to be in the order of the values either. The result is my "userdefined" fields and their corresponding "uservalues" are appearing in apparently random places.

Can the iterate function use the "natural" order of the map, or do I have use a different technique. It's not as easy to maintain -  but presumably, I could use two separate arrays, one for the xpath and one for my variables, and iterate/loop through that.

Thanks for any thoughts
Jon Harris



#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [LP8.6] iterate function

Jolle Carlestam-2
2 sep. 2016 kl. 12:53 skrev Jon Harris <[hidden email]>:
>
> The technique works fine, however the issue is that the iterate function is "iterating" in a strange order, it's not the order I defined the map and it doesn't appear to be in the order of the values either. The result is my "userdefined" fields and their corresponding "uservalues" are appearing in apparently random places.

A map is an object where you can never rely on the order of the items put into the map. If you need the items in a specific order you would have use an array.

This is a natural behavior of how maps are designed.

HDB
Jolle

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [LP8.6] iterate function

Jon Harris
Hi Jolle

Thanks. That's what I thought. The array technique will be fine, just much easier to make a mistake as they might go out of sequence.

Thinking about it, I can probably use an array of pairs.

Regards
Jon


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Jolle Carlestam
Sent: 02 September 2016 12:16
To: [hidden email]
Subject: Re: [LP8.6] iterate function

2 sep. 2016 kl. 12:53 skrev Jon Harris <[hidden email]>:
>
> The technique works fine, however the issue is that the iterate function is "iterating" in a strange order, it's not the order I defined the map and it doesn't appear to be in the order of the values either. The result is my "userdefined" fields and their corresponding "uservalues" are appearing in apparently random places.

A map is an object where you can never rely on the order of the items put into the map. If you need the items in a specific order you would have use an array.

This is a natural behavior of how maps are designed.

HDB
Jolle

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email] Official list archives available at http://www.lassotalk.com To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [LP8.6] iterate function

Marc Vos-3
Have a look at: http://www.lassosoft.com/lassoDocs/languageReference/obj/Map/Keys

- -
Marc

On 02-09-2016 15:10, "Jon Harris" <[hidden email] on behalf of [hidden email]> wrote:

    Hi Jolle
   
    Thanks. That's what I thought. The array technique will be fine, just much easier to make a mistake as they might go out of sequence.
   
    Thinking about it, I can probably use an array of pairs.
   
    Regards
    Jon
   
   
    -----Original Message-----
    From: [hidden email] [mailto:[hidden email]] On Behalf Of Jolle Carlestam
    Sent: 02 September 2016 12:16
    To: [hidden email]
    Subject: Re: [LP8.6] iterate function
   
    2 sep. 2016 kl. 12:53 skrev Jon Harris <[hidden email]>:
    >
    > The technique works fine, however the issue is that the iterate function is "iterating" in a strange order, it's not the order I defined the map and it doesn't appear to be in the order of the values either. The result is my "userdefined" fields and their corresponding "uservalues" are appearing in apparently random places.
   
    A map is an object where you can never rely on the order of the items put into the map. If you need the items in a specific order you would have use an array.
   
    This is a natural behavior of how maps are designed.
   
    HDB
    Jolle
   
    #############################################################
   
    This message is sent to you because you are subscribed to
      the mailing list Lasso [hidden email] Official list archives available at http://www.lassotalk.com To unsubscribe, E-mail to: <[hidden email]>
    Send administrative queries to  <[hidden email]>
   
    #############################################################
   
    This message is sent to you because you are subscribed to
      the mailing list Lasso [hidden email]
    Official list archives available at http://www.lassotalk.com
    To unsubscribe, E-mail to: <[hidden email]>
    Send administrative queries to  <[hidden email]>
   




#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [LP8.6] iterate function

stevepiercy
In reply to this post by Jon Harris
Using an array of pairs to preserve order is slower than using a
map, especially as the object increases in elements.

Also you can use a cleaner technique to parse the XML which
avoids this nasty bit of potential error:

     var('thisValue') = var('thisValueArray')->get(1)->contents ;

...where $thisValueArray has the potential to be an array of
zero elements.

Use an xpath string() method:

     #xml -> extract('string(//STOCK_ITEM/USER_DEFINED[3])')

xpath has a rich syntax.
https://www.w3.org/TR/xpath/#section-String-Functions

--steve


On 9/2/16 at 10:53 AM, [hidden email] (Jon Harris) pronounced:

>Hi
>
>I am working with a webservice - which is sending me XML data
>which we are importing to local database tables.  Each record
>has around 70 fields, include a dozen "user defined" fields  
>(the code below is a subset). We are still working on the data
>feed, so this is in a state of flux at the moment.
>
>For debugging, I am using an iterate function to go through a
>map. The pertinent code is shown below.
>
>var('xPathMap') = map(
>'Stock_code' = '//STOCK_ITEM/STOCK_CODE/text()',
>'Stock_desc' = '//STOCK_ITEM/STOCK_DESC/text()',
>'userdefined1' = '//STOCK_ITEM/USER_DEFINED[1]/@NAME',
>'userdefined2' = '//STOCK_ITEM/USER_DEFINED[2]/@NAME',
>'userdefined3' = '//STOCK_ITEM/USER_DEFINED[3]/@NAME',
>'uservalue1' = '//STOCK_ITEM/USER_DEFINED[1]/text()',
>'uservalue2' = '//STOCK_ITEM/USER_DEFINED[2]/text()',
>'uservalue3' = '//STOCK_ITEM/USER_DEFINED[3]/text()'
>) ;
>
>iterate( var('xPathMap') , var('thisPair') ) ;
>var('thisField') = var('thisPair')->first ;
>var('thisXpath') = var('thisPair')->second ;
>var('thisValueArray') = XML_Extract(-XML=$theXML, -XPath=$thisXpath )  ;
>var('thisValue') = var('thisValueArray')->get(1)->contents ;
>var('thisField') ": " var('thisValue') '<br />' ;
>/iterate ;
>
>The technique works fine, however the issue is that the iterate
>function is "iterating" in a strange order, it's not the order
>I defined the map and it doesn't appear to be in the order of
>the values either. The result is my "userdefined" fields and
>their corresponding "uservalues" are appearing in apparently
>random places.
>Can the iterate function use the "natural" order of the map, or
>do I have use a different technique. It's not as easy to
>maintain -  but presumably, I could use two separate arrays,
>one for the xpath and one for my variables, and iterate/loop
>through that.
>
>Thanks for any thoughts
>Jon Harris
>
>
>
>#############################################################
>
>This message is sent to you because you are subscribed to
>the mailing list Lasso [hidden email]
>Official list archives available at http://www.lassotalk.com
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy              Website Builder              Soquel, CA
<[hidden email]>               <http://www.stevepiercy.com/>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Loading...