assigning associative array in smarty - arrays

So what I'm trying to achieve is assign multiple values in to a smarty array on the indexes that I want. It's hard to describe it, so I'll just show it:
{assign var='paymentTypes'
value=','|explode:"$paymentMethods[50],$paymentMethods[51],
$paymentMethods[11],$paymentMethods[10],$paymentMethods[12],
$paymentMethods[99],$paymentMethods[100]"}
(Formatted for readability)
So now I have a paymentTypes array with the values placed on the keys starting from 0. What I want is to keep the keys that were in paymentMethods array - 50, 51, 11.. etc.
And it has to be done totally in Smarty template file. Thanks for any ideas.

I've found a workaround. The needed associative array was for a HTML Select tag. So I just used {html_options} with value and output attributes instead of options. I assigned one array just for values and another just for output.
I had this:
{html_options options=$paymentTypes}
But I didn't want to show all of the values from paymentTypes array, but I had to have them in the array. So what I did was this:
{assign var='paymentTypesOutput' value=','|explode:"$paymentTypes[50],
$paymentTypes[51],$paymentTypes[11],$paymentTypes[10],$paymentTypes[12],
$paymentTypes[99],$paymentTypes[100]"}
{assign var='paymentTypesValues' value=','|explode:"50,51,11,10,12,99,100"}
{html_options values=$paymentTypesValues output=$paymentTypesOutput}
It's not the most elegant solution - but it works.

Related

How to get specific array within an array

I'm having trouble getting a specific set of values from an array of arrays because the values come up as aundefineda. my array is stored in state and set up like this:
survey: [{names: ["sara", "tom"]}, {age: ["17", "33"]}, {col: ["blue", "green"]}]
To access just the names, I've been trying to use the line:
console.log(this.state.survey.names)
When I check, the console has "undefined" for the names. When I check the value of survey, it shows up as an array of arrays, where each nested array has two values stored inside.
I want to check this because I am trying to map out the nested arrays and mapping out Survey names isn't working because the length shows 0. I am successful in mapping out nested arrays when they are not stored in state, so I am wondering what I am missing or if my syntax is incorrect.
You're trying to access an array as if it was an object, instead of an array of objects. A simple fix is -
this.state.survey.find(item => item.names).names
In the long term, unless you have a reason to store this as an array, it would be a lot easier to reorganize and store as objects. If you restructure this to be something like
survey: {
names: ['name1', 'name2'],
age: ...etc
If you are trying to say that Sara is 17 and her col is blue, why not just store that as a single object?

How to destructure an id out of an array in Rails

I'm getting an array back from redis (trhough a controller for my projects) which I need to destructure in my product view.
Array (showing 2 results, but it can many more):
["project-5", "project-4"]
The numbers (5, 4) are my project id's (#project.id) which I need to subtract to use in a .each do function.
All that I can find for it, are destructure solutions for multiple variables. But here I only have 1 variable.
Can anyone help me out with how I can isolate the product id out of this array?
You can use match method to get the ids with the help of regex. Assuming array consists of your projetcs with id-s 3 and 44 then a one-liner would be:
2.0.0-p353 :091 > array.map{|el| el.match(/\d+$/)[0]}
=> ["3", "44"]
If you sure need to use .each do then
array.each do |el|
el.match(/\d+$/)[0]
end
You can also use method gsub with regex to replace everything except the id inside string with an empty string, so only id-part of the string will remain.
One-liner for this (array = ["proj3ct-3", "project-1567"]):
2.0.0-p353 :040 > array.map {|el| el.gsub(/(.*\-)[^\d]*/, "")}
=> ["3", "1567"]
.each do similarly to previous example with match.
EDIT: If you want the do-block to return the array already in formatted form then you should use bang-method from gsub like this: el.gsub!(/(.*\-)[^\d]*/, "") This will also make changes in elements. Non-bang methods only return changed values without altering them.
Just in case you don't know and would like to test/analyze your regex then this is a good place to do it- I've been using it for quite a while. Also a good place to recheck some basics is here

how to modify an array value with given index?

I want to modify an array cell, which I can do when I know the cell as a number. However here my cell position is given by $i.
pomme[`${i}`]=""
I tried without the `` and it doesn't work either?
How am I suppose to do it?
You don't need the quotes. Just use ${i}, or even $i:
pomme[${i}]=""
Or
pomme[$i]=""

How to derive values from an array using the index value

I am trying to use an array in NetLogo to store my images and call the values using their index. Looks like I am getting stuck with common manner of accessing the array's value via arrayName[0].
How do I do that in NetLogo? googling doesn't seem to have the answer.
My array:
let imgArray ["easy1.png" "easy2.png" "easy3.png" "easy4.png" "easy5.png"]
I am trying to fix the image in the following manner:
clear-drawing import-drawing imgArray[1]
You're looking for item:
item 1 imgArray
Note that it's zero-indexed, so the first item is item 0 imgArray, though first imgArray is more idiomatic.
Also, arrays in NetLogo are called lists.

How to copy cell to array in Matlab

Alright let me explain detailed my question
this below image is displaying my matrix where i want to copy my data
Alright now what i want to do that is as you can see 1x4 cell
i want to copy it as an array to another variable such as
input_values=ones(1,4);%init
input_values=input_matrix_training(1);
So at the above i am trying to copy the elements in that cell array which is row 1 to the input_values array. But if i do as i above i am getting this instead of the values that array contains. ty
instead of above it should be like
The other values are a cell, and are thus best referenced with {} instead of (). Also, sometimes they need to be wrapped into [], depending on the format. Plus the fact that you don't need to initialize input_values, and what you should do becomes this:
input_values=[input_matrix_training{1}];
Or you can just use cell2mat
input_values=cell2mat(input_values(1));

Resources