When I was trying to print a specific element in array, I have mistakenly typed the name of the same array inside [ ] as element and got some output as shown. I thought that it is taking the size of the array and printing that number of characters, which was proven to be wrong by the output.
#array = (0..10, 12);
print "#array[#array]";
prints
Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
0 1 2 3 4 5 6 7 8 9 10
I went one step ahead and edited code as shown below
print "#array[#array[#array]]";
output as: `0 1 2 3 4 5 6 7 8 9 10 0` Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
Now I tried to put a number inside the second array instead of again giving array name as below.
print "#array[#array[1,2,3]]";
output as
1 2 3
But no warning message this time.
What it is trying to print? and What is the reason behind this behavior.
You're creating an array slice. Start out by understanding that your array contains elements 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12.
That's a total of 12 elements (there is no value 11, but at index 11 you are storing the value 12).
Now when you use this construct: #array[#array] you're taking a slice, and requesting the values stored in indices 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 12. There is no element with an index of 12 in your array. The eleventh element has the value "12".
#array[] is array slice notation. For example:
my #array = (qw/apple banana cucumber date/)
#array[1,2] will return a list of (banana, cucumber) (index 1 and index 2 of #array).
Now, #array[#array] tries to take the values of the inner #array and use them as indexes of the outer #array. In your example, #array[#array] is equivalent to #array[0..10, 12]. Since one of the values of the inner array is 12, and the outer array has no index of 12, you get an undefined value warning.
Related
So I have this function.
array = Array.new
def put_array(array)
l = array.index(array[-1])
puts l
for i in 0...l do
puts array[i]
end
end
put_array([1, 2, 3, 4, 5, 6])
the output is to print the array but I keep getting this result.......
5
1
2
3
4
5
Error ::The index output is 5, whereas the expected output should be 6. I have tried rindex,array.last instead of array[-1] but I keep getting same answer
I'm not exactly sure what your expected output was.
But your error comes from the fact that array indexing in ruby is zero-based, ie they start at 0. Therefore :
Despite your array having 6 elements, the index of its last element is 5.
This also mean that when you start iterating through your array with an index value of 1, you are starting with its second element.
On a different note, with 1...l you are defining an exclusive range, that is a range of integers from 1 to l, excluding l. If you want to include l, you need to use 1..l
A quickfix to your code would be :
for i in 0..l do
puts array[i]
end
But a more idiomatic way of achieving the same output would be :
array.each {|e| puts e}
Also
l = array.index(array[-1])
looks a bit convoluted. Why not simply ?
l = array.length
If you simply puts array it will print each element of array in new line:
array = [1, 2, 3, 4, 5, 6]
puts array
#=> Output:
1
2
3
4
5
6
def put_array(array)
puts array
end
> put_array(array)
If you want to perform on index basis:
> array.each_with_index {|e,i| puts array[i]}
I think you are confused because 0...5 outputs the number range 0,1,2,3,4 and not 0,1,2,3,4,5. You need to use .. rather than ... at the line starting for i in
def put_array(array)
l = array.index(array[-1])
puts l
for i in 0..l do
puts array[i]
end
end
put_array([1, 2, 3, 4, 5, 6])
That will output:
5
1
2
3
4
5
6
Though as others have stated, there are easier ways to achieve the same output.
I am not sure if you are trying to print the array, if it so .. your code can be modified this way
array = Array.new
def put_array(array)
l = array.length
for i in 0...l do
puts array[i]
end
end
put_array([1, 2, 3, 4, 5, 6])
index gives you the index for the given object, so in your case index(-1) will give nil because the array does not contain -1.
Instead you want to do this:
array.index(6) # => 5 (6 is at index 5 of array)
array[5] # => 6 (5th item in array is 6)
array[-1] # => 6 (-1th (aka last) item in the array is 6)
array.last # => 6 (last item in array is 6)
I have the following sample sheet:
1/A B C D E F G H I J
2
3 Points 8 4 2 1
4
5 Values 1 2 3 4 4 3 1 2
I'm trying to sum the 'Points' based upon the array index from the 'Values'.
My expected result from this is: 30
Here is my formula:
{=SUM(INDEX($C$3:$F$3,1,C5:J5))}
For some reason though, this only returns the first value of the array, rather than the entire sum.
To clarify, the C# version would be something like:
var points = new int[] { 8, 4, 2, 1 };
var values = new int[] { 2, 4, 3, 1, 2, 4, 2 };
var result = (from v in values
select points[v - 1]).Sum(); // -1 as '4' will crash, but in Excel '4' is fine
Edit: Adding further clarifying example
Another example to clarify:
Points is the array. The 'values' represents the index of the array to sum.
The example above is the same as:
=SUM(8, 4, 2, 1, 1, 2, 8, 4)
INDEX will never take its row or column parameters from arrays and then perform multiple times within one array formula contained in one cell. For this OFFSET will be needed.
Either
{=SUM(N(OFFSET($C$3,,C5:J5-1)))}
as an array formula.
Or
=SUMPRODUCT(N(OFFSET($C$3,,C5:J5-1)))
as an implicit array formula without the need for [Ctrl]+[Shift]+[Enter].
I have an array in which I want to replace values at a known set of indices with the value immediately preceding it. As an example, my array might be
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
and the indices of values to be replaced by previous values might be
y = [2, 3, 8];
I want this replacement to occur from left to right, or else start to finish. That is, the value at index 2 should be replaced by the value at index 1, before the value at index 3 is replaced by the value at index 2. The result using the arrays above should be
[1, 1, 1, 4, 5, 6, 7, 7, 9, 0]
However, if I use the obvious method to achieve this in Matlab, my result is
>> x(y) = x(y-1)
x =
1 1 2 4 5 6 7 7 9 0
Hopefully you can see that this operation was performed right to left and the value at index 3 was replaced by the value at index 2, then 2 was replaced by 1.
My question is this: Is there some way of achieving my desired result in a simple way, without brute force looping over the arrays or doing something time consuming like reversing the arrays around?
Well, practically this is a loop but the order is number of consecutive index elements
while ~isequal(x(y),x(y-1))
x(y)=x(y-1)
end
Using nancumsum you can achieve a fully vectorized version. Nevertheless, for most cases the solution karakfa provided is probably one to prefer. Only for extreme cases with long sequences in y this code is faster.
c1=[0,diff(y)==1];
c1(c1==0)=nan;
shift=nancumsum(c1,2,4);
y(~isnan(shift))=y(~isnan(shift))-shift(~isnan(shift));
x(y)=x(y-1)
How do you solve this problem in O(n) time?
Given an unsorted array of integers, design algorithms to transform the array such that the integers are replaced by the nearest bigger integer on their right. If there is no bigger integer on its right, the integer remains the same. For example, the following array of integers
2 1 4 5 3 6 7 9 4 8
should become
4 4 5 6 6 7 9 9 8 8
Init Stack of integers
From left to right.
Pop from stack while element is smaller, and replace it with the current
Add id of element to stack
Note that the stack will be strictly decreasing. And that each element is popped/added max 1 time. Hence O(N)
Example code in Python:
l = [2, 1, 4, 5, 3, 6, 7, 9, 4, 8]
s = [0]
for i in range(1, len(l)):
while s and l[i] > l[s[-1]]:
l[s.pop()] = l[i]
s.append(i)
Im taking CompArch for the first time ever. I have to print out an array
list: .word 3, 0, 1, 2, 6, -2, 4, 7, 3, 7
I managed to print it out in two rows
R1 3 0 1 2 6
R2 -2 4 7 3 7
How can i create a loop or code to add all the numbers in the first row. Or in other words the first 5 ints from the array. Thanks for your help in advance.