I noticed a few issues with your code, see my edits/additions below. I put your example data in a file called report.txt and tested your script using it. I get the same errors. I printed your hash using Data::Dumper, which made troubleshooting easier. However, it can still be difficult to see problematic extra white space.
split(/\s*\|\s*/, $line)
will remove the spaces from your parsed data.@{$k{$_}{added}} ne ''
I suggest that you check that the value exists, like this:exists($k{$_}{added})
print "${k{user}}\n";
so I removed it from the code.This code,
gives the following output:use strict; use warnings; use diagnostics; use Data::Dumper; open(FIL,"report.txt") or die("$!"); my %k=(); while (my $line=<FIL>) { next if $. < 2; my ($user,$roles,$type,$dt,$empty1,$empty2)=split(/\s*\|\s*/,$ +line); push @{$k{$user}{$type}}, $roles; } my @names = sort keys(%k); # Dumping the reference to the hash to more clearly show its structure + print Dumper \%k; foreach ( @names ) { print "Name: ", $_, "\n"; if( exists($k{$_}{added}) ){ print "\tAdded:\n\t\t"; print join ',', @{$k{$_}{added}}; print "\n"; } if ( exists($k{$_}{deleted}) ) { print "\tDeleted:\n\t\t"; print join ',', @{$k{$_}{deleted}}; print "\n"; } }
$VAR1 = { 'cdf' => { 'added' => [ 'deploy' ] }, 'xyz' => { 'deleted' => [ 'admin', 'deploy' ] }, 'abc' => { 'added' => [ 'admin', 'deploy' ], 'deleted' => [ 'developer' ] } }; Name: abc Added: admin,deploy Deleted: developer Name: cdf Added: deploy Name: xyz Deleted: admin,deploy
In reply to Re: Aggregating the column based on the common column values
by kevbot
in thread Aggregating the column based on the common column values
by kanskr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |