fructu
BAN USER#!/usr/bin/perl
# author: fructu
# v0.01
#
# description:
#
# 1) i use read_file_primary_to_hash to load the primary file
# into a hash if the line is repited the hash has
# the number of repetitions.
#
# 2) i use read_file_secondary_to_hash to read the secondary file
# for each line i searh it in the hash and chek if the hash is
# defined if not -> return error;
# if the hash is defined for that line i check have more than 0
# lines and substract 1 in order to mach the line of the primary
# file.
# Then in the same function i check all hash entries are 0 if
# not i return 0.
#
use warnings;
use strict;
sub read_file_primary_to_hash($ $)
{
my $file_name = shift;
my $hash = shift;
open file_in, "<$file_name" or die "$!\n";
while(<file_in>){
chomp();
$hash->{$_}++;
}
close file_in;
}
sub read_file_secondary_to_hash($ $)
{
my $file_name = shift;
my $hash = shift;
open file_in, "<$file_name" or die "$!\n";
while(<file_in>){
chomp();
if(defined $hash->{$_}){
if($hash->{$_} <= 0){
print "line not found [$_]\n";
return 0;
}
$hash->{$_}--;
}else{
print "line not found [$_]\n";
return 0;
}
}
foreach my $k (keys %$hash){
if( 0 != $hash->{$k} ){
print "line count bad [$k] : [$hash->{$k}]\n";
return 0;
}
}
close file_in;
return 1;
}
sub main
{
my $h1 = {};
my $result = 0;
read_file_primary_to_hash('e_2_primary.txt', $h1);
$result = read_file_secondary_to_hash('e_2_secundary.txt', $h1);
if(1 == $result){
print "the files have the same lines\n";
}else{
print "the files do not have the same lines\n";
}
}
main();
This script can be improved using a function to generate a hash like
use Digest::SHA qw(sha1 sha1_hex sha1_base64 ...);
$digest = sha1($data);
and store it in the hash instead of the whole line.
- fructu July 21, 2013
I did this algorithm based in the next rules:
a matrix have corners (0,0) and (n,m)
if you have a matrix of 1x1 dimension check if the number is that cell.
if number < (0,0) ---> we know the number is not in the matrix.
if number > (n, m) ---> we know the number is not in the matrix.
I divide the matrix in 4 submatrix and do recursion wich each of them.
- fructu August 07, 2013