in reply to grouping lines of data together for later use

Problems I see in your code:

Here is my re-write of your core code, using HoA grouping and hash slices. Working, tested code:

#!/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 (<DATA>) { 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

Replies are listed 'Best First'.
Re^2: grouping lines of data together for later use
by blazar (Canon) on Sep 27, 2005 at 15:29 UTC
    When you said while (<@dataarray>), I shuddered.
    Indeed, when I saw it, I thought the code had not been really tested as claimed, for I could have bet it would have yielded an error. But this doesn't seem to be the case:
    $ perl -MO=Deparse -e '@a=qw/a b c/; print while <@a>' @a = ('a', 'b', 'c'); use File::Glob (); print $_ while defined($_ = glob(join($", @a))); -e syntax OK
    and actually IMHO by virtue of an (un?)fortunate chance:
    $ perl -le '@a=qw/a b c/; print while <@a>' a b c
    It seems that while (<whatever>)'s magic applies even if those angular parens are not to be interpreted as the IO-diamond operator but as the glob one. Now I wonder if this is a (perhaps unavoidable) side-effect or if it is intentional, although I doubt about the latter possibility.

    Well, this may be a good subject for another question, or for a meditation... Update: done!