#!/usr/bin/perl -W use strict; use warnings 'all'; use Data::Dumper; $Data::Dumper::Useqq = 1; $| = 1; my @field_names = qw( PROP COLOUR TXTCOL URL360 USER YOUR_NAME ADDRESS TOWN ZIP_CODE COUNTRY EMAIL TELEPHONE_NO TELEPHONE_NO2 THEME WEB_ADDRESS PPEMAIL JUNK1 JUNK2 JUNK3 ); # Simplified @field_names for this example: @field_names = qw( PROP COLOUR USER YOUR_NAME ); # 'Prop' is a unique key in file PAGE and in hash %onprop. my %onprop; # HoH # 'user' is not unique, so each hash entry will be an array of 'Prop's. my %user_props; # HoA while () { chomp; next unless /\S/; my @fields = split "\t"; warn unless @fields == @field_names; my %h; @h{@field_names} = @fields; my $prop = $h{PROP} or warn; my $user = $h{USER} or warn; # Create array of users and hash of data warn "Overwriting '$prop'" if exists $onprop{$prop}; $onprop{$prop} = \%h; push @{ $user_props{$user} }, $prop; } print Data::Dumper->Dump( [ \%onprop, \%user_props ], [ qw( *onprop *user_props ) ], ); print join("\t", @field_names), "\n\n"; foreach my $user ( sort keys %user_props ) { my @prop_list = @{ $user_props{$user} }; my $number_of_props = @prop_list; print "Grouped data ($number_of_props lines) for user '$user'\n"; foreach my $prop (@prop_list) { my %h = %{ $onprop{$prop} }; my @fields = @h{@field_names}; print join("\t", @fields), "\n"; } print "\n"; } __DATA__ foo red jonnyfolk Saint J bar white Util Me baz blue jonnyfolk Saint J qux black Util Me2