Extracting data out of a big text...

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

Extracting data out of a big text...

Post by shadowslash » Tue Jul 20, 2010 7:50 am

Hey guys, I have a problem, can anyone possibly help me with this:

I have a large string and I want to extract just a portion of it, here is an example big string of text:

Code: Select all

[[6500,true,{"attributes":{"1":{"name":"Food","min_value":"0","max_value":"10","flavors":{"1":"Loamflakes","24":"SKITTLES ®"}},"2":{"name":"Light","min_value":"0","max_value":"10","flavors":{"6":"Black","5":"Natural"}},"3":{"name":"Music","min_value":"0","max_value":"10","flavors":{"18":"Reef Riff","20":"Coral Love","21":"Calm Water","10":"The Conch Song","9":"Seashell Shock","8":"O' Lighthouse","7":"Ballad of the Overseer","16":"Kelp!"}},"4":{"name":"Temperature","min_value":"0","max_value":"10","flavors":{"15":"Boiling","14":"Hot","13":"Moderate","11":"Freezing","12":"Chilly"}}},"engine_delay_time":"21600","max_health":"32766","max_inhabitant_count":"17","max_decoration_count":"1000","gaia_curr_time":1279607814}],[6510,true,{"653625":{"serial":"624641307","attr_settings":{"1":{"attr_value":null,"attr_flavor":"1"},"2":{"attr_value":null,"attr_flavor":"6"},"3":{"attr_value":null,"attr_flavor":"18"},"4":{"attr_value":null,"attr_flavor":"12"}},"name":"my little minions <3","user_id":"3662446","show_in_sig":"1","show_in_profile":0,"last_engine_run":"1279571189","tap_count":"2027","view_count":"4036","total_gold_won":1109037,"env_health":"29460","env_bg_id":null,"env_last_grant_time":"1279571189","inhab_retire":false,"game_info":{"1":{"type":1,"instance_id":"526270229.1279596179.904921959","open_time":1279606965,"close_time":1279607805,"end_time":1279607865,"length":60,"results_time":1279607875,"state":"active","player_count":7}},"events":{"metadata":[],"subevents":["index","profile","overseer_takeover_2009"]}}}],[6511,true,{"1314262183":{"name":"mrow","serial":"1314262183","inhab_health":"5850","inhab_incept":"1261683281","inhab_expires":"1275798654","inhab_cryo":"1271068764","in_env":"0","item_id":"33881","lifespan":"7500000"},"1511258969":{"name":null,"serial":"1511258969","inhab_health":"7096","inhab_incept":"1266770464","inhab_expires":"1281665242","inhab_cryo":"1271072700","in_env":"0","item_id":"27421","lifespan":"1300000"},"1572831175":{"name":null,"serial":"1572831175","inhab_health":"7048","inhab_incept":"1266944881","inhab_expires":"1268266649","inhab_cryo":"1267428420","in_env":"0","item_id":"27421","lifespan":"1300000"},"1584597773":{"name":null,"serial":"1584597773","inhab_health":"8380","inhab_incept":"1267411719","inhab_expires":"1365915545","inhab_cryo":"1272619120","in_env":"0","item_id":"39782","lifespan":"2419200"},"1584178511":{"name":null,"serial":"1584178511","inhab_health":"8220","inhab_incept":"1267411719","inhab_expires":"1366713548","inhab_cryo":"1272429313","in_env":"0","item_id":"39782","lifespan":"2419200"},"1584212097":{"name":null,"serial":"1584212097","inhab_health":"8440","inhab_incept":"1267411719","inhab_expires":"1338362741","inhab_cryo":"1273738201","in_env":"0","item_id":"39782","lifespan":"2419200"},"1589631879":{"name":null,"serial":"1589631879","inhab_health":"8300","inhab_incept":"1267411719","inhab_expires":"1359267566","inhab_cryo":"1272429234","in_env":"0","item_id":"39782","lifespan":"2419200"},"1588573109":{"name":null,"serial":"1588573109","inhab_health":"8280","inhab_incept":"1267411719","inhab_expires":"1373461553","inhab_cryo":"1271109009","in_env":"0","item_id":"39782","lifespan":"2419200"},"1589233129":{"name":null,"serial":"1589233129","inhab_health":"8360","inhab_incept":"1267411719","inhab_expires":"1326666746","inhab_cryo":"1271108999","in_env":"0","item_id":"39782","lifespan":"2419200"},"1588987321":{"name":"paco","serial":"1588987321","inhab_health":"12000","inhab_incept":"1267428461","inhab_expires":"1424660646","inhab_cryo":"1277266840","in_env":"0","item_id":"39666","lifespan":"3024000"},"1589841139":{"name":"jose","serial":"1589841139","inhab_health":"12000","inhab_incept":"1267428461","inhab_expires":"1321384744","inhab_cryo":"1273030124","in_env":"0","item_id":"39666","lifespan":"3024000"},"1589816319":{"name":"martin","serial":"1589816319","inhab_health":"12000","inhab_incept":"1267428461","inhab_expires":"1377648098","inhab_cryo":"1275018981","in_env":"0","item_id":"39666","lifespan":"3024000"},"1588366305":{"name":"javier","serial":"1588366305","inhab_health":"12000","inhab_incept":"1267589154","inhab_expires":"1384521324","inhab_cryo":"1276119727","in_env":"0","item_id":"39666","lifespan":"3024000"},"1566741267":{"name":"raul","serial":"1566741267","inhab_health":"12000","inhab_incept":"1267610151","inhab_expires":"1303423762","inhab_cryo":"1273030113","in_env":"0","item_id":"39666","lifespan":"3024000"},"1589910021":{"name":"lucy","serial":"1589910021","inhab_health":"13584","inhab_incept":"1268040166","inhab_expires":"1438683856","inhab_cryo":"0","in_env":"1","item_id":"39770","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"1","frequency":"1","cap":"200"}]}},"1606696283":{"name":"crest","serial":"1606696283","inhab_health":"9900","inhab_incept":"1268181898","inhab_expires":"1445120841","inhab_cryo":"0","in_env":"1","item_id":"39716","lifespan":"2419200","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"3","frequency":"2","cap":"200"}]}},"1607806391":{"name":"falcon","serial":"1607806391","inhab_health":"9920","inhab_incept":"1268369500","inhab_expires":"1440239976","inhab_cryo":"0","in_env":"1","item_id":"39716","lifespan":"2419200"},"1616311157":{"name":"billy bob","serial":"1616311157","inhab_health":"9860","inhab_incept":"1268986887","inhab_expires":"1442375457","inhab_cryo":"0","in_env":"1","item_id":"39750","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"3","frequency":"2","cap":"300"}]}},"1624204811":{"name":"anne marie","serial":"1624204811","inhab_health":"9760","inhab_incept":"1269905272","inhab_expires":"1444373863","inhab_cryo":"0","in_env":"1","item_id":"39750","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"3","frequency":"2","cap":"300"}]}},"1565719661":{"name":null,"serial":"1565719661","inhab_health":"9740","inhab_incept":"1270347063","inhab_expires":"1359870152","inhab_cryo":"0","in_env":"1","item_id":"39780","lifespan":"2419200"},"1565513653":{"name":null,"serial":"1565513653","inhab_health":"9740","inhab_incept":"1270347063","inhab_expires":"1360031057","inhab_cryo":"0","in_env":"1","item_id":"39780","lifespan":"2419200","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"1","frequency":"1","cap":"200"}]}},"1657704289":{"name":null,"serial":"1657704289","inhab_health":"5000","inhab_incept":"1270507533","inhab_expires":"1271747770","inhab_cryo":"1271153866","in_env":"0","item_id":"39718","lifespan":"1209600"},"1649766791":{"name":"gyroscope","serial":"1649766791","inhab_health":"9740","inhab_incept":"1270512728","inhab_expires":"1395718880","inhab_cryo":"0","in_env":"1","item_id":"39780","lifespan":"2419200","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"1","frequency":"1","cap":"200"}]}},"1661181619":{"name":"herman","serial":"1661181619","inhab_health":"9660","inhab_incept":"1270627196","inhab_expires":"1359766491","inhab_cryo":"0","in_env":"1","item_id":"39780","lifespan":"2419200"},"1627873781":{"name":"sparkz","serial":"1627873781","inhab_health":"9660","inhab_incept":"1270627196","inhab_expires":"1395349114","inhab_cryo":"0","in_env":"1","item_id":"39780","lifespan":"2419200","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"1","frequency":"1","cap":"200"}]}},"1711895921":{"name":null,"serial":"1711895921","inhab_health":null,"inhab_incept":null,"inhab_expires":null,"inhab_cryo":null,"in_env":null,"item_id":"27421","lifespan":"1300000"},"1713039165":{"name":"jet lee","serial":"1713039165","inhab_health":"9280","inhab_incept":"1273030145","inhab_expires":"1331247907","inhab_cryo":"0","in_env":"1","item_id":"39750","lifespan":"3800000"},"1713039167":{"name":"betty paige","serial":"1713039167","inhab_health":"9060","inhab_incept":"1273407703","inhab_expires":"1299231754","inhab_cryo":"0","in_env":"1","item_id":"39750","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"3","frequency":"2","cap":"300"}]}},"1713039169":{"name":"janet","serial":"1713039169","inhab_health":"9320","inhab_incept":"1273030145","inhab_expires":"1301684472","inhab_cryo":"0","in_env":"1","item_id":"39750","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"3","frequency":"2","cap":"300"}]}},"1713651563":{"name":"molly","serial":"1713651563","inhab_health":"12848","inhab_incept":"1273407729","inhab_expires":"1295163854","inhab_cryo":"0","in_env":"1","item_id":"39770","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"1","frequency":"1","cap":"200"}]}},"1389321005":{"name":null,"serial":"1389321005","inhab_health":"8880","inhab_incept":"1275019046","inhab_expires":"1282162667","inhab_cryo":"0","in_env":"1","item_id":"39716","lifespan":"2419200"},"1724759345":{"name":"latoya","serial":"1724759345","inhab_health":"8940","inhab_incept":"1274826142","inhab_expires":"1287258464","inhab_cryo":"0","in_env":"1","item_id":"39750","lifespan":"3800000","game_specifics":{"drop_type":"1","rewards":[{"type":"1","item_id":null,"amount":"2","chance":"3","frequency":"2","cap":"300"}]}},"1726576945":{"name":null,"serial":"1726576945","inhab_health":"9060","inhab_incept":"1273738237","inhab_expires":"1309531771","inhab_cryo":"0","in_env":"1","item_id":"39716","lifespan":"2419200"},"1727018279":{"name":null,"serial":"1727018279","inhab_health":null,"inhab_incept":null,"inhab_expires":null,"inhab_cryo":null,"in_env":null,"item_id":"27421","lifespan":"1300000"},"1747842895":{"name":null,"serial":"1747842895","inhab_health":null,"inhab_incept":null,"inhab_expires":null,"inhab_cryo":null,"in_env":null,"item_id":"27421","lifespan":"1300000"},"1770617455":{"name":null,"serial":"1770617455","inhab_health":null,"inhab_incept":null,"inhab_expires":null,"inhab_cryo":null,"in_env":null,"item_id":"27421","lifespan":"1300000"}}],[6512,true,{"618324484":{"serial":"618324484","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":"1232656701","in_env":"0","item_id":"26927","decor_type":"1","is_ground_based":"n"},"624641308":{"serial":"624641308","decor_x_pos":"70","decor_y_pos":"490","layer":"19","decor_incept":"1232656701","in_env":"1","decor_grant_type":null,"decor_grant_id":null,"decor_grant_quant":null,"item_id":"27015","decor_type":"0","is_ground_based":"y"},"624641306":{"serial":"624641306","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":"1232656701","in_env":"1","item_id":"27043","decor_type":"2","is_ground_based":"n"},"774266394":{"serial":"774266394","decor_x_pos":"439","decor_y_pos":"530","layer":"4","decor_incept":"1232656701","in_env":"0","item_id":"26975","decor_type":"0","is_ground_based":"y"},"801302603":{"serial":"801302603","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26915","decor_type":"1","is_ground_based":"n"},"1460465639":{"serial":"1460465639","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26903","decor_type":"1","is_ground_based":"n"},"1498141973":{"serial":"1498141973","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26903","decor_type":"1","is_ground_based":"n"},"1501498297":{"serial":"1501498297","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26991","decor_type":"0","is_ground_based":"n"},"1572837653":{"serial":"1572837653","decor_x_pos":"594","decor_y_pos":"491","layer":"20","decor_incept":"1275011405","in_env":"1","item_id":"26953","decor_type":"0","is_ground_based":"y"},"1577411371":{"serial":"1577411371","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"27761","decor_type":"0","is_ground_based":"y"},"1639822303":{"serial":"1639822303","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26915","decor_type":"1","is_ground_based":"n"},"1677062065":{"serial":"1677062065","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26927","decor_type":"1","is_ground_based":"n"},"1713364843":{"serial":"1713364843","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26903","decor_type":"1","is_ground_based":"n"},"1487095441":{"serial":"1487095441","decor_x_pos":"0","decor_y_pos":"0","layer":null,"decor_incept":"1263498458","in_env":"1","item_id":"38730","decor_type":"1","is_ground_based":"n"},"1770619657":{"serial":"1770619657","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26927","decor_type":"1","is_ground_based":"n"},"1789869263":{"serial":"1789869263","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26927","decor_type":"1","is_ground_based":"n"},"1802121719":{"serial":"1802121719","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26953","decor_type":"0","is_ground_based":"y"},"1825785597":{"serial":"1825785597","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26937","decor_type":"0","is_ground_based":"y"},"1835322421":{"serial":"1835322421","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26991","decor_type":"0","is_ground_based":"n"},"1835323603":{"serial":"1835323603","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26979","decor_type":"0","is_ground_based":"y"},"1839167345":{"serial":"1839167345","decor_x_pos":null,"decor_y_pos":null,"layer":null,"decor_incept":null,"in_env":null,"item_id":"26927","decor_type":"1","is_ground_based":"n"}}],[107,false,[-4,"User session has expired"]]]
Now if you use find on your browser and search for "cap" without the quotes, you will see it appears on various parts of the string each with a different value, now I want to know how I'd be able to extract those numbers and possibly add them up all together? I've been up all night already trying to figure this thing out =/ Can anybody help me? If you feel really grand you can also make a function that I can use... =/ For those who'll say don't ask for the code, I did indicate that that is only for those who feel grand enough to do that.. Thank you all
Parañaque, Philippines
Image
Image

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

Re: Extracting data out of a big text...

Post by shadowslash » Tue Jul 20, 2010 9:14 am

Okay, I somehow found a way for it by using matchText() function but the problem is, natchText() only returns the first occurence of the said term, how do I do it for each of the "cap"s?
Parañaque, Philippines
Image
Image

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: Extracting data out of a big text...

Post by Curry » Tue Jul 20, 2010 9:48 am

matchText: put original text into a throwaway variable, delete the found segment and then find again

offset: this function provides a skip value to allow continuing the search and may suffice for your needs

Alternatively: parse your data to work with it
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

Re: Extracting data out of a big text...

Post by shadowslash » Tue Jul 20, 2010 10:13 am

Curry wrote:matchText: put original text into a throwaway variable, delete the found segment and then find again

offset: this function provides a skip value to allow continuing the search and may suffice for your needs

Alternatively: parse your data to work with it
Hi Curry! Thanks for replying! I managed to get through this trouble after I had a cup of coffee :D
Here's what I did

Code: Select all

on mouseUp
   local tMatch,tSource,tTotal,tCap,tCap1
   put fld 1 into tSource
   put 0 into tTotal
   put true into tMatch
   repeat until tMatch is false
      put matchChunk(tSource,(quote & "cap" & quote & ":" & quote & "([0-9]*[0-9])" & quote),tCap,tCap1) into tMatch
      if tMatch is true then
         get char tCap to tCap1 of tSource
         add it to tTotal
         delete char tCap to tCap1 of tSource
      end if
   end repeat
   answer "Total:" && tTotal
end mouseUp
Parañaque, Philippines
Image
Image

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: Extracting data out of a big text...

Post by Curry » Tue Jul 20, 2010 10:42 am

Good job.
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

Post Reply