How can I tell if E4X expression has a match or not? - apache-flex

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!

In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}

OK I got it to work with this:
if(String(defaultItem.#style_number).length)

Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

Related

How to extract a string value from an array using scripted field in kibana?

Is there a way to extract a string value from an array with the use of if statement in scripted field in kibana. I tried the below code, however, I am unable to filter out the correct and incorrect values in discover tab of kibana. This might be due to remark field is an array.
def result_string = "";
if (doc['nac.keyword'].value =="existing_intent" &&doc['remark.keyword'].value != "acceptable") {
result_string = "incorrect";
}
if (doc['nac.keyword'].value =="existing_intent" &&doc['remark.keyword'].value == "acceptable") {
result_string = "correct";
}
return result_string;`
You can use the contains method defined on Array to check for element membership:
!doc['remark.keyword'].value.contains("acceptable") //does not contain
For this, you might want to ensure first that doc['remark.keyword'].value is indeed an Array.

Loadrunner Parameters in JSON String

I'm trying to use a parameter inside of a JSON string, and would like to use an inner parameter to replace an GUID. I've changed the default parameter start and end characters since curly braces are used in JSON.
I've tried to do something like this, where the json param contains my json which is similar to this below.
{"DashboardGUID":"<Dash_GUID>"}
request_json = lr_eval_string("<json>");
lr_save_string(request_json, "request_json_param");
I'm expecting the lr_eval_string to replace the with the GUID that's in this parameter, what's the best why of replacing this ID in my JSON String?
Not sure what you are asking but I will put this here in case someone comes here in the future:
main.c
Action()
{
lr_eval_json("Buffer/File=my_json.json", "JsonObject=MJO",LAST);
lr_json_stringify("JsonObject=MJO","Format=compact", "OutputParam=newJsonBody",LAST);
lr_save_string(lr_eval_string(lr_eval_string("{newJsonBody}")),"tmp");
web_reg_find("Text={mydate}",LAST);
web_rest("POST",
"URL=http://myServer.microfocus.com/url",
"Method=POST",
"EncType=raw",
"Body={tmp}",
HEADERS,
"Name=Content-Type", "Value=application/json", ENDHEADER,
LAST);
return 0;
}
my_json.json
{
"LastActionId": 0,
"Updated": "{mydate}"
}
Okay so instead of doing what I'm thinking above I ended up creating an array of char's with this {"DashboardGUID":"<Dash_GUID>", someotherdata:"123"} in 10 different positions within the array. I then randomly selected an element from this array and when doing the lr_eval_string the parameter was replaced.
Hopefully this makes sense those looking to do something similar.

Asp.net mvc razor view string.format does not seems to work

I am using string.format to format my model value inside razor view but it does not gives desired result
#string.Format("{0:00}", Model.Range == null ? "" : Model.Range.ToString())
It should result as 05
if i am using below it gives me result but not from model
#string.Format("{0:00}", 5)
Someone have any idea or same experience ?
If Model.Range is a number type then you need to write:
#string.Format("{0:00}", Model.Range == null ? "" : Model.Range)
because with the Model.Range.ToString() you have converted your Range to string so the number formatting cannot be applied because it is not a number anymore.
By the way string.Format handles null arguments so it is enough to write:
#string.Format("{0:00}", Model.Range)
If Model.Range is not a number but with Model.Range.ToString() you get a number in a string representation then you need to first convert it to a number (like using int.Parse or its other variants) then you can pass the number to string.Format which can now apply the correct formatting.

What is wrong with this code? why the List does not identify?

what is wrong with this code?
bool claimExists;
string currentClaimControlNo = "700209308399870";
List<string> claimControlNo = new List<string>();
claimControlNo.Add("700209308399870");
if (claimControlNo.Contains(currentClaimControlNo.Substring(0, 14)))
claimExists = true;
else
claimExists = false;
Why the claimControlNo above is coming into false?
Since I know the value exists, how can i tune the code?
It's reporting false because you aren't asking whether the list contains the currentClaimControlNo, you're asking whether it contains a string that is the first fourteen characters of the fifteen-character string currentClaimControlNo.
Try this instead:
claimExists = claimControlNo.Any(ccn => ccn.StartsWith(currentClaimControlNo.Substring(0,14)));
Your count is wrong. There are 15 characters. Your substring is cutting off the last 0 which fails the condition.
Because you're shaving off the last digit in your substring.
if you change the line
if (claimControlNo.Contains(currentClaimControlNo.Substring(0, 14)))
to
if (claimControlNo.Contains(currentClaimControlNo.Substring(0, 15)))
it works.
Because contains on a list looks for the whole item, not a substring:
currentClaimControlNo.Substring(0, 14)
"70020930839987"
Is not the same as
700209308399870
You're missing a digit, hence why your list search is failing.
I think you are trying to find something in the list that contains that substring. Don't use the lists contain method. If you are trying to find something in the list that has the subset do this
claimExists = claimControlNo.Any(item => item.Contains(currentClaimControlNo.Substring(0, 14)))
This goes through each item in claimControlNo and each item can then check if it contains the substring.
Why do it this way? The Contains method on a string
Returns a value indicating whether the specified System.String object occurs within this string.
Which is what you want.
Contains on a list, however
Determines whether an element is in the System.Collections.Generic.List.
They aren't the same, hence your confusion
Do you really need this explaining?
You are calling Substring for 14 characters when the string is of length 15. Then you are checking if your list (which only has one item of length 15) contains an item of length 14. It doesn;t event need to check the value, the length is enough to determine it is not a match.
The solution of course is to not do the Substring, it makes not sense.
Which would look like this:
if (claimControlNo.Contains(currentClaimControlNo))
claimExists = true;
else
claimExists = false;
Then again, perhaps you know you are trimming the search, and are in fact looking for anything that has a partial match within the list?
If this is the case, then you can simply loop the list and do a Contains on each item. Something like this:
bool claimExists = false;
string searchString = currentClaimControlNo.Substring(0, 14);
foreach(var s in claimControlNo)
{
if(s.Contains(searchString))
{
claimExists = true;
break;
}
}
Or use some slightly complex (certainly more complex then I can remember off the top of my head) LINQ query. Quick guess (it's probably right to be fair, I am pretty freaking awesome):
bool claimExists = claimControlNo.Any(x => x.Contains(searchString));
Check it:
// str will be equal to 70020930839987
var str = currentClaimControlNo.Substring(0, 14);
List<string> claimControlNo = new List<string>();
claimControlNo.Add("700209308399870");
The value str isn't contained in the list.

Using strings to access custom methods dynamically

I am creating a two-dimensional list that has consecutive numbers at the end of "day", for use as dataProvider for a DataGrid
i have been accessing them via the command
dg1.selectedItem.day1
dg1.selectedItem.day2
dg1.selectedItem.day3
etc...
is there any way to take the string ("day"+i) and convert it into a (what is it? variable name?)
so that i can do something along the lines of:
for(var i:Number=1; i<numFields; i++)
{
dg1.selectedIndex = i-1;
dg1.selectedItem.(mysteryFunction("day"+i)) = 42;
}
if there's a function that i could use for mysteryFunction, or what data type to use, any info would be very helpful
this is what i've been using (so tedious):
<mx:XMLList id="sched">
<field>
<day1></day1>
<day2></day2>
<day3></day3>
</field>
<field>
<day1></day1>
<day2></day2>
<day3></day3>
</field>
...
</mx:XMLList>
The "mystery function" you are looking for is as simple as indexing with brackets:
for(var i:Number=1; i<numFields; i++)
{
dg1.selectedIndex = i-1;
dg1.selectedItem["day"+i] = 42;
}
And it is called, surprisingly, an attribute.
Use an Array or if you are going to bind it (which I am kind of betting on) use ArrayCollection instead of naming these variables individually.
If the members are generated by some program, you better put all of these in one of the collection classes I mentioned above and then start the processing. It makes life easier in the long run.
E4X is the way to go when dealing with XML. The Mozilla guys have an arguably better explanation of that technology. So, if your XML is stored in a variable as:
var tree:XML = <field>
<day1></day1>
<day2></day2>
<day3></day3>
You can simply do:
tree.day1 = 42;
Why would you want this mysteryFunction()? A dataProvider object is just a collection of some Type. You know the type already, right? Read this.
Anyway, there is no such mystery function. Note, however, string concatenation with a number converts the number to a string. Try
trace("str " + 42);

Resources