I have some XML in the below format.
<user uid="0001">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<ImagePath>images/0001.jpg</ImagePath>
<flightno>GS1234</flightno>
</user>
<user uid="0002">
<FirstName>Luke</FirstName>
<LastName>Dixon</LastName>
<ImagePath>images/0002.jpg</ImagePath>
<flightno>TD1234</flightno>
</user>
<user uid="0003">
<FirstName>Paul</FirstName>
<LastName>Kerr</LastName>
<ImagePath>images/0003.jpg</ImagePath>
<flightno>GS1234</flightno>
</user>
This is a small sample, there are a couple 100 of these.
I have used E4x filtering to filter down another set of XML data that produces an as3 array. The array contains some flight numbers (eg : [GS1234,PB7367,TD1234].
I'm wondering how I can filter my XML (as shown above) to only show the users whose 'flightno' EXISTS in the AS3 array.
I'm guessing its some sort of E4X query but I can't seem to get it right!
Thanks!
// Don't mind me using this trick with inline XML, it's not the point.
// It's here just to make it possible to copy and paste code
// with multiline XML sample.The actual solution is a one-liner below.
var usersXML:XML = new XML(<x><![CDATA[
<data>
<user uid="0001">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<ImagePath>images/0001.jpg</ImagePath>
<flightno>GS1234567</flightno>
</user>
<user uid="0002">
<FirstName>Luke</FirstName>
<LastName>Dixon</LastName>
<ImagePath>images/0002.jpg</ImagePath>
<flightno>TD1234</flightno>
</user>
<user uid="0003">
<FirstName>Paul</FirstName>
<LastName>Kerr</LastName>
<ImagePath>images/0003.jpg</ImagePath>
<flightno>GS1234</flightno>
</user>
</data>
]]></x>.toString());
// once again, the way I create sample departingXML
// is not important, it's just for copying and pasting into IDE.
var departingXML:XML = new XML(<x><![CDATA[
<flights>
<flight>
<number>GS1234</number>
<date>10/11/2015</date>
<time>1440</time>
</flight>
<flight>
<number>TD1234</number>
<date>10/11/2015</date>
<time>1450</time>
</flight>
</flights>
]]></x>.toString());
// 1. create filter array
var flightNoArray:Array = [];
departingXML.flight.number.(flightNoArray.push(toString()));
trace(flightNoArray); // GS1234,TD1234
trace(typeof(flightNoArray[0])); // string
// 2. filter users:
var list:XMLList = usersXML.user.(flightNoArray.indexOf(flightno.toString()) >= 0);
trace(list); // traces users 0002 and 0003
I wouldn't call it efficient or at least readable though.
// Note: this line is somewhat queer and I don't like it,
departingXML.flight.number.(flightNoArray.push(toString()));
// but this is the only way I can now think of to get and array
// of strings from an XMLList nodes without a loop.
// I would advise to use a readable and simple loop instead.
usersXML.user -- this gets you an XMLList with all nodes named "user"
usersXML.user.(some condition) -- this filters the XMLList of user nodes given a condition
flightNoArray.indexOf(flightno.toString()) >= 0 -- and this is a filter condition
flightno.toString() -- gets you a string inside flightno child
REFERENCE: Traversing XML structures.
Explanation of the search trick in the note above.
UPDATE: it turned out in comments that it was also the way the filter array was populated that was causing trouble. The code below demonstrates some more E4X.
This is how the filter list was created and what was actually happening:
// once again, the way I create sample departingXML
// is just for the sake of copying and pasting, it's not related to solution.
var departingXML:XML = new XML(<x><![CDATA[
<flights>
<flight>
<number>GS1234</number>
<date>10/11/2015</date>
<time>1440</time>
</flight>
<flight>
<number>TD1234</number>
<date>10/11/2015</date>
<time>1450</time>
</flight>
</flights>
]]></x>.toString());
// the way it was done before
var flightNoArray: Array = [];
for each(var num: XML in departingXML.flight) {
flightNoArray.push(num.number);
// WARNING! num.number is an XMLList! It s NOT a string.
// Addressing nodes by name ALWAYS gets you an XMLList,
// even if there's only one node with that name
// Hence, `flightNoArray.indexOf("string")` could not possibly work,
// as there are lists inside of the array, not strings.
// We can check if this is a fact:
trace(flash.utils.getQualifiedClassName(flightNoArray[flightNoArray.length-1]));
// (traces XMLList)
// Or this way (note toXMLString() method)
trace(flightNoArray[flightNoArray.length-1].toXMLString());
// (traces <number>GS1234</number>)
}
trace(flightNoArray);
trace(flightNoArray); traces GS1234,TD1234 because this is the way toString() method works for xml nodes -- it gets you text, that is inside. This is why there is a special method toXMLString(), that gets you a string representation of a node.
Related
If I have an array, [Joe, John, Adam, Sam, Bill, Bob] and I want to try to add this to a new row by doing SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array]); , what happens is that the entire list of names goes into 1 cell. Is there a way to break this up so they file away into the same row, but different columns? I need to continue using appendRow however.
I get this:
But I really want to have it look like this:
var my2DArrayFromRng = datasheet.getRange("A:A").getValues();
var a = my2DArrayFromRng.join().split(',').filter(Boolean);
var array = [];
for (d in a) {
array.push(a[d]);
}
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array.toString()]);
You are converting your array to a string before you post it which is causing your issue.
Do not use the array.toString() method inside append row. Instead just append the array as it is.
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow(array);
I'm really fresh to Python and need help reading information from txt file. I have a large C++ app need to duplicate it in Python. Sadly I have no idea where to start. I've been reading and watching some tutorials, but little help from them and I'm running out of time.
So my task is:
I have a shopping list with:
-Name of the item, price and age.
I also need to create two searches.
Search whether the item is in the shop (comparing strings).
if name of the item is == to the input name.
Search by age. Once the program finds the items, then it needs to print the list according to the price - from the lowest price to the highest.
For example you input age 15 - 30, the program prints out appropriate
items and sorts them by the price.
Any help would be nice. At least from where I could start.
Thank you.
EDITED
So far, I have this code:
class data:
price = 0
agefrom = 0
ageto = 0
name = ''
# File reading
def reading():
with open('toys.txt') as fd:
toyslist = []
lines = fd.readlines()
for line in lines:
information = line.split()
print(information)
"""information2 = {
'price': int(information[1])
'ageftom': int(information[2])
'ageto': int(information[3])
#'name': information[4]
}"""
information2 = data()
information2.price = int(information[0])
information2.agefrom = int(information[1])
information2.ageto = int(information[2])
information2.name = information[3]
toyslist.append(information2)
return toyslist
information = reading()
I HAVE A PROBLEM WITH THIS PART. I want to compare the user's input with the item information in the txt file.
n_search = raw_input(" Please enter the toy you're looking for: ")
def name_search(information):
for data in information:
if data.name == n_search:
print ("We have this toy.")
else:
print ("Sorry, but we don't have this toy.")
If you want to fins something in a list it's generally as straightforward as:
if "apple" in ["tuna", "pencil", "apple"]
However, in your case, the list to search is a list of lists so you need to "project" it somehow. List comprehension is often the easiest to reason about, a sort of for loop in a for loop.
if "apple" in [name for name,price,age in [["tuna",230.0,3],["apple",0.50,1],["pencil",1.50,2]]]
From here you want to start looking at filters whereby you provide a function that determines whether an entry is matched or not. you can roll your own in a for loop or use something more functional like 'itertools'.
Sorting on a list is also easy, just use 'sorted(my_list)' supplying a comparator function if you need it.
Examples as per your comment...
class ShoppingListItem:
def __init__(self,name,price,age):
self.name=name
self.price=price
self.age=age
or
from collections import namedtuple
sli = namedtuple("ShoppingListItem",['name','age','price'])
I have an array, lets call it _persons.
I am populating this array with Value Objects, lets call this object PersonVO
Each PersonVO has a name and a score property.
What I am trying to do is search the array &
//PSEUDO CODE
1 Find any VO's with same name (there should only be at most 2)
2 Do a comparison of the score propertys
3 Keep ONLY the VO with the higher score, and delete remove the other from the _persons array.
I'm having trouble with the code implementation. Any AS3 wizards able to help?
You'd better use a Dictionary for this task, since you have a designated unique property to query. A dictionary approach is viable in case you only have one key property, in your case name, and you need to have only one object to have this property at any given time. An example:
var highscores:Dictionary;
// load it somehow
function addHighscore(name:String,score:Number):Boolean {
// returns true if this score is bigger than what was stored, aka personal best
var prevScore:Number=highscores[name];
if (isNaN(prevScore) || (prevScore<score)) {
// either no score, or less score - write a new value
highscores[name]=score;
return true;
}
// else don't write, the new score is less than what's stored
return false;
}
The dictionary in this example uses passed strings as name property, that is the "primary key" here, thus all records should have unique name part, passed into the function. The score is the value part of stored record. You can store more than one property in the dictionary as value, you'll need to wrap then into an Object in this case.
you want to loop though the array and check if there are any two people with the same name.
I have another solution that may help, if not please do say.
childrenOnStage = this.numChildren;
var aPerson:array = new array;
for (var c:int = 0; c < childrenOnStage; c++)
{
if (getChildAt(c).name == "person1")
{
aPerson:array =(getChildAt(c);
}
}
Then trace the array,
I have a Perl class that is storing node and arc information for a tree data structure.
When I try to output this as XML using XML::Simple I do not get the full expansion of the arrays.
I have
$table->{arcs} = #arcs;
$table->{nodes} = #nodes;
And when I try to output this as XML I get the following output
<?xml version='1.0'?>
<Root>
<arcs>0</arcs>
<nodes>0</nodes>
</Root>
But the information is stored into the arrays correctly.
Here is the code I am working with
my $xml = new XML::Simple(NoAttr => 1, RootName => 'Root', ForceArray => 1);
$xml->XMLout(
$table,
KeepRoot => 1,
OutputFile => $xml_directory . $out_file . ".xml",
XMLDecl => "<?xml version='1.0'?>",
NSExpand => 0,
ValueAttr => { \#node_values => 'node' }
);
Any ideas on how to expand out the arrays without having to hard code what you want your tags to be?
I would like to be able to go on the fly from data structure to XML for generation.
The statements
$table->{arcs} = #arcs;
$table->{nodes} = #nodes;
are scalar assignments, with the result that the hash elements are set to the number of elements in the respective arrays.
You should change the assignment to assign references to the arrays instead, like this:
$table->{arcs} = \#arcs;
$table->{nodes} = \#nodes;
However this XML result
<Root>
<arcs>0</arcs>
<nodes>0</nodes>
</Root>
shows that the sizes you are getting are zeroes, so the arrays are actually empty and this is only part of the story.
Please show your complete code so that we can see where you are going wrong.
I have this XML tree that looks like this (I've changed the tag names but if you're really clever you may figure out what I'm actually doing.)
<ListOfThings>
<Thing foo:action="add">
<Bar>doStuff --slowly</Bar>
<Index>1</Index>
</Thing>
<Thing foo:action="add">
<Bar>ping yourMother.net</Bar>
<Index>2</Index>
</Thing>
</ListOfThings>
With libxml2, I want to programmatically insert a new Thing tag into the ListOfThings with the Index being the highest current index, plus one. I do it like this (sanity checking removed for brevity):
xpath = "//urn:myformat[#foo='bar']/"
"urn:mysection[#name='baz']/"
"urn:ListOfThings/urn:Thing/urn:Index";
xpathObj = xmlXPathEvalExpression(xpath, xpathCtx);
nodes = xpathObj->nodesetval;
/* Find last value and snarf the value of the tag */
highest = atoi(nodes->nodeTab[nodes->nodeNr - 1]->children->content);
snprintf(order, sizeof(order), "%d", highest + 1); /* highest index plus one */
/* now move up two levels.. */
cmdRoot = nodes->nodeTab[nodes->nodeNr - 1];
ASSERT(cmdRoot->parent && cmdRoot->parent->parent);
cmdRoot = cmdRoot->parent->parent;
/* build the child tag */
newTag = xmlNewNode(NULL, "Thing");
xmlSetProp(newTag, "foo:action", "add");
/* set new node values */
xmlNewTextChild(newTag, NULL, "Bar", command);
xmlNewChild(newTag, NULL, "Index", order);
/* append this to cmdRoot */
xmlAddChild(cmdRoot, newTag);
But if I call this function twice (to add two Things), the XPath expression doesn't catch the new entry I made. Is there a function I need to call to kick XPath in the shins and get it to make sure it really looks over the whole xmlDocPtr again? It clearly does get added to the document, because when I save it, I get the new tags I added.
To be clear, the output looks like this:
<ListOfThings>
<Thing foo:action="add">
<Bar>doStuff --slowly</Bar>
<Index>1</Index>
</Thing>
<Thing foo:action="add">
<Bar>ping yourMother.net</Bar>
<Index>2</Index>
</Thing>
<Thing foo:action="add">
<Bar>newCommand1</Bar>
<Index>3</Index>
</Thing>
<Thing foo:action="add">
<Bar>newCommand2</Bar>
<Index>3</Index> <!-- this is WRONG! -->
</Thing>
</ListOfThings>
I used a debugger to check what happened after xmlXPathEvalExpression got called and I saw that nodes->nodeNr was the same each time.
Help me, lazyweb, you're my only hope!
Based on your XPath it looks like that is just a snippet of a namespaced document (using default namespace at that). I bet you made a prior call to register "urn" as the prefix for a namespace using xmlXPathRegisterNs. When you add the new nodes, you aren't creating them in namespaces so the XPath is correctly only picking the namespaced "Index" elements.