I am able to create a matrix using this code
#!/usr/bin/perl -w
@arr1 = (10,20,30);
@arr2 = (10,20,30);
@arr3 = (10,20,30);
@ref_arr = (\@arr1, \@arr2, \@arr3);
print"Prog starts\n";
foreach $ref (@ref_arr) {
#print @$ref->[0];
foreach $val (@$ref) {
print "$val ";
}
print"\n";
}
Using the map function I can modify each value in the matrix like this
Example: increase every value by 1
foreach $ref (@ref_arr) {
map($_++, @$ref);
}
but I want to modify a certain row or a specific value, i.e. either add 1 to all of the second row’s values or to the first column of the second row
You must always
use strictanduse warningsat the top of every program that you write. This applies especially if you are asking for help with your code, as these measures will reveal simple errors that you would otherwise overlookThe
mapfunction is not for iterating over a list: it is for ‘mapping’ one list to another by applying a function to each element of the source listWhen you write
you are building and discarding a copy of the values in
@list. What you should write isAs for how to modify a single value out of the array, your array initialisation can be simplified to
I hope from this it is easier to see that the first
10in the structure is accessible as$data[0][0]and, say, the last20is$data[2][1](remembering that Perl arrays are indexed from zero). You can access and modifiy these values just as any ordinary scalarAs for your particular examples, the second row is
@{$data[1]}so you can increment every element of the row by writing$_++ for @{$data[1]}. The first column of the second row is incremented with$data[1][0]++