CakePHP and set:combine with a default value - cakephp

i have an array.
At some stage, i'm adding more data to it.
So we have:
$editable = someArrayGeneratingFunctionHere();
$points = preg_split('/,/',$this->data['Video']['points']);
lovely. Now, the "points" array has a bunch of data that may or may not already be in the editable array.
What i want is to check if the data is in editable, and add if not.
I'd like to do this efficiently too.
So, i have this method:
private function associateWithRelatedBodyParts($editable, $keysAlreadyPresent, ...){
$point = getOtherPointsThatAreRelatedToThisPoint();
if (!isset($keysAlreadyPresent[$point])){
insertDataIntoEditable();
} //else the value is already here. Do not add it again!
return $editable;
}
so the whole thing looks like this:
$editable = someArrayGeneratingFunctionHere();
$points = preg_split('/,/',$this->data['Video']['points']);
$valuesInEditable = ...
foreach ($points as $point){
$editable = $this->associateWithRelatedBodyParts($editable, $valuesInEditable,...);
}
What a lot of setup! The whole point of all this is thus:
i want to flatten the original $editable array, because that way, i can quickly test if a point is in the editable. If it is not, i'll add it.
My current way to retrive the valuesInEditable array is
$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id','BodyPartsVideo.{n}.body_part_id');
This is moronic. I'm sticking the same value twice into the array. What i'd really like is just:
$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id',True);
or something like that. So the whole point of this question is:
how do i set a default value using Set:combine in cakephp. If you have a better suggestion, i'd love to hear it.

Without seeing the structure of your array, it seems like you are going through a lot of work to combine the two arrays. Is there a reason you cannot use
$final_array = array_merge($points, $editable);
before running the set combine?

Related

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

Storing values obtained from for each loop Scala

Scala beginner who is trying to store values obtains in a Scala foreach loop but failing miserably.
The basic foreach loop looks like this currently:
order.orderList.foreach((x: OrderRef) => {
val references = x.ref}))
When run this foreach loop will execute twice and return a reference each time. I'm trying to capture the reference value it returns on each run (so two references in either a list or array form so I can access these values later)
I'm really confused about how to go about doing this...
I attempted to retrieve and store the values as an array but when ran, the array list doesn't seem to hold any values.
This was my attempt:
val newArray = Array(order.orderList.foreach((x: OrderRef) => {
val references = x.ref
}))
println(newArray)
Any advice would be much appreciated. If there is a better way to achieve this, please share. Thanks
Use map instead of foreach
order.orderList.map((x: OrderRef) => {x.ref}))
Also val references = x.ref doesn't return anything. It create new local variable and assign value to it.
Agree with answer 1, and I believe the reason is below:
Return of 'foreach' or 'for' should be 'Unit', and 'map' is an with an changed type result like example below:
def map[B](f: (A) ⇒ B): Array[B]
Compare To for and foreach, the prototype should be like this
def foreach(f: (A) ⇒ Unit): Unit
So If you wanna to get an changed data which is maped from your source data, considering more about functions like map, flatMap, and these functions will traverse all datas like for and foreach(except with yield), but with return values.

Is it possible to apply changes to all objects in Array without using "for each" or "for" in Swift 3?

for example
var imageViewArray:UIImageView = [imageView1,imageView2,imageView3]
I want to chage sameimageView.image = img or imageView.isUserInteractionEnabled = false to all Image View inside the array
1. It's an array
First of all it's not
var imageViewArray:UIImageView
but
var imageViewArray:[UIImageView]
because you want an array of UIImageView right?
2. Naming conventions
Secondly is Swift we don't name a variable after it's type so imageViewArray becomes imageViews.
3. map
Now if you really hate the for in and the foreach your can write
imageViews = imageViews.map { imageView in
imageView.isUserInteractionEnabled = true
return imageView
}
or as suggested by Bohdan Ivanov in the comments
imageViews.map { $0.isUserInteractionEnabled = true }
4. Wrap up
This answer shows you how to use the wrong construct (map) to do something that should be made with the right construct (for in).
That's the point of having several constructs, everything could be made with an IF THEN and a GOTO. But a good code uses the construct that best fits that specific scenario.
So, the best solution for this scenario is absolutely the for in or the for each
imageViews.forEach { $0.isUserInteractionEnabled = true }

Coffeescript Array Indexing

If I have an array of photos in coffeescript
photos = [ly.p1, ly.p2, ly.p3, ly.p4, ly.p5, ly.p6, ly.p7, ly.p8, ly.p9, ly.p10, ly.p11, ly.p12]
for photo, i in photos
photoMask = new Layer
How can I write my for loop so that the resulting photoMask objects are outputted as photoMask1, photoMask2, photoMask3 .. photoMask12 ?
EDIT: Further elaboration
Maybe the best way to explain this is what I am trying to do in psuedocode:
for photo, i in photos
photoMask[i] = new Layer
photoMask[i].addSubLayer(photo)
So ly.p1 would have a corresponding photoMask1. That way, I can access photoMask1 separately and independently.
While I agree to the commenters about this being a bit strange, you could use something like this:
photos = [ly.p1, ly.p2, ly.p3, ly.p4, ly.p5, ly.p6, ly.p7, ly.p8, ly.p9, ly.p10, ly.p11, ly.p12]
masks = {}
for photo, i in photos
photoMask = new Layer
masks["photoMask#{i}"] = photoMask
This will create dynamic keynames within the masks object. If you really need them globally (in the browser) you could do the same thing with the window object.
But without knowing what exactly you're trying to do, I wouldn't recommend any of the above.

AS3 Copy multidimensional arrays/Vector

I have two Vectors one called "SET_grid" which should never me changed and one called "tmp_grid" which can, but how do i copy SET_grid to tmp_grid without binding it to the original, so if tmp_grid change then the SET_gird doesn't, these Vectors are both multidimensional e.g.
public var tmp_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
public var SET_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
so i would use them like this....
tmp_grid[x][y].sayhello();
tmp_grid = SET_grid does not work
tmp_grid = SET_grid.concat(); // nor does this one
Any help would be great
Nested arrays cannot be cloned without iterations. It's because they're nested :)
What this means is that you have to use nested loops and push to second vectors..

Resources