I am building five arrays in perl like (null, null, 32, 54, null, 59). So I first put the values that I know at specific index and fill the others with null. I do this like this
my @p0_series, @p1_series;
$p0_series[2] = "test";
$p1_series[3] = "string";
$p2_series[2] = "hello";
$p3_series[2] = "hi";
for($a = 0; $a < 5; $a++) {
if(!defined $p0_series[$a]) {
$p0_series[$a] = null;
}
if(!defined $p1_series[$a]) {
$p1_series[$a] = null;
}
if(!defined $p3_series[$a]) {
$p3_series[$a] = null;
}
if(!defined $p4_series[$a]) {
$p4_series[$a] = null;
}
if(!defined $p5_series[$a]) {
$p5_series[$a] = null;
}
}
I am trying to reduce this code to simpler one but I am not able to use the variable name p0_series, p1_seires dynamically in a loop. I tried like
for($a=0: $a <5; $a++ ){
if(!defined $p$i_series[$a] ) {
# assign values;
}
}
Which is not working. I am new to perl any help is appreciated. Is it possible I can assign the null value to all the undef elements in the array in a simpler manner?
use strict; use warningsat the top of your script to get warned about some problems with your script which I didn't address in my answer.