Reputation: 45
i have two files, one containing all active user loginnames, and the other one containing permissions with user logins that don't exist in the active users file anymore, so i want to compare these and filter out the user names that no longer exist, but i have no clue how to get started. I have filtered out the the usernames from both files, and they are in two separate arrays. Here's my whole code, warning, it contains other stuff that i need the program to do so just act like it doesn't exist. And also Warning, don't worry about the Comments, it's German, it's only for me to know what does what.
#!/usr/bin/perl
use strict;
#use warnings;
use utf8;
use Data::Dumper;
my $usernames = 'usernames.txt';
my $permissions = 'ab1000.prm';
my $prmlogins = 'prmlogins.txt';
# Programme ausführen
clean_permissions();
# Subroutinen
# Subroutine welche die gewünschte Datei Zeile für Zeile ausgibt
sub read_lines {
my($file) = @_;
open my $in, "<:encoding(utf8)", $file or die "$file: $!";
local $/ = undef;
my $content = <$in>;
close $in;
return split /\n/, $content;
}
sub clean_permissions {
my $counter = 0; # Zählervariable wird definiert
my @adnutzer = ''; # Array für Nutzernamen wird definiert
my @ab1000prm = read_lines('ab1000.prm'); # Zeilen des Dokuments werden eingelesen
my @adnames = read_lines('ad_user.csv'); # Zeilen der CSV mit Subroutine in Array einlesen
my $zeilenzaehler = 1; # Zeilenzähler wird definiert
my $zeilen = ''; # Variable in der die Anzahl der Zahlen gespeichert werden
my @prmnutzer = '';
foreach(@adnames) { # Über das erstellte Array itirieren
$counter++; # Zähler für Zeilenanzahl
next if ($counter eq 1); # Erste Zeile überspringen
my @tmp = split(';', $_); # Spalten am Semikolon trennen
push(@adnutzer, uc($tmp[3])); # Vierte Spalte (Userlogins) in Array pushen
}
foreach(@ab1000prm) { # Über die Permissions datei itirieren
my $wortzaehler = length($_); # Variable zum Zählen von Zeichen in einer Zeile
$zeilen = $zeilenzaehler++; # Zeilenzähler wird der Zeilen Variable zugewiesen
if ($wortzaehler > 255) { # Wenn eine Zeile mehr als 255 Zeichen hat:
#print ("Zeile $zeilenzaehler behinhaltet: $wortzaehler zeichen!\n"); # Wird die Nummer der Zeile und die Anzahl and Zeichen ausgegeben
}
next if /^#/; # Zeilen mit '#' werden übersprungen
my @tmp = split(":", $_); # Übrigen Zeilen werden am ':' getrennt und in temporäres Array gepackt
@prmnutzer = split(",", $tmp[1]); # Der wichtige Teil der Zeilen wird in Array gepackt
}
#print("Das Dokument beinhaltet: $zeilen Zeilen!\n"); # Anzahl der Zeilen wird ausgegeben
}
The Permissions file looks like this:
##
!*EDIT.FIRMA.FIR_BELEGNR_007:
## Sperrt im Einkauf das Editieren des ursp. Bestelltermins
!*EDIT.POSITIONEN.POS_TERM2:
##
!INFOPT*ARTSTAT.AST_BSTWERT:EDV,EK,EKL,GF,KSLMITEK,VT,EKIWE,EKTEC,KSIWE,EKHOT
## SperrtdieStammdateninderArtikelverwaltung.
ART_VERW*M0805.1:
##
!*EDIT.EINKVERBAND.EKV_X_SZBNR:KATEI,EDV,MHERGEN,CJANSSENS
## SperrteditierenvobRabattB-ArtikelimKunden
!*EDIT.KUNDEN.KDN_GRRAB_002:EDV,MGREB,JEIFERT,CJANSSENS,RWAUMANS,NWACHALL
The usernames are listed after the ":" everything before that is irrelevant.
And the Active Users File looks like this:
FAX800
LABDELHALIM
DABEL
LABU
UACKERMANN
DADAEM
CADLER
SADOLF
FAFF
KAGOCS
JAHLHEIM
JAIGNER
KAIZELKSNIS
NAKGUEN
DALAERTS
JALBERS
SALBERT
SALBERTSILA
FALBERTS
SALBRECHT
AALEX
MALGAC
BALLES
YALTUNTAS
SAMBERG
KAMESEDER
BAMLING
CAMLUNG
UAMSUESS
KANDERS
MANDRAE
AANDRAE
GAND
AANTO
EAPPEL
AAPPEL
AAPPELWILPEG
BAPTOULACH
Thats just a snippet, the file has about 2000 lines...
And also i edit on Mac OS, if that's relevant.
ps. Im really new to Perl and Programming in General :D
Upvotes: 0
Views: 73
Reputation: 6798
Please see the following code snippet for possible approach to the problem.
read active users into array and return array reference
read permissions users into array and return array reference
create hash with active users
go through permission users and see if user is active
- if user is not active save his id into 'users_ceased' array
output content of 'users_ceased' to console
NOTE:
Adjust filenames accordingly to your setup, data and format of input is preserved from posted
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $debug = 1;
my $fn_users = 'data_active_users.dat'; # File with active users
my $fn_perms = 'data_permission.dat'; # File with permissions
my $users_act = read_users($fn_users); # Read active users
my $users_perm = read_perms($fn_perms); # Read permission users
say Dumper($users_act) if $debug;
say Dumper($users_perm) if $debug;
my %active;
my @users_ceased;
$active{$_}++ for @{ $users_act }; # Fill hash with active users
for my $user ( @{ $users_perm } ) {
push @users_ceased, $user if not $active{$user};
}
say '
Ceased users
-----------------------------';
say for @users_ceased;
sub read_users {
my $filename = shift;
open my $fh, '<:encoding(utf8)', $filename
or die "Couldn't open $filename";
my @users = <$fh>;
close $fh;
chomp @users;
return \@users;
}
sub read_perms {
my $filename = shift;
my @users;
open my $fh, '<:encoding(utf8)', $filename
or die "Couldn't open $filename";
while( <$fh> ) {
next unless /.*?:(.*)/;
my $list = $1;
next if not defined $list;
my @add_users = split ',', $list;
@users = (@users, @add_users);
}
close $fh;
return \@users;
}
Ouput
Ceased users
-----------------------------
EDV
EK
EKL
GF
KSLMITEK
VT
EKIWE
EKTEC
KSIWE
EKHOT
KATEI
EDV
MHERGEN
CJANSSENS
EDV
MGREB
JEIFERT
CJANSSENS
RWAUMANS
NWACHALL
With slight code modification
my %active;
my %users_ceased;
$active{$_}++ for @{ $users_act }; # Fill hash with active users
for my $user ( @{ $users_perm } ) {
$users_ceased{$user}++ if not $active{$user};
}
say '
Ceased users
-----------------------------';
for my $user ( sort keys %users_ceased ) {
printf "%-10s => %2d time(s)\n", $user, $users_ceased{$user};
}
Output will be following
Ceased users
-----------------------------
CJANSSENS => 2 time(s)
EDV => 3 time(s)
EK => 1 time(s)
EKHOT => 1 time(s)
EKIWE => 1 time(s)
EKL => 1 time(s)
EKTEC => 1 time(s)
GF => 1 time(s)
JEIFERT => 1 time(s)
KATEI => 1 time(s)
KSIWE => 1 time(s)
KSLMITEK => 1 time(s)
MGREB => 1 time(s)
MHERGEN => 1 time(s)
NWACHALL => 1 time(s)
RWAUMANS => 1 time(s)
VT => 1 time(s)
Upvotes: 1