I have an array which holds a structures which represents form fields of a web page. My question is how can I easily access data in the array to get the value I am looking for. In this case I am trying to get the value for the field with the name "availableScreensCSV" for now I am looping thru the array and if the key = "name' and is "availableScreensCSV" then I get the data for the value. I hope there is a simpler way to do this. Below is what my array / structure looks like
array
1 struct
NAME templateId
TYPE hidden
VALUE [empty string]
2 struct
NAME useTestSystem
TYPE hidden
VALUE false
3 struct
NAME availableScreensCSV
TYPE hidden
VALUE ICASR,ICADM,ICTQ1,ICTQA,ICTQB,ICTQ6,ICFGB
4 struct
NAME ccna
TYPE hidden
VALUE IFX
5 struct
NAME pon
TYPE hidden
VALUE I11192014013
6 struct
NAME asr
TYPE hidden
VALUE 1432310020
7 struct
NAME icsc
TYPE hidden
VALUE SW80
My gut reaction is, "how did your data get into that array?" If it's a form post, you should be able to simply get form fields by name: form.availableScreensCSV -- but that hinges on your form post.
Since you neglected to mention, I'm assuming that the array may not always be in the same order, in which case looping is your only reasonable option. There are functional approaches like arrayFind with an inline function but that is some syntactic sugar on top of a loop. If you're just looking for cleaner, more readable code, then arrayFind with an inline function may be the way to go.
If you're looking for the most performant option, then a simple loop that does a <cfbreak /> once it finds the desired element will be the best option.
Related
I'm trying to connect two parameters. In the first, the user inputs a file which contains a list (each line is one item). In the second, I'm hoping to set a parameter of type Field or GPValueTable. Here's a look at how this part of the code currently looks like:
def getParameterInfo(self):
#Define parameter definitions
# Input Features parameter
in_features = arcpy.Parameter(
displayName="Input Features",
name="in_features",
datatype="DETextFile",
parameterType="Required",
direction="Input")
# User selection
selection_field = arcpy.Parameter(
displayName="Selection list",
name="selection_field",
datatype="Field",
parameterType="Required",
direction="Input")
selection_field.parameterDependencies = [in_features]
# Derived Output Features parameter
out_features = arcpy.Parameter(
displayName="Output Features",
name="out_features",
datatype="GPFeatureLayer",
parameterType="Derived",
direction="Output")
out_features.parameterDependencies = [in_features.name]
parameters = [in_features, selection_field]
return parameters
The text file looks like this:
A
B
C
The toolbox dialog output is just A. I'm having a hard time understanding what ArcGIS intended to create here. Perhaps I'm using the wrong data types, but their parameter explanation doesn't make it very clear.
Any ideas?
You are confused with the arcpy terminology, I think. The second parameter's datatype="Field" does not imply data can be read/parsed from a text file where the data columns are written, in fact it is the schema/fields of a proper table and expectation is not a simple text but the field objects of a table/feature class (in the toolbox's run-time, assignment to arcpy.Parameter yields a geoprocessing value object but it is a different discussion). If you look at the "Creating value table parameters" section here, GPFeatureLayer's (param0) fields are populated in param1 automatically when the dependency is set.
The solution is setting your selection_field to GPValueTable and populating the selection_field.filters[0].list by reading the in_features's content after the parameter is altered but has not validated in def updateParameters(...). Have a look at https://gis.stackexchange.com/questions/370250/updating-valuetable-parameter-of-arcpy-python-toolbox-tool.
I am trying to write a xquery to get the Value for a specific name .Below is the request payload: i.e. if the Name ="ID" then get the
"Value" for that tag (i.e.1000000000.)If the Name="User" get the "Value" for that tag( ie."US").
<not:Items xmlns:v5="http://www.example.com"
xmlns:com="http://commom.com
xmlns:not="http://services.not.com"
xmlns:com1="http://common1.com">
<not:Array>
<com1:Item>
<v5:List>
<com:extensionsItem>
<com:Name>ID</com:Name>
<com:Value>1000000000</com:Value>
</com:extensionsItem>
<com:extensionsItem>
<com:Name>User</com:Name>
<com:Value>US</com:Value>
</com:extensionsItem>
</v5:List>
</com1:Item>
</not:Array>
</not:Items>
I tried the options below:
<ns2:ID>{fn:data($Items/not:Array/com1:Item/v5:List/com:extensionsItem[1]/com:Value)}<ID>
This statement works . But I cannot assure that ID will always come in first element in the array of List.So i want a statement that will work even if ID comes in any other
place in the array and I can retrieve the value.
Thanks in advance
I think you simply want to apply a predicate $Items/not:Array/com1:Item/v5:List/com:extensionsItem[com:Name = 'ID']/com:Value
I have a list of lists in the format below. This is data coming from a csv and I am trying to emulate the data review function that excel has in python. The only reason I can't do it directly in excel is this document is almost 1GB and has 1.1 mil row.
((a1,b1,c1,d1,e1),(a1,b2,c1,d2,e2),(a1,b1,c2,d3,e3),(a2,b1,c1,d3,e4),(a2,b2,c2,d3,e5)...)
I want to convert it into a single data structure something like a multidimensional array. like below
((a1:(b1:(c1:(),c2:()),b2:(),b3:()),a2:(b1:(c1:()),b2:(c2:()),b3:())))
I use autovivify class for other purposes but I can't use it here because some of the keys I want to use are strings. Appreciate help here.
If I understand your question correctly, you want to transform that list into a tree-like structure, where each tuple in the list represents one path down the tree. You can do this using nested dictionaries:
def add_to_dict(d, t):
if t:
first, rest = t[0], t[1:]
nested = d.setdefault(first, {})
add_to_dict(nested, rest)
Given a dictionary d (initially empty) and one of those tuples t, if that tuple is not empty, it takes the first element from the tuple, adds a nested dictionary to the original dictionary using this element as key (or takes one that already exists in this place), and adds the rest of the tuple to that dictionary in the same way.
Example using your data:
data = (('a1','b1','c1','d1','e1'),
('a1','b2','c1','d2','e2'),
('a1','b1','c2','d3','e3'),
('a2','b1','c1','d3','e4'),
('a2','b2','c2','d3','e5'))
d = {}
for t in data:
add_to_dict(d, t)
The resulting dictionary d looks like this:
{'a1': {'b1': {'c1': {'d1': {'e1': {}}},
'c2': {'d3': {'e3': {}}}},
'b2': {'c1': {'d2': {'e2': {}}}}},
'a2': {'b1': {'c1': {'d3': {'e4': {}}}},
'b2': {'c2': {'d3': {'e5': {}}}}}}
I need to access to items stored in a parameter that represents selected elements in a multiselect. I pass selected items from gsp to controller with the following code into the remoteFunction:
params: '\'receiptItemsSelected=\' + jQuery(this).val()'
Now, following the code found in discussion here, I use the closure to get each value, but if I perform a multiselect, the size of receiptItemsSelected is always 1, but value is, for example, 1,2. To get values as a list I've done the following in the controller
params.list("receiptItemsSelected")
but it does not give me two elements if I select two items in the multiselect, but always one element.
The question is: if I select two elements, how can I get each element and use it in the controller? And how can I have that elemnts as Long and not as String?
Thanks
If you're parameters are being passed with string representation of a list, e.g.:
http://yoursite.com/?receiptItemsSelected=1,2,3
You have to split the value using normal Groovy string manipulation and perform the type conversion yourself:
def receiptsAsLongs = params.receiptItemsSelected.split(',')*.toLong()
If your parameters are passed with the convention of repeated parameters makes a list, e.g.:
http://yoursite.com/?receiptItemsSelected=1&receiptItemsSelected=2
Then grails can convert this to a list for you using params.list(), but you must do the final String to Long conversion:
def receiptsAsLongs = params.list('receiptItemsSelected')*.toLong()
params.list() is intended for multi-valued parameters, i.e. it will work if you have
receiptItemsSelected=1&receiptItemsSelected=2
You may have more luck using serialize() rather than val() to build the request body.
I'm using jsoncpp parser (http://jsoncpp.sourceforge.net) to parse JSON data.
So, if we have the following JSON:
{ "name": "Joseph", "age": 20 }
How can I get the property name name and value Joseph, ... after age and 20?
OK, we can do universally this:
string e = root.get(propertyName, defaultValue).asString();
But really what we want is something like this:
string e = root.get(name, "Mark").asString();
Now, variable e is Joseph, it works. But I have to take/write "name". I do not want to QUERY (without questioning the function I want to get "name" (name of property) and "Joseph" (value of property)).
After it would be best to store in a field (C/C++ for example):
property[name][0] = "Joseph"
property[age][0] = 20
How can I do that? Or any other ideas?
You can get all the member names of a Json::Value object using its getMemberNames() function. That returns an object that you can iterate over using .begin() and .end(), like any other standard library container. (In fact, the return type is an alias for std::vector<std::string>.)
After you have the member names, you should be able to iterate through them and use .get(std::string &, const ValueType &) as you already are doing to get the values for each of the object's keys.
Note that JSON objects are inherently unordered, so you can't necessarily rely on that name list having any ordering whatsoever. If you want an ordered object, you should be using JSON arrays, not JSON objects.