pass Multidimensional array to a function - arrays

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.

Related

How Can I read TCL file value by value

I have a file in Tcl, and I want to read each value alone as the array... I recognize my file like this
PUx(1) 1 2 3 4 5
PUx(2) 1 2 3 4 5
PUx(3) 1 2 3 4 5
PUx(4) 1 2 3 4 5
PUx(5) 1 2 3 4 5
So, I want to get, for example, the value of PUx(1) one by one and add it to the variable.
enter image description here
As Donal pointed out, it all depends on the actual format. But if the example data is representative, this might work for you:
set content {PUx(1) 1 2 3 4 5
PUx(2) 1 2 3 4 5
PUx(3) 1 2 3 4 5
PUx(4) 1 2 3 4 5
PUx(5) 1 2 3 4 5}
foreach line [split $content \n] {
set values [lassign $line varName]
set $varName $values
}
parray PUx
lassign assumes the line-wise data to represent a valid Tcl list. This might or might not be the case for you.
Update
You might want to re-organize your dataset, this would allow you to use a Tcl array idiom to access "rows" and "columns" of data in a straightforward manner, more or less:
set content {PUx(1,1) 1
PUx(1,2) 2
PUx(1,3) 3
PUx(1,4) 4
PUx(1,5) 5
PUx(2,1) 1
PUx(2,2) 2
PUx(2,3) 3
PUx(2,4) 4
PUx(2,5) 5}
foreach line [split $content \n] {
set values [lassign $line varName]
set $varName $values
}
parray PUx
# first column: *,1
foreach {k v} [array get PUx *,1] {
puts $v
}
# first row: 1,*
foreach {k v} [array get PUx 1,*] {
puts $v
}
Provided that your main concern is how to compute the sum over a list of elements, these are three options available to:
proc lsum1 {x} {
set r 0
foreach i $x {
incr r $i
}
return $r
}
proc lsum2 {x} {
expr [join $x " + "]
}
proc lsum3 {x} {
::tcl::mathop::+ {*}$x
}
set x {1 2 3 4 5}
lsum1 $x
lsum2 $x
lsum3 $x
lsum1 and lsum3 are preferable. lsum2 is the literate translation of what you describe as your "problem", at least in my reading. You may also want to check the Tcl wiki. It gives you some background on the details of lsum3.
This can be easily integrated with reading your data, as shown in my first answer:
lsum1 $PUx(1)
lsum3 $PUx(1)

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;
}

Perl: How do I sort each index of an array containing strings of numbers

I have an array reading numeric values from a text file. Each index of the array contains a string of numbers separated by a space and in random order. How do I sort each index of numbers in numeric order from lowest to highest? This is what I have so far:
print "\n\nNow sorted: \n";
foreach $line(#lines)
{
chomp($line);
#nums = sort(split (//, $line));
print "$nums"."\n";
}
Perhaps the following will be helpful:
use strict;
use warnings;
my #lines = <DATA>;
foreach my $line (#lines) {
my #nums = sort { $a <=> $b } split ' ', $line;
print "#nums\n";
}
__DATA__
7 2 9 6 4 10
3 6 8 8 10 1
9 4 10 9 2 5
5 0 2 3 7 8
Output:
2 4 6 7 9 10
1 3 6 8 8 10
2 4 5 9 9 10
0 2 3 5 7 8
Note that the above modifies your script just a little. Remember to always use strict; use warnings; Note also the anonymous sub { $a <=> $b } after sort. This is needed to sort numerically. Without it, a string comparison would have been done, and the first printed line would be 10 2 4 6 7 9. It also appears that you were attempting to split on a zero-width match, i.e., split //, $line. The result of this split is a list of single characters which comprised the line--not what you wanted, as you needed to split on spaces. Lastly, you populated #nums and then printed $nums.

Create Object in Specific loaction in Array Powershell [duplicate]

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.

Counting arrays in loop

I have a loop...
while($rows=mysql_fetch_array($result))
{
$staff[] = $rows['staff'];
$a = array_count_values($staff);
$b = count($a);
echo"$b<br>";
}
that output
1
1
1
2
2
2
3
3
4
5
5
on my research, it must be and I wanted the result to be this way
3 (is equal to three 1's)
3 (is equal to three 2's)
2 (is equal to two 3)
1 (is equal to one 4)
2 (is equal to two 5's)
any help?
what I want is to get the number of same element in an array
As far as I understand your concern, this should do the trick:
$staff = array();
while($rows=mysql_fetch_array($result))
{
$staff[] = $rows['staff'];
}
$a = array_count_values($staff);
print_r($a);

Resources