-1

I have many files which has many 2x2 arrays. In the present example I show only one file (file1)

Input file(file1):

1: 6.1703
 541.631 46.0391

2: 6.1930
 537.446 45.9239

3: 6.1931
 177.171 288.579

4: 6.1939
 167.171 298.579

5: 8.2281
 533.686 53.7245

6: 8.6437
 519.219 65.0547

7: 9.0823
 484.191 95.0753

8: 9.3884
 237.75 240.082

9: 9.4701
 167.525 246.234

10: 9.7268
 411.929 70.7877

First, I need to see the value of the position (1,2) of each matrix, and choose it if it is near to 6.1937 and has a large value in the element(2,1). In this example the chosen value should be 6.1930.

Second, I need to choose the lowest value of the position (1,2) of each matrix, which has large value of the element (2,2).In this case, the chosen value is 6.1931

So the output should be like this

 6.1930  6.1931
1
  • HI @choroba I tried to use PROCINFO, and sorted it, but I cannot get the desired output Commented Jun 15, 2016 at 12:57

1 Answer 1

0

Perl to the rescue:

#!/usr/bin/perl
use warnings;
use strict;

my $close_to = 6.1937;

my ($close, $lowest);

$/ = q();
while (<>) {
    my @arr = split;
    if ((! defined $close || abs($arr[1] - $close_to) < abs($close - $close_to))
        && $arr[2] > $arr[3]
        ) {
        $close = $arr[1];
    }
    if ((! defined $lowest || $arr[1] < $lowest)
        && $arr[2] < $arr[3]
        ) {
        $lowest = $arr[1];
    }
    if (eof) {
        print "$close $lowest\n";
        undef $close;
        undef $lowest;
    }
}

Save as script.pl, call as script.pl file1 file2 file3...

6
  • Hi @choroba. In the case to want select the value of the position (1,2) of each matrix, which has large value of the element (2,2) between the first five arrays. What modification to the perl script need I do Commented Aug 5, 2016 at 4:57
  • hi I was trying to modify here, but I cannot find a solution if ((! defined $lowest || $arr[1] < $lowest) && $arr[2] < $arr[3] ) { $lowest = $arr[1]; Commented Aug 5, 2016 at 10:44
  • @alloppp: Can you provide a sample input + output, plus explain what "between the first five arrays" means? Creating a new question (with a link to the old one) is the best for new questions. Commented Aug 5, 2016 at 10:48
  • The input is the same(I will cut the input then with bash sed ). But the difference is that the selection criteria for the $lowest = arr[1] need to be changed by the something like highest = arr[3] (and compare only the arr[3] to find the highest arr[3]) Commented Aug 5, 2016 at 10:50
  • After find the highest arr[3] , the corresponding arr[1] need to be printed Commented Aug 5, 2016 at 10:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.