jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I have a data file in which holds information on different items accorded to individual users. A user might have more than one item (each one being unique), in which case he is accorded a new line for each item.
Users with multiple items are treated differently to those with just the one and so I am trying to group together the lines for each user and then trat them on a case by case basis.
I have written the following code which does seem to do the trick, but I would like advice and comments from my fellow monks on how I could do it better (and to ensure that I'm not fluking the results and thus saving up problems for the future.
I have commented the code to try to explain my thought process (just in case you thought there wasn't one!! )
Any help given would be much appreciated.
open PAGE, "$website" or die "Cant open $website: $!"; flock (PAGE, 1) or die "Can't lock website file for reading"; while (my $line = (<PAGE>)) { ($Prop,$colour,$txtcol,$url360,$user,$your_name,$address,$town,$zip_co +de,$country,$email,$telephone_no,$telephone_no2,$theme,$web_address,$ +ppemail,undef,undef,undef) = split "\t", $line; #create array of users and hash of data $onprop{$Prop} = [$Prop,$colour,$txtcol,$url360,$user,$your_name,$ +address,$town,$zip_code,$country,$email,$telephone_no,$telephone_no2, +$theme,$web_address,$ppemail]; push (@dataarray, $user); } #iterate over users while (<@dataarray>) { my @microdata; #remove examine first user my $item = shift (@dataarray); foreach my $scalar (keys %onprop) { unless (! $item) { #compare lines of hash looking for lines with same user if ( @{ $onprop{ $scalar } }[4] eq $item) { #if found, remove from array my $a = shift(@dataarray); push (@microdata, @{ $onprop{ $scalar } }); } } } #store all results for this owner in single reference my $refmicro = \@microdata; push (@group, $refmicro); } foreach (@group) { print "@{ $_ }<br><br>"; }
#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; #definitions: #========= my ( $Prop, $colour, $txtcol, $url360, $user, $your_name, $address, $town, $zip_code, $country, $email, $telephone_no, $telephone_no2, $theme, $web_address, $ppemail ); my %onprop; my @dataarray; my @group; print "Content-type: text/html\n\n"; while (my $line = (<DATA>)) { ($Prop,$colour,$txtcol,$url360,$user,$your_name,$address,$town,$zip_co +de,$country,$email,$telephone_no,$telephone_no2,$theme,$web_address,$ +ppemail,undef,undef,undef) = split "\t", $line; #create array of users and hash of data $onprop{$Prop} = [$Prop,$colour,$txtcol,$url360,$user,$your_name,$ +address,$town,$zip_code,$country,$email,$telephone_no,$telephone_no2, +$theme,$web_address,$ppemail]; push (@dataarray, $user); } #iterate over users while (<@dataarray>) { my @microdata; #remove examine first user my $item = shift (@dataarray); foreach my $scalar (keys %onprop) { unless (! $item) { #compare lines of hash looking for lines with same user if ( @{ $onprop{ $scalar } }[4] eq $item) { #if found, remove from array my $a = shift(@dataarray); push (@microdata, @{ $onprop{ $scalar } }); } } } #store all results for this owner in single reference my $refmicro = \@microdata; push (@group, $refmicro); } foreach (@group) { print "@{ $_ }<br><br>"; } __DATA__ 012 undef undef undef graham undef u +ndef undef undef undef undef 037 undef undef graham undef undef undef unde +f undef undef undef undef undef 028 red tdblk johnandmark undef undef undef +undef undef undef undef undef 108 yellow tdblk undef johnandmark undef undef u +ndef undef UK undef undef undef undef + 013 blue tdblk undef jon undef undef undef un +def undef undef undef undef undef undef undef + 008 blue tdblk malcolm undef undef undef unde +f undef undef undef undef 133 green tdblk sharon undef undef undef unde +f uk undef undef undef undef undef 047 blue tdblk gill undef undef undef undef + undef undef undef undef undef
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: grouping lines of data together for later use
by reasonablekeith (Deacon) on Sep 27, 2005 at 13:11 UTC | |
|
Re: grouping lines of data together for later use
by Util (Priest) on Sep 27, 2005 at 14:25 UTC | |
by blazar (Canon) on Sep 27, 2005 at 15:29 UTC | |
|
Re: grouping lines of data together for later use
by blazar (Canon) on Sep 27, 2005 at 12:09 UTC | |
by jonnyfolk (Vicar) on Sep 27, 2005 at 12:24 UTC | |
by blazar (Canon) on Sep 27, 2005 at 12:50 UTC |