Julia - using dictionary key as index for a multidimensional array - julia

I have a dictionary with keys as shown:
testdict = Dict{Array{Int64,1},Float64}([1,3,1] => 0.0, [2,3,1] => 0.0, [1,3,2] => 0.0, [2,3,2] => -2.64899e-16, [2,1,2] => 0.858307, [1,2,1] => 0.0, [1,2,2] => 0.0, [2,2,1] => 0.0, [2,2,2] => 0.65796, [2,1,1] => -5.81556e-16, [1,1,2] => -3.50541e-16, [1,1,1] => 0.0)
These keys vary considerably both regarding range and length, they are initialized by an array in the beginning of the function Im writing...it could look like
[2,3,2]
for the dictionary above...,
or
[10,3,50,60]
creating a dictionary with keys
[1,1,1,1], [1,1,1,12] ... , [10, 3, 50, 59], [10,3, 50, 60]
And what I need to accomplish, is create a multidimensional array by
result_array = Array(Float64, tuple([2,3,2])
But then I need to populate the array with the values from the dictionary,
so I would need to set element [1,1,1] as
result_array[1,1,1] = 0.0
How can I use the keys from the dictionary to set the indices of the result_array with their respected values?

Splat the key to turn result_array[[1,1,1]...] into result_array[1,1,1].
testdict = Dict{Array{Int64,1},Float64}([1,3,1] => 0.0, [2,3,1] => 0.0, [1,3,2] => 0.0, [2,3,2] => -2.64899e-16, [2,1,2] => 0.858307, [1,2,1] => 0.0, [1,2,2] => 0.0, [2,2,1] => 0.0, [2,2,2] => 0.65796, [2,1,1] => -5.81556e-16, [1,1,2] => -3.50541e-16, [1,1,1] => 0.0)
result_array = Array(Float64,2,3,2)
for (k,v) in testdict
result_array[k...] = testdict[k]
end

Related

Php Create array with increments days of week containing multidimensional array

I would like to create an array like this , and I'm lost with the syntax:
I can't nest the arrays together
$PeriodDayWeek = Array(
[monday] => Array
(
[start] => Array(
[from] => Morninghour,
[to] => Afternoonhour,
),
[end] => Array(
[from] => Morninghour,
[to] => Afternoonhour,
)
),
[tuesday] ...
until [sunday]
)
I have the beginning but it's not good:
$PeriodDayWeek = [];
foreach($EnglishDay as $Day):
$Day = array(
"start" => array(
"from" => $_POST['Dayweek_JoMo_FrH_'. $Day] .'h'. $_POST['Dayweek_JoMo_FrM_'. $Day] ,
"to" => $_POST['Dayweek_JoMo_ToH_'. $Day] .'h'. $_POST['Dayweek_JoMo_ToM_'. $Day]
)
);
endforeach;
$PeriodDayWeek = array_merge($Day, $EnglishDay);
$events_meta['periodevent_dayweek'] = maybe_serialize($PeriodDayWeek);
Solved :
$PeriodDayWeek =array();
foreach($EnglishDay as $day){
$PeriodDayWeek[$day] = array(
"start" => array(
"from" => $_POST['Dayweek_JoMo_FrH_'. $day] .'h'.$_POST['Dayweek_JoMo_FrM_'. $day] ,
"to" => $_POST['Dayweek_JoMo_ToH_'. $day] .'h'. $_POST['Dayweek_JoMo_ToM_'. $day]
)
);
}
$events_meta['periodevent_dayweek'] = maybe_serialize($PeriodDayWeek);

How to get value from multidimensional array

Array ( [0] => Array ( [data] => Array ( [0] => Array ( [like_info] => Array ( [like_count] => 30 ) [comment_info] => Array ( [comment_count] => 6 ) [share_count] => 17 [attachment] => Array ( [description] => Mitkkk kkssk [media] => Array ( [0] => Array ( [src] => com.jpg&cfs=1 ) ) [name] => euch ) [permalink] => example.com/47457343655 [created_time] => 1925 ) ) ) )
how to get value from all the column in variable listed here
like_count
share_count
comment_count
description
src
permalink
created_time
You can make a vector of variables, i mean a single array:
$var = array();
Then you can do a double loop to get all the values of the multi dimensional array and store them on the vector, like:
for($i = 0; i< count($yourarray); i++){
if(is_array($yourarray[i])){
for($j = 0; j< count($yourarray[i]); j++) $var[] = $yourarray[i][j];
}else $var[] = $yourarray[i];
}

Elasticsearch NEST, case sensitive multi-field

I struggle to make this multi-field setting to case sensitive, what's the missing piece?
Thanks in advance!!.
targetClientProperties.MapFluent< CMSDocument >
( m => m. MapFromAttributes()
.Properties( p => p.MultiField( mf => mf
.Name ( n => n.Hash )
.Fields(fs => fs
.String(s => s.Name(t => t.PropertyHash))
.String(s => s.Name(t => t.TileHash))
))));
Set the fields to not_analyzed:
targetClientProperties.MapFluent< CMSDocument >
( m => m. MapFromAttributes()
.Properties( p => p.MultiField( mf => mf
.Name ( n => n.Hash )
.Fields(fs => fs
.String(s => s.Name(t => t.PropertyHash).Index(FieldIndexOption.not_analyzed))
.String(s => s.Name(t => t.TileHash).Index(FieldIndexOption.not_analyzed))
))));

how extract values from multidimensional array

I have this multidimensional array:
Array
(
[car] => Array
(
[responsecode] => 200
[ford] => Array
(
[start] => 0
[count] => 20
[model] => 972000
[results] => Array
(
[0] => Array
(
[date] =>
[clickurl] => xx
[url] => xx
[dispurl] => xx
[title] => xx
[abstract] => xx
)
[1] => Array
(
[date] =>
[clickurl] => xx
[url] => xx
[dispurl] => xx
[title] => Txx
[abstract] => xx
)
I need retrieve value from [model] (972000)
Its really hard for me. Thanks in advance!
if you have your array assigned to a variable say $arry then it would be
$arry['car']['ford']['model']
there are other techniques to get the 'model' from every 'car' in an array, is that what you are looking for?
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>
http://php.net/manual/en/language.types.array.php

cakephp 2.0 hasmany relation retrieve data

I have two model name image and comment. here the relation is like image has many comments . Now in my listing page I want to display all imae detail and only the no of comment on that image . can you tell me how should I get that ?
after I write the query my return data is
Array
(
[0] => Array
(
[Image] => Array
(
[image_id] => 57
[user_id] => 1
[category_id] => 22
[image_title] => scroul
[description] => beutifull natural image for the animal
[keyword] => scrual
[image_price] =>
[image_name] => 7bf4a72509da5906903c84e88228b9dd.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 12
[size] => 3244
[resolution] => 2162 x 1644
[i_date] => 1348573022
[i_by] => 1
[u_date] => 1348573022
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 5
[image_id] => 57
[user_id] => 2
[comment] => socute
[comment_date] => 1348739230
[is_active] => N
[is_deleted] => N
)
)
)
[1] => Array
(
[Image] => Array
(
[image_id] => 56
[user_id] => 1
[category_id] => 22
[image_title] => cute dog
[description] => cute dog looking
[keyword] =>
[image_price] =>
[image_name] => d4af899b0d52cccbec94952a3abd0077.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 8
[size] => 620
[resolution] => 2592 x 1944
[i_date] => 1348572897
[i_by] => 1
[u_date] => 1348572897
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 3
[image_id] => 56
[user_id] => 2
[comment] => ohhhhhhh
[comment_date] => 1348737968
[is_active] => N
[is_deleted] => N
)
)
)
[3] => Array
(
[Image] => Array
(
[image_id] => 55
[user_id] => 1
[category_id] => 22
[image_title] => ships
[description] => ships with beutiful green background
[keyword] => ship,green,animal,nature,background,eating,white ship
[image_price] =>
[image_name] => c0dfc2432ae047e9160f3ef99880fe87.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 1
[size] => 1831
[resolution] => 2520 x 1944
[i_date] => 1348572846
[i_by] => 1
[u_date] => 1348661976
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 2
[image_id] => 55
[user_id] => 2
[comment] => i like it
[comment_date] => 1348737942
[is_active] => Y
[is_deleted] => N
)
[1] => Array
(
[comment_id] => 4
[image_id] => 55
[user_id] => 2
[comment] => good scene
[comment_date] => 1348738004
[is_active] => N
[is_deleted] => N
)
)
)
)
in the above array there is a all comment of that image. I don't want here comment list I just want no of comments .
Could you post the find statement you're using. You can set recursive to a certain level, from the documentation:
-1 Cake fetches Group data only, no joins.
0 Cake fetches Group data and its domain
1 Cake fetches a Group, its domain and its associated Users
2 Cake fetches a Group, its domain, its associated Users, and the Users’ associated Articles
So you can call the following if you only want the data from the current model only:
$this->Model->find('all', array('recursive' => -1));
There is also the Containable behavior, which allows you to specify which Model data you want to retrieve when calling find.
Say you have a Post model which hasMany Image. The call, with the Containable behaviour in the Post Model properly included, would be:
$this->Post->find('all', array(
'conditions' => array('Post.id' => 1),
'contain' => array('Image')
));
EDIT:
Because of the spelling and formatting I misread your initial question. I thought you wanted "no comments" to appear in the data array, instead you want only the "number of comments" to appear.
If you want the comment count, use counterCache as Kishor Kundan proposes.
Cake offers another amazing magic, "counterCache".
You can define the counterCache in your Comments model
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true
);
Then you add a field, 'comment_count' in your images table (or the table which is being used by the model Image) and the cake will do the rest for you.
This does add an overhead every time a comment is added/deleted but it is far better alternative than to issue an 'count' every time you fetch image data.
For more info you can check the cookbook. Look out for "counterCache" there.
UPDATE:
To limit the scope for counter cache, use additional attribute 'counterScope' as
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true,
'counterScope' => array('Image.active' => 1)
);

Resources