how would I go about counting the number of times a string occurs in a table?
Basically I have a table that is like, say 200 entires for example (but it is larger)... each entry has a sub entry called name.
so..
itemlist[i].name == somestring.
Now I can search and find a match pretty easy using an if statment while looping though the table...
if string.find(string.lower(itemlist[i].name), string.lower(searchString)) ~= nil then
So say I'm searching for Thomas, it will return when it finds "Thomas F Malone".
The thing is some cases there are more than one result for the search value.. for example.. say there are three different names that all start with Thomas.
At the moment it will just find the 1st occurrence of the Thomas.
So the plan is to count all the occurrences of Thomas and then output all of them... but I can not work out how to get the numeric value of how many times the result is found in the table.
TL;DR - How can I count the number of occurrences that of a string in a table?
When you found the matching values, store them in a temporary table, e.g.
table.insert(temporaryTable, value)
rather than quitting the function using return. You can find below, a sample function which gathers and counts the occurence of a query string in a variable inside of a multidimensional table (or see it in action here).
--data
itemList = {
{name = "Denny Kuhlman", id = "6688"},
{name = "Russell Leisy", id = "3751"},
{name = "Hilario Stermer", id = "1886"},
{name = "Thomas Hemming", id = "9666"},
{name = "Samuel Lafuente", id = "8232"},
{name = "Lazaro Ashby", id = "5274"},
{name = "Ronnie Nicosia", id = "9664"},
{name = "Edison Seyal", id = "1344"},
{name = "Jerald Officer", id = "9497"},
{name = "Lupe Burdge", id = "266"},
{name = "Stephan Iler", id = "5968"},
{name = "Josue Stephens", id = "2128"},
{name = "Salvador Ortmann", id = "3643"},
{name = "Tony Ricker", id = "8799"},
{name = "Corey Carbone", id = "6485"},
{name = "Conrad Theberge", id = "139"},
{name = "Arnulfo Oquendo", id = "2861"},
{name = "Damien Balsley", id = "5572"},
{name = "Efren Sloop", id = "7106"},
{name = "Blair Clagon", id = "614"},
{name = "Dario Service", id = "1411"},
{name = "Paul Ashalintubbi", id = "3403"},
{name = "Felix Veal", id = "1539"},
{name = "Laurence Caskey", id = "2827"},
{name = "Will Ranallo", id = "8463"},
{name = "Thomas Brenner", id = "9599"},
{name = "Claudio Hallmark", id = "6265"},
{name = "Nolan Haslett", id = "9661"},
{name = "Lenard Pereira", id = "5652"},
{name = "Dusty Duer", id = "4034"},
}
--
function countStringOccurence(query, itemList)
query = string.lower(query)
--if query string is found, store index of the itemList in table searchResult
local searchResult = {}
for i, item in ipairs(itemList) do
local name = string.lower(item.name)
if string.find(name, query) then
table.insert(searchResult, i)
end
end
--return both the occurence count and the list of found item
return #searchResult, searchResult
end
--execute the function
count, foundItemList = countStringOccurence("thomas", itemList)
--print results
print(count) --> 2
for i, index in ipairs(foundItemList) do
print(index, itemList[index].name) --> 4 Thomas Hemming
--> 26 Thomas Brenner
end
NB: Please note that if your table/list entries might be moved around (e.g. sort), it might not be advisable to just store the array index because the references/values collected in foundItemList might become incorrect.
Just don't stop iterating over the table once you found something. Keep on going and increment a counter variable every time you find your string or put your results into a table and count its elements later.
local texts = {"a123", "b213", "a332", "d411", "a124"}
local result = {}
for i,v in ipairs(texts) do
if string.find(v, "a") then
table.insert(result, v)
end
end
print(#result)
I want to create multiple relationships between the same node using py2neo library. I used create if the relationship does not exist and merge when it exists. Here is a sample of my code :
def create_route(graph, sourcefile, airport_nodes):
with open(sourcefile, encoding="utf8") as csvfile:
reader = csv.DictReader(csvfile)
fieldnames = reader.fieldnames
for row in reader:
source_airport = row['origin']
destination_airport = row['destination']
source_airport_node = airport_nodes[source_airport]
destination_airport_node = airport_nodes[destination_airport]
node_properties = {'distance':row['distance']}
node_properties1 = {'duration': row['duration']}
graph.create(Relationship(source_airport_node, destination_airport_node,**node_properties1))
graph.merge(Relationship(source_airport_node, destination_airport_node, **node_properties))
The problem that It get only one relationship with the last attribute which is the distance.
Thank you
I can't figure how to create a jira issue and define its component with SOAPpy:
client = so.WSDL.Proxy(cfg_wsld)
auth_token = client.login(cfg_username, cfg_password)
issue_params = dict()
issue_params['project'] = project
issue_params['type'] = issue_type
issue_params['summary'] = summary
issue_params['description'] = summary
newissue = client.createIssue(auth_token, issue_params)
This sample works fine but I try to add components to it Jira will return missmatchTypeException.
I've tried all kinds of variants: passing arrays, strings, ints into it but it won't pick any of them up.
Most attempts (passing string, int, array of both) will cause TypeMissmatch, this causes NullPointerException inside Jira:
issue_params['components'] = {u'Разное': {'id': '11143', 'name': u'Разное'}}
I know the exact id of the issue type I want to use but how do I pass it properly? When I retrieve an issue with this type components returns as SOAPpy.Types.typedArrayType() but this still fails:
issue_params['components'] = so.Types.typedArrayType(data={'id': '11143', 'name': u'Разное'})
newissue = client.createIssue(auth_token, issue_params)
(<class 'SOAPpy.Errors.Error'>, <Error : Data must be a sequence>, None)
issue_params['components'] = so.Types.typedArrayType(data=[{'id': '11143', 'name': u'Разное'},])
This did the trick - data needs to be an array.
I want to create an e4x object.
The I want to dynamically keep adding attributes for it and also add value later.
e.g
var node = <node />;
//some code
1) add attribute to 'node'
2) add value to 'node'
Also I found such examples for Flex3 but none for Javascript. Any further documentation would also be appreciated
if you want to add an attribute or value
var node = <node/>
node.#id = 123
node.toXMLString()
//returns
//<node id="123"/>
if you would like to add attributes named dynamically then use the square brackets
node.#["prioritory"] = "high"
//returns
//<node id="123" prioritory="high"/>
the same works for adding child elements
node.description = "Warning"
node.toXMLString()
//<node id="123" prioritory="high">
// <description>Warning</description>
//</node>
node["location"] = "R23"
node.toXMLString()
//<node id="123" prioritory="high">
// <description>Warning</description>
// <location>R23</location>
//</node>
I find this link helpful when trying to refresh my e4x http://wso2.org/project/mashup/0.2/docs/e4xquickstart.html
Im wondering how to achieve the following:
I have this Session Container created by a factory:
$container = new Container('Fans');
$container->setExpirationSeconds('219867583');
return $container;
then i'm creating an instance in my controller like this:
$this->sessionService = $this->getServiceLocator()->get('SessionService');
Now i want to add something to the Session:
This one works fine:
$this->sessionService->team = 'TEST';
But what i want to achieve is the following
$this->sessionService->team[0] = 'Team Name 0' // This doesn't work;
$this->sessionService->team[1] = 'Team Name 1' // This doesn't work;
\Zend\Debug\Debug::dump($this->sessionService->team);
The Output looks like this:
<pre>string(9) "TEST" </pre>
I don't know if i misunderstood something or do something wrong.
Does anybody know how to do it right ?
This is a problem with the magic __get() functionality in PHP. Because Zend\Session uses __get() to supply access to session variables, you cannot access them like arrays. What does work, is the following:
$team = array();
$team[0] = 'Team Name 0';
$team[1] = 'Team Name 1';
$this->sessionService->team = $team;
How about using an array?
$teams = array();
$session->teams = $teams;
$session->teams[0] = 'blubb';
If you first assign it to be a string, it will remain a string. Make it an array and use it as an array, too ;)