基于值的Perl - Split排序数组分为两个数组。

时间:2022-06-27 09:08:38

I am fairly new to Perl.

我对Perl相当陌生。

I have a large array of numbers which is sorted first:

我有一大堆的数字,首先被排序:

my @numbers = do {                                                      
        open my $fh, '<', glob("hotfolder/*.out") or die $!;            
        local $/;                                                           
        <$fh> =~ /\d+/g;                                                    
};

    my @sorted_numbers = sort { $a <=> $b } @numbers;

The sorted values are now in the @sorted_numbers array. Next it needs to find the missing values in this array:

排序后的值现在位于@sorted_numbers数组中。接下来需要查找该数组中缺失的值:

my @missing;

     my $i = 0;
            for ( $sorted_numbers[ 0 ] .. $sorted_numbers[ -1 ] ) {         
                ++$i, next if $_ == $sorted_numbers[ $i ];                  
                    push @missing, "$_\n";
    }

This is where I need some help. I am not sure how to achieve the next step. Each number in the array either starts with 2 (200000) or 3 (300000).

这是我需要帮助的地方。我不知道下一步该怎么走。数组中的每个数字都以2(200000)或3(300000)开始。

The missing values should only show between these ranges. Let's say the first range finished at 240000. The other range will start somewhere over 300000.

缺失的值应该只显示在这些范围之间。假设第一个射程在240000。另一个范围将从30万开始。

Of course I do not want to return all the values outside these two ranges as missing.

当然,我不想返回这两个范围之外的所有值。

I tried something along these lines but the syntax and logic is completely wrong:

我沿着这些思路尝试了一些东西,但是语法和逻辑是完全错误的:

    foreach (my $step = @sorted_numbers) {


    if ($step <= 299999) {
        my $i = 0;
            for ( $sorted_numbers[ 0 ] .. $sorted_numbers[ -1 ] ) {         
                ++$i, next if $_ == $sorted_numbers[ $i ];                  
                    push @missing, "$_\n";
                    }   
    }
    else ($step > 299999) {
        my $i = 0;
            for ( $sorted_numbers[ 0 ] .. $sorted_numbers[ -1 ] ) {         
                ++$i, next if $_ == $sorted_numbers[ $i ];                  
                    push @missing2, "$_\n"; 
                    }
        }
    }

Let me know if something is unclear and I'll be happy to provide further information.

如果有什么不清楚的地方,请告诉我,我很乐意提供进一步的信息。

1 个解决方案

#1


4  

Just check the difference between neighbouring numbers. If it's more than 1 but small enough (see $gap_size below), report the missing numbers:

检查相邻数字之间的差异。如果大于1但足够小(见下面$gap_size),报告缺失的数字:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $gap_size = 50;  # Tune this to your needs.

my @numbers = sort { $a <=> $b } map /\d+/g, <>;

for my $i (1 .. $#numbers) {
    my $diff = $numbers[$i] - $numbers[$i-1];
    if ($diff > 1 && $diff < $gap_size) {
        say for $numbers[$i-1] + 1 .. $numbers[$i] - 1;
    }
}

#1


4  

Just check the difference between neighbouring numbers. If it's more than 1 but small enough (see $gap_size below), report the missing numbers:

检查相邻数字之间的差异。如果大于1但足够小(见下面$gap_size),报告缺失的数字:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $gap_size = 50;  # Tune this to your needs.

my @numbers = sort { $a <=> $b } map /\d+/g, <>;

for my $i (1 .. $#numbers) {
    my $diff = $numbers[$i] - $numbers[$i-1];
    if ($diff > 1 && $diff < $gap_size) {
        say for $numbers[$i-1] + 1 .. $numbers[$i] - 1;
    }
}