G
G
Gibbon Cho2015-04-28 16:03:27
Perl
Gibbon Cho, 2015-04-28 16:03:27

How to do subtraction of two files in perl?

There are two files, the first has a bunch of IDs, the second has only some IDs (used). So you need to write a perl script so that it subtracts the ID of the second file from the first file. The files are very large. The first one can be up to 20k lines, so the script needs to be optimized. Help if you can =)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2015-04-29
@gibboncho

If I understand the question correctly.
Let's say we have two files 1.txt and 2.txt

cat 1.txt                                                                                                                                   
100
2323
2390238
32322323
9002

cat 2.txt 
34
4343
434
2390238
32322323
9002

with such content.
Now they can be compared and the difference calculated like this:
#!/usr/bin/env perl 
use strict;
use warnings;
use utf8;
use File::Slurp;
use Array::Utils qw(:all);

my @arr1 = read_file( '1.txt', chomp => 1 );
my @arr2 = read_file( '2.txt', chomp => 1 );

# Получилось 2 массива. 
# Теперь сравниваем их c помощью use Array::Utils qw(:all);
# И вычисляем разницу
# Получаем элементы из массива @аrr1, которые не входят в массив @arr2


my @minus = array_minus( @arr1, @arr2 );

for my $i (@minus){
        print "$i \n";
}

Result of work:
100 
2323

Now you can write what is in the array @minusto a file, and that's all.

V
Vlad Zhivotnev, 2015-04-28
@inkvizitor68sl

comm -23 file1 file2 , if you decide to write on a perl.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question