Firstly hello.
I am trying to create a basic calculator of sorts that transfers my array into a cvs file.
The problem that I came upon is that when I try to create a nested array in ruby with pre-existing arrays I get a 1D array where all of the values in each row are stuck someway
whole_array=[[part1],[part2],[part3]]
puts whole_array[1][1]
return part1[1]
When this is invoked it does not output anything with the puts command yet part1,part2,part3 all being arrays have a value
The only thing I can think of is that ruby forgets that those variables are and puts them in as strings
Related
I have 2 json files, I am putting them into 2 multidimentional arrays in python, and I need to compare them and return an array of elements deleted, added and updated.
PS: I want to do this without taking in consideration that I know the depth of the arrays.
I'm trying to write a function to change arrays to hash values, I've done it so that I can change a numerical array into their hash values but I can't seem to figure out how to change string arrays into their hash values.
I'm having trouble doing it with string arrays. for example if array= [aced]
this is my code so far.
function [h]=Hash31(array1)
n=length(array1);
i=1;
h=0;
if array1
for i=1:1:n
h=array1(i)+31*h;
end
end
and it turns a numerical array into their hash value.
I was wondering if I had to do some kind of conversion or logical to make it work
and if I did to that how would I fit it into the existing function.
would it have to be a logical, because I've tried using an if function but I don't know how to write in code 'if the array is string, then...' in a way that Matlab understands.
I have my original array that has 85 arrays, and i can access to them like this
da1[34]
The problem is that I have a list (aa) where I have the specific index for the arrays I need. I want to make a loop to append all that arrays in a new list, so i used this code
aa=[45,76,18,34]
for i in aa:
orden=[]
orden.append(da1[i])
But I only get an array, I don't know what I'm doing wrong.
What I hope is to get the same number of arrays as the number of indexes that I have in aa
You just need to define the list outside the loop, like this:
aa=[45,76,18,34]
orden=[]
for i in aa:
orden.append(da1[i])
The reason is you are creating the list every time you iterate in the for. So at the end you end up only with the last element in the list.
I'm a beginner (a wet lab biologist, who has to fiddle a bit with bioinformatics for the first time in my life) and today I've got stuck on one problem: how to parse an array to a hash of arrays in perl?
This doesn't work:
#myhash{$key} = #mytable;
I've finally circumvented my problem with a for loop:
for(my $i=0;$i<=$#mytable;$i++){$myhash{$key}[$i]=$mytable[$i]};
Of course it works and it does what I need to be done, but it seems to me not a solution to my problem, but just a way to circumvent it... When something doesn't work I like to understand why...
Thank you very much for your advice!
If you are asking how to put an array as one value of a hash, you do this by taking a reference to the array, since references are scalars and the values of hashes must be scalars. This is done with the backslash operator.
$myhash{$key} = \#mytable;
The for loop you describe creates such a reference through autovivification, as $myhash{$key}[0] creates an array reference at $myhash{$key} in order to assign to its index. Also note that the difference between taking a reference and copying each value is that in the former case, changes to the array after the fact will also affect the values referenced via the hash value, and vice versa.
$mytable[5] = 42; # $myhash{$key}[5] is also changed
As Grinnz mentioned you can save a reference to an array, but any change on the array latter will be reflected in hash (it is same data).
For example if you reuse same array in the loop then data in hash will reflect last iteration of the loop.
In such case you will want a copy of array stored in the hash.
#{$hash{$key}} = #array;
Programming Perl: Data strutures
I currently have a large single row array of chars... I also have two arrays, the first array has all the start indexes of data I would like to retrieve from the char array, the second array has all the end indexes for the data. How can I retrieve all these wanted values from my char array without using a loop?
So far I have tried doing
chararray(1,start(:):end(:))
but this will only retrieve the first value I would like!
Cheers!
Try this -
chararray(bsxfun(#plus,start1(:)-start1(1),start1(1):end1(1)))
This would create a 2D char array where each row be the output from each iteration of your loop code.
Also, please note that I am using start1 and end1 to represent your start and end arrays respectively, so as not to create a clash with the reserved terminate scope end used by MATLAB.