Link to home
Start Free TrialLog in
Avatar of mikeysmailbox1
mikeysmailbox1

asked on

get the last nth lines from a an array if nth element is xxx

Hi

I have an array and I am checking for a line in the array with foreach. If that line = xxxx I need to
get back the prior lines until the line is = //\s

//        dddddd
//*    
//******************************
//* blah
//* blah
//******************************
//*     xxxx          << get with if statement

I know I can get the line and element number using $#array - $x
but not sure how to get the line until //\s back.



open(FILE, "my file.txt");
push(@array,<FILE>);
close(FILE);

foreach $line (@array) {

 if($line =~ /\/\/\*\s+xxxx/) {
 
    $aa = $#array;

    do {
             push(@MYSLICE,"$array[$aa]");
             print "- $array[$aa]\n";
             print "- $aa\n";
             $aa--;

     } until ($array[$aa] =~ /\/\/\s+/);
           
    print @MYSLICE;

 }

}


I only get back the     //*



Thanks

Mike
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mikeysmailbox1
mikeysmailbox1

ASKER

Thanks for the info.