kanskr has asked for the wisdom of the Perl Monks concerning the following question:
My data is like below
Username | Roles | Type |date |SR NO|Remarks| abc|admin |added | 01072015 abc|developer |deleted |01072015 abc|deploy |added |01072015 xyz |admin |deleted |01072015 xyz| deploy|deleted|01072015 cdf|deploy|added|01072015
and i need to do the following a) i need to group based on the user, roles and concatenate the roles filed with the delimiter "," b) I need to create the output as below.
Username |roles added |roles deleted |date username |roles_added |roles deleted |date abc |admin,deploy |developer |01072015 xyz ||admin,deploy |01072015 cdf |deploy||01072015
In order to achieve this,am using the code below
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(/\|/,$line); push @{$k{$user}{$type}}, $roles; } my @names=(sort keys(%k)); foreach ( @names) { if ( @{$k{$_}{Added}} ne ''){ print "${k{user}}\n"; print join ',', @{$k{$_}{Added}}; print "\n"; } if ( @{$k{$_}{Deleted}} ne '') { print join ',', @{$k{$_}{Deleted}}; print "\n"; } }
The script fails with below error Can't use an undefined value as an ARRAY reference at report3.pl line 34, <FIL> line 23. at report3.pl line 34 Please help me how i can generate the output as expected. Many thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Aggregating the column based on the common column values
by kevbot (Vicar) on Jul 03, 2015 at 05:55 UTC | |
|
Re: Aggregating the column based on the common column values
by kcott (Archbishop) on Jul 03, 2015 at 13:11 UTC |