Create Object in Specific loaction in Array Powershell [duplicate] - arrays

This question already has answers here:
Adding element at specified index location in an array. Powershell
(5 answers)
PowerShell function for adding elements to an array
(2 answers)
Closed 8 years ago.
I was wondering if you could help me on this one. I have an array of objects in powershell containing this:
$array = #(1,2,3,4,5)
so $array gives me this:
1
2
3
4
5
now i would like to add in the number 6 on the position $array[3], so that the output would be:
1
2
3
6
4
5

There are many ways of doing this. Ex.
PS > $i = 1..5
PS > $i
#ouput
1
2
3
4
5
PS > function insertInto ($array, $index, $value) {
#($array[0..($index-1)],$value,$array[$index..($array.length-1)])
}
PS > $i = insertInto $i 3 6
PS > $i
#output
1
2
3
6
4
5
Warning, the method above is not very good for single value arrays.

Related

Ordering an array with the smallest value first MATLAB [duplicate]

This question already has answers here:
Sorting entire matrix according to one column in matlab
(2 answers)
Closed 4 years ago.
I'm trying to get the smallest value in the 2D array but keep the order of the 2D array, for example:
If I had the array
7 | 7
2 | 3
8 | 0
3 | 7
5 | 5
I want to order it so it displays
2 | 3
8 | 0
3 | 7
5 | 5
7 | 7
I've looked at mink and sortrows but none seem to give the output I need.
Any help with this would be much appreciated!
You can use circshift to change the order of the elements in the way you need. First find the index to the minimal element, then shift so that the element is at the top:
M = [7,7 % example data from OP
2,3
8,0
3,7
5,5];
[~,ind] = min(M(:,1));
M = circshift(M,1-ind,1);
For the updated question, you first need to identify the row that you want to be moved to the top of the matrix. Once you know this you can simply rearrange the matrix so that this row, and all that occur after it, are placed first.
A = [7 7
2 3
8 0
3 7
5 5]
[~,index] = min(A(:,1)); % Identify row which should occur first
A = A([index:end,1:(index-1)], :) % Rearrange rows
A =
2 3
8 0
3 7
5 5
7 7

Combine two arrays in Bash line by line [duplicate]

This question already has answers here:
How to merge two arrays in a zipper like fashion in Bash?
(6 answers)
Closed 6 years ago.
I have 2 arrays in BASH and I want to combine them line by line i.e.
arr1=( 1 2 3 4 )
arr2=( 5 6 7 8 )
When simply adding one array to another it's like 1 2 3 4 5 6 7 8 and I want the output of this combination to be 1 5 2 6 3 7 4 8 (line by line)
Any advice?
arr1=( 1 2 3 4 )
arr2=( 5 6 7 8 )
declare -a result
resultIndex=0
for index in ${!arr1[*]}; do
result[$resultIndex]=${arr1[$index]}
let "resultIndex++"
result[$resultIndex]=${arr2[$index]}
let "resultIndex++"
done
echo "${result[#]}"

How to make a copy of a nested array in an array of array structure

I'm trying to make a copy of a nested array, and it appears that I continue to make a reference with my attempts.
To be more specific I am trying to have an array of arrays wherein each sub array builds upon the previous array. Here is my attempt:
#!/usr/bin/perl -w
use strict;
use warnings;
my #aoa=[(1)];
my $i = 2;
foreach (#aoa){
my $temp = $_;#copy current array into $temp
push $temp, $i++;
push #aoa, $temp;
last if $_->[-1] == 5;
}
#print contents of #aoa
foreach my $row (#aoa){
foreach my $ele (#$row){
print "$ele ";
}
print "\n";
}
My output is:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
And I want/expect it to be:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I'm assuming my problems lies with how I am assigning $temp, please let me know if this is not the case. Any help is appreciated.
Create a new array with my, copy the contents of the array to be built upon, then add to it.
Keeping it as close as possible to your code
foreach (#aoa) {
last if $_->[-1] == 5;
my #temp = #$_; #copy current array into #temp
push #temp, $i++;
push #aoa, \#temp;
}

bad substitution shell- trying to use variable as name of array

#!/bin/bash
array=( 2 4 5 8 15 )
a_2=( 2 4 8 10 )
a_4=( 2 4 8 10 )
a_5=( 10 12 )
a_8=( 8 12 )
a_15=( 2 4 )
numberOfTests=5
while [ $i -lt ${#array[#]} ]; do
j=0
currentArray =${array[$i]}
*while [ $j -lt ${#a_$currentArray [#]} ]; do #### this line i get ->>>> bad substitution*
./test1.sh "${array[$i]}" -c "${a_"$currentArray "[$j]}" &
let j=j+1
done
let i=i+1
done
so Im trying this code, loop over an array(called array), The array should point out
the array number we are now looping(a_X). And every time to point out the current place and value.
can anybody help me how im using the $currentArray to work properly so I can know the length of the array and the value?
I get in the line I marked an error.
Thank you guys!
The simplest solution is to store the full names of the arrays, not just the numerical suffix, in array. Then you can use indirect parameter expansion while iterating directly over the values, not the indices, of the arrays.
# Omitting numberOfTests has it does not seem to be used
array=(a_2 a_4 a_5 a_8 a_15)
a_2=( 2 4 8 10 )
a_4=( 2 4 8 10 )
a_5=( 10 12 )
a_8=( 8 12 )
a_15=( 2 4 )
for arr in "${array[#]}"; do
currentArray=$arr[#]
for value in "${!currentArray}"; do
./test1.h "${arr#a_}" -c "$value" &
done
done

pass Multidimensional array to a function

I have a multidimensional array of integers that only works inside of the function, but produces random numbers when I try to pass it to another function.
PS> $array
1
2
3
4
PS> $array[0]
1
2
PS> $array[0][1]
2
The array was originally formed with:
$array = #(($data1),($data))
and appended on $data for any array after that: ($data is cleared each iteration)
$array += ,($data)
Which seems to be fine, considering before I tried to create functions everything was working.
I then try to pass the array into a another function
theFunc ($array)
Function theFunc {
param ($theData)
#process data
}
I'm getting the correct ammount of arrays, but the numbers inside them do not match the multidimensional array that I passed in one bit.
Any help is appreciated, thank you.
I tried the following example and everything seems to be fine:
$data1 = #(1, 2, 3, 4)
$data2 = #(5, 6, 7, 8)
#($data1, $data2)
$array[0]
1
2
3
4
$array[1]
5
6
7
8
function myFunc { param($arr) $arr; $arr[0]; $arr[1]; }
myFunc($array)
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
Please provide output you get.

Resources