Array.size() returned wrong values (Grails) - grails

I'm developing an app using Grails. I want to get length of array.
I got a wrong value. Here is my code,
def Medias = params.medias
println params.medias // I got [37, 40]
println params.medias.size() // I got 7 but it should be 2
What I did wrong ?
Thanks for help.

What is params.medias (where is it being set)?
If Grials is treating it as a string, then using size() will return the length of the string, rather than an array.
Does:
println params.medias.length
also return 7?
You can check what Grails thinks an object is by using the assert keyword.
If it is indeed a string, you can try the following code to convert it into an array:
def mediasArray = Eval.me(params.medias)
println mediasArray.size()
The downside of this is that Eval presents the possibility of unwanted code execution if the params.medias is provided by an end user, or can be maliciously modified outside of your compiled code.
A good snippet on the "evil (or lack thereof) of eval" is here if you're interested (not mine):
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

I think 7 is result of length of the string : "[37,40]"
Seems your media variable is an array not a collection
Try : params.medias.length

Thanks to everyone. I've found my mistake
First of all, I sent an array from client and my params.medias returned null,so I converted it to string but it is a wrong way.
Finally, I sent and array from client as array and in the grails, I got a params by
params."medias[]"

List medias = params.list('medias')
Documentation: http://grails.github.io/grails-doc/latest/guide/single.html#typeConverters

Related

How to correctly return a list of dictionaries in Zapier Code (Python)?

The Zapier code documentation says that the output of a code zap can be either a dictionary or a list of dictionaries (See "Data Variable" section: https://zapier.com/help/code-python/).
When doing this,
output = [{'Booking':'Shirt'},{'Booking':'Jeans'}]
the output of the code returns only the first dictionary, however:
runtime_meta__duration_ms: 2
runtime_meta__memory_used_mb: 22
id: [redacted]
Booking: Shirt
Fields with no value:
runtime_meta__logs
What am I doing wrong here? Thanks a lot!
David from the Zapier platform team here. Code steps returning an array is a mostly undocumented (because there's no UI support and it's confusing, as you can tell) feature.
When testing, it'll only show the first item in the array. When it runs for real, all steps after the code step will run for each item in the array. The task history will reflect this
So set up the zap and turn on and it'll work like you expect.
Sorry for the confusion and let me know if you have any other questions!
For anyone still looking for an answer to this questions, below is what find out returning list in Zapier.
# first import and convert your input value to an array.
# special note any line items imported into a python variable are converted to list format.
my_items = input_data['my_CSV_string']
my_list_of_items = my_items.split(",")
# Create a new list array
my_new_list = []
length = len(my_list_of_items)
#Do all your computations
for i in range(length):
my_new_list.append(float(my_list_of_items[i])*1.5)
# After completing any tasks you can return the list as follows,
# If you are using line items keep the list in its original format
return {
'my_processed_values': my_new_list,
'original_values': my_list_of_items
}
# If you want to return it as a CSV "basically making the array flat"
my_old_CSV_list= ','.join(map(str, my_list_of_items))
my_new_CSV_list= ','.join(map(str, my_new_list))
return {
'my_processed_cvs_values': my_new_CSV_list,
'original_values': my_list_of_items
}
Hope this helps. I am not a Python expert but in theory the more lists used the longer the zap will take to process. Try to keep your python processing time to the lowest.
Best,

LoadRunner web_reg_save_param, ord=all, paramName_count issues

I am using LoadRunner version 12.02 - Build 2739
Looking at an old, yet correct 'guide' shows that I have used the statements correctly (3rd Point, around 1/3rd of the way down the page, inside the code block - the atoi statement).
But I am still unable to convert the _count parameter to an int-Variable.
In the script, before the call is made;
web_reg_save_param(
"ParamName=rotaPeople",
"LB=someText",
"RB=\")",
"Ord=ALL",
LAST);
After the web call the save_param function is placed before, the output log shows;
Notify: Saving Parameter "ParamName=rotaPeople_count = 21".
Inside the script, after the call is made, and the count has been totaled;
lr_output_message("RP_C:%d",lr_eval_string("{rotaPeople_count}"));
lr_output_message("RP_C:%s",lr_eval_string("{rotaPeople_count}"));
peoplesCount = atoi(lr_eval_string("{rotaPeople_count}"));
lr_output_message("PC:%d",peoplesCount);
In the logs after the above executions are made;
Warning: The string 'rotaPeople_count' with parameter delimiters is not a parameter.
RP_C:110826864
Warning: The string 'rotaPeople_count' with parameter delimiters is not a parameter.
RP_C:{rotaPeople_count}
Warning: The string 'rotaPeople_count' with parameter delimiters is not a parameter.
PC:0
Anyone have any ideas?
Note: The Warning messages are expected
Note: Workaround: Used web_reg_save_param_regex() and created a regular expression. Using the returned _count parameter within a for-loop worked. Keeping question open, as the original problem still persists
The problem is that you use a soon to be deprecated API web_reg_save_param which does not support the ParamName syntax. In this API the second parameter is always the parameter name and therefore the correct use would be:
web_reg_save_param(
"rotaPeople",
"LB=someText",
"RB=\")",
"Ord=ALL",
LAST);
The proper API to use is web_reg_save_param_ex which does support the syntax you used so the call should look like:
web_reg_save_param_ex(
"ParamName=rotaPeople",
"LB=someText",
"RB=\")",
"Ord=ALL",
LAST);
Then the rest of your code should work properly.
I am not sure what you are doing but you might want to take a look at the somewhat unknown API lr_paramarr_random which will automatically pull a random value from the parameters array.
This should help you
web_reg_save_param(
"rotaPeople",
"LB=someText",
"RB=\")",
"Ord=ALL",
LAST);
lr_output_message("PC:%d",atoi(lr_eval_string("{rotaPeople_count}")));
You are using ord=all,see the run time data which value you want to capture,If you want to capture the 10th value please use ord=10,automatically this warning will remove from output log.
Example for capturing an array of dynamic values:
Action()
{
int i;
int ncount;
char ParamName[100];
web_set_sockets_option("SSL_VERSION", "TLS");
web_reg_save_param("trackingno","LB=;","RB= (NTN 0430)","search=All","ord=all",LAST);
web_submit_data("barcode.pl",
"Action=http://qtetools.rmtc.fedex.com/barcode/cgi-bin/barcode.pl",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://qtetools.rmtc.fedex.com/barcode/html/barcode.shtml",
"Snapshot=t2.inf",
"Mode=HTML",
ITEMDATA,
"Name=formcode", "Value=0430", ENDITEM,
"Name=count", "Value=10", ENDITEM,
"Name=narrow", "Value=2", ENDITEM,
LAST);
ncount= atoi(lr_eval_string("{trackingno_count}"));
for (i =1;i <= ncount;i++)
{
sprintf(ParamName, "{trackingno_%d}", i);
lr_output_message("Value of %s: %s",ParamName,lr_eval_string(ParamName));
}
return 0;
}

action script 2.0 simple string comparing with conditional statement

So I have this crazy problem with comparing 2 strings in ActionScript 2.0
I have a global variable which holds some text (usually it is "statistic") from xml feed
_root.var_name = fields.firstChild.attributes.value;
when I trace() it it gives me the expected message
trace(_root.var_name); // echoes "statistik"
and when I try to use it in conditional statement the rest of code is not being executed because comparing :
if(_root.overskrift == "statistik"){
//do stuff
}
returns false!
I tried also with:
if(_root.overskrift.equals("statistik"))
but with the same result.
Any input will be appreciated.
Years later but on a legacy project I had the same problem and the solution was in a comment to this unanswered question. So let's make it an anwer:
Where var a:String="some harmless but in my case long string" and var b:String="some harmless but in my case long string" but (a==b) -> false the following works just fine: (a.indexOf(b)>=0) -> true. If the String were not found indexOf would give -1. Why the actual string comparison fails I coudn't figure out either. AS2 is ancient and almost obsolete...

JSON Parsing issue in Rails

I am seeing a strange issue in Rails.
Request Body (request.body):
renewals[][driver_1][dl_number]=123&
renewals[][driver_1][expiration_date]=20130513&
renewals[][driver_1][last_name]=123&
renewals[][driver_1][state]=AL&
renewals[][driver_1][verified]=1&
renewals[][driver_2][verified]=0&
renewals[][id]=6415&
renewals[][insurance][expiration_date]=20130513&
renewals[][insurance][naic]=123&
renewals[][insurance][policy_number]=123&
renewals[][insurance][verified]=1&
renewals[][mailing_address][address_has_changed]=0&
renewals[][mailing_address][city]=GULF%20SHORES&
renewals[][mailing_address][state]=AL&
renewals[][mailing_address][street_address]=8094%20BEACH%20LANE&
renewals[][mailing_address][zip]=35023&
renewals[][driver_1][dl_number]=123&
renewals[][driver_1][last_name]=123&
renewals[][driver_1][state]=AL&
renewals[][driver_1][verified]=1&
renewals[][driver_2][verified]=0&
renewals[][id]=6412&
renewals[][insurance][expiration_date]=20130513&
renewals[][insurance][naic]=123&
renewals[][insurance][policy_number]=123&
renewals[][insurance][verified]=1&
renewals[][mailing_address][address_has_changed]=0&
renewals[][mailing_address][city]=HUEYTOWN&
renewals[][mailing_address][state]=AL&
renewals[][mailing_address][street_address]=123%20ANY%20LANE&
renewals[][mailing_address][zip]=35023&
renewals[][driver_1][dl_number]=123&
renewals[][driver_1][last_name]=123&
renewals[][driver_1][state]=AL&
renewals[][driver_1][verified]=1&
renewals[][driver_2][verified]=0&
renewals[][id]=6411&
renewals[][insurance][expiration_date]=20130513&
renewals[][insurance][naic]=123&
renewals[][insurance][policy_number]=123&
renewals[][insurance][verified]=1&
renewals[][mailing_address][address_has_changed]=0&
renewals[][mailing_address][city]=HUEYTOWN&
renewals[][mailing_address][state]=AL&
renewals[][mailing_address][street_address]=104%20MERRIMONT%20ROAD&
renewals[][mailing_address][zip]=35023&
JSON Parsed Params (params[:renewals]): https://gist.github.com/t2/5566652
Notice in the JSON that the driver_1 information is missing on the last record. Not sure why this is. The data is in the request. Any known bug I am missing? Let me know if you need more info.
Unfortunately this is just how Rails parses JSON like this (where your [] is massively nested). I've come up against this before - http://guides.rubyonrails.org/form_helpers.html#combining-them gave some explanation.
From what I remember, if you can put in numeric keys rather than just [] (i.e. [1] for the first one, [2] for the second etc.) then it will work as you want it to.
So I figured it out. I needed to set the requestSerializationMIMEType to RKMIMETypeJSON.

Can't turn array into a string error

I have a simple call
JSON.parse(Panda.get("/videos/#{self.panda_video_id}/encodings.json"))
Which returns :
can't convert Array into String
This is because the Panda.get("/videos/#(self.panda_video_id}/encodings.json") call returns an array in the new Panda 1.0.0 gem.
I also tried :
JSON.parse(Panda.get("/videos/#{self.panda_video_id}/encodings.json").to_s)
This returns:
705: unexpected token at 'created_at2010/07/19 20:28:13 +0000video_id4df3be7b6c6888ae86f7756c77c92d8bupdated_at2010/07/19 20:28:30 +0000started_encoding_at2010/07/19 20:28:21 +0000id6e2b35ad7d1ad9c9368b473b8acd0abcextname.mp4encoding_time0encoding_progress100file_size513300height110statussuccesswidth200profile_idf1eb0fe2406d3fa3530eb7324f410789'
Question
How would you turn the call at the top so that it returns a string?
does the following work:
panda_data = Panda.get("/videos/#{self.panda_video_id}/encodings.json")
JSON.parse(panda_data.to_s)
if it doesn't what is the error output?
If panda_data is an array, panda_data.to_s is guaranteed to return a string
Not that anyone had a chance at this, but
Panda_Gem since -v=0.6 has made all Panda.[get, post, etc.] requests return a hash. So you don't need the JSON.parse anymore. Removing the JSON.parse allows it to work.

Resources