arrays - using perl, how to select last 2 lines in the case of each row has same word? -


bini  --  -21.89753  -20.47853  -20.27835  -18.34952  -16.23454  bini  --  -16.89753  -14.47853  -13.27835  -12.34952  -11.23454  bini  --  -10.09014   

my file has array shown above. , array starting bini array having multilines showing 3 lines here. wanted try extract last 3 elements last 2 lines. so, -12.34952 -11.23454 -10.09014 these 3 elements wanted. sometimes, last line may have elments 2 5 depending on files. here, has 1 elements last line.

what tried follows

while(my $line = <file>) {      if($line =~ /bini/) {      #extract last 3, 2, 1 element      @entries = split(/ws+/,$line);      $element1 = (pop@entries);      $element2 = (pop@entries);      $element3 = (pop@entries);      } 

as result, see element1 -10.09014, unfortunately, couldn't element 2 , element 3. me? ..


i want keep original script. mean,, creaing process of result.txt , opening method of "log" output format.

blockquote

#!/usr/bin/perl use warnings;  use strict;  use file::stat;   open (out, ">", "result\.txt") or die "cannot open file\,\n"; #from plx, want creat result.txt   foreach $answer (glob "*.log") { # format of reading file "log"   open (file, "<", "$answer") or die "cannot open file\.\n";  @file = split ('\.', $answer); 

blockquote

your opening method of file induced error ( @array = read_file('input.txt') wonder how can using script starting $line = 0 script. though changed format of txt log (e.g. input.log), still gave error message. (read_file 'input.txt' - sysopen: no such file or directory @ text.plx line 6)

.....

you can take numbers every line, push them @ end of @entries, , keep last three.

my @entries; while(my $line = <file>) {      next if $line !~ /bini/;      push @entries, grep /\d/, split /\s+/,$line;      @entries = @entries[-3 .. -1] if @entries > 3; } print join "\n", @entries; 

output

-12.34952 -11.23454 -10.09014 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -