BASIC User Group > Code Challenges

XML Challenge

(1/2) > >>

AIR:
New Challenge!

Using the attached xml file, For each Food record:


* Extract the Name of the food
* Extract the information for the Sodium content
* Print out the Name in Quotes, taking care to eliminate any trailing spaces, and the Sodium Content, in the format shown below
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10

AIR.

AIR:
MBC VERSION:


--- Code: Text ---$MODULE xml.inc$EXECON raw tree as NODE, node as NODEdim name$,sodium$ tree = xmlLoad("sample.xml")node = xmlFind(tree,"food")while node     name$ = xmlGet$(node,"/name")    sodium$ = xmlGet$(node,"/sodium")    print enc$(trim$(name$)), " has a sodium level of ",sodium$    node = xmlNext(node,tree,"food")wend xmlDelete(tree) 
OUTPUT:

[riveraa@MacDev ~/Projects/mbc] $ ./xmlchallenge
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10
[riveraa@MacDev ~/Projects/mbc] $

AIR.

John:
Script BASIC


--- Code: Script BASIC ---' XML Chalenge - Script BASIC - JRS IMPORT t.bas SPLITA t::LoadString("sample.xml") BY "<food>" TO foodFOR idx = 0 TO UBOUND(food)  IF food[idx] LIKE "*<name>*</name>*<sodium>*</sodium>*" THEN _  PRINT FORMAT("\"%s\" has a sodium level of %g\n", TRIM(JOKER(2)),JOKER(4))NEXT 

$ time scriba xmlcc
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10

real   0m0.012s
user   0m0.012s
sys   0m0.000s
$

John:
AIR,

If can you create a project for MBC in the sandbox, I'll create a MBC syntax highlighting file for the forum.

AIR:

--- Quote from: John on December 13, 2018, 08:34:51 pm ---Script BASIC



--- End quote ---

Nice!.

If we're gonna brute-force it:


--- Code: Text ---$EXECON dim a$, cnt, name as REGEX, sodium as REGEX a$ = loadfile$("sample.xml")cnt = tally(a$,"<food>") for int i = 0 to cnt-1    if regmatch(a$,"<name>([a-zA-Z ,]+)</name>",&name) and regmatch(a$,"<sodium>([0-9]+)</sodium>",&sodium) then         print enc$(trim$(name.results[1]$)), " has a sodium level of ",sodium.results[1]$    end if    a$ = REMAIN$(a$, "</food>")next
[riveraa@MacDev ~/Projects/mbc] $ time ./xmlChallenge2
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10

real   0m0.005s
user   0m0.002s
sys   0m0.002s
[riveraa@MacDev ~/Projects/mbc] $

AIR.

Navigation

[0] Message Index

[#] Next page

Go to full version