Convert "Array" to comma delimited list

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
newpie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 155
Joined: Sat Jun 29, 2013 11:24 pm

Convert "Array" to comma delimited list

Post by newpie » Sat Nov 28, 2015 2:35 am

Hello, I have a php pdo script that gives me an format posted below. I know the order of the keys, but I am trying to get get in a list that I can analyze with item 1, item 2 and so on. Would nice to have a function. The Array may expand with keys, but I will know the key order still.

In the below example, with two keys, my goal would to be have a result of : John,Manager

Code: Select all

Array
(
    [0] => Array
        (
            [Name] => John
        )

    [1] => Array
        (
            [Position] => Manager
        )

)
I appreciate any help in this matter. Happy Holidays as well.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Convert "Array" to comma delimited list

Post by sritcp » Sat Nov 28, 2015 3:36 am

Hi newpie:

Here you go:

Code: Select all

-- Create an example array
put "John" into tArray[1]["Name"]
put "Manager" into tArray[1]["Position"]
put "Jane" into tArray[2]["Name"]
put "Accountant" into tArray[2]["Position"]
put "Joan" into tArray[3]["Name"]
put "Counsel" into tArray[3]["Position"]

set the rowDelimiter to comma
repeat with i = 1 to 3
combine tArray[i] by row
end repeat

set the rowDelimiter to return
combine tArray by row
put tArray
Result:
John,Manager
Jane,Accountant
Joan,Counsel

Happy Thanksgiving,
Sri

Note: This gives you a general method to explore. If you have a number of keys, your particular set of keys may not sort naturally as in the above examples. A simple solution is to (temporarily) replace your keys with integers (1,2,....) so they will be displayed in order.

newpie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 155
Joined: Sat Jun 29, 2013 11:24 pm

Re: Convert "Array" to comma delimited list

Post by newpie » Sat Nov 28, 2015 3:53 am

Thanks sritcp, I appreciate the quick reply. I tried the below piece of code on example in my first post and it seems the "array" I am given from php pdo is in a different format and the livecode script returned nothing for tArray I am afraid.

Code: Select all

set the rowDelimiter to comma
repeat with i = 1 to 3
combine tArray[i] by row
end repeat

set the rowDelimiter to return
combine tArray by row
put tArray
Is there a way to parse out the data from below to perhaps just grab everything after "=>" ignoring the words "Array"?

Code: Select all

Array
(
    [0] => Array
        (
            [Name] => John
        )

    [1] => Array
        (
            [Position] => Manager
        )

)

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Convert "Array" to comma delimited list

Post by sritcp » Sat Nov 28, 2015 3:12 pm

Hi newpie:

Sorry, I don't know anything about php's output format.

Regards,
Sri

newpie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 155
Joined: Sat Jun 29, 2013 11:24 pm

Re: Convert "Array" to comma delimited list

Post by newpie » Sat Nov 28, 2015 7:57 pm

Thanks sritcp for trying, I researched the forum further and came up with this function using regex and MatchChunk. Hopefully it can help someone out there dealing with print arrays from PHP. Thanks

Code: Select all

on mouseUp
   put PHPArrayParser (field "text") into tAnswer
   answer word 2 of tAnswer --this is an example of retrieving 2nd word to analyze further
end mouseUp


function PHPArrayParser Chunk
   local tVar, R
   repeat for each line tLine in Chunk -- process the lines
      put word 1 of tLine into tVar
      get MatchChunk( tVar, "\[.[a-zA-Z]+\]$") --this will return true or false if the line begins with this format ["letters"]
      if it is true then
         put word 3 of tLine & tab after R
      end if
   end repeat
   return R
end PHPArrayParser
Edit:
Added the links that help me to the above conclusion:
http://forums.livecode.com/viewtopic.php?f=7&t=10708
http://forums.livecode.com/phpBB2/viewtopic.php?t=3094
http://forums.livecode.com/phpBB2/viewt ... =7&t=25729
Last edited by newpie on Sat Nov 28, 2015 8:15 pm, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Convert "Array" to comma delimited list

Post by bn » Sat Nov 28, 2015 8:03 pm

Hi newpi,

could you please add a link to the thread where you found the script?

Thank you

Bernd

newpie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 155
Joined: Sat Jun 29, 2013 11:24 pm

Re: Convert "Array" to comma delimited list

Post by newpie » Sat Nov 28, 2015 8:15 pm

Hey bn, I posted some links which help me make this script, it is not a direct cut and paste. Hope it helps. Thanks

Post Reply