How to check if a variable is defined and search for two patterns within an array using perl regex?
$/ = ""; # paragraph mode
__Data__
#SCSI_test # put this line into @arrayNewLines
kdkdkdkdkdkdkdkd
dkdkdkdkdkdkdkdkd
- ccccccccccccccc # put this line into @arrayNewLines
while(<FILEREAD>)
{
chomp;
my @arrayOld = split('\n',$_);
foreach my $i (0 .. $#arrayOld) # using the foreach of index to remove the lines.
{
if($arrayOld[$i] =~ /-(.*)/ || /\#(.*)/)
{
my @arrayNewLines = $arrayOld[$i];
splice @arrayOld,$i,1; # remove lines from arrayOld
print "@arrayNewLines\n";
}
print "@arrayOld\n";
}
}
This code does not work in terms of placing each type of – or # lines into the same @arrayNewLines array. It only works on the first (/-(.)/) pattern and does not work on the second (/#(.)/). I need it to search on both patterns and defined the $arrayOld[i] variable to prevent use of uninitiilize messages.
I want to defined
This does not work the way you think it does, for the same reason that this wouldn’t either:
Use this instead:
Just as you’d use this: