> Gunnar Hjalmarsson wrote: > > David Greenberg wrote: > > #!/usr/bin/perl > > use strict; > > open (GROUPFH, "<File1"); > > my @groups = (); > > while (my $line = <GROUPFH>) { > > chomp ($line); > > push (@groups, $line); > > } > > close (GROUPFH); > > open (USERFH, "File2"); > > my %group_to_users = (); > > while (my $line = <USERFH>) { > > chomp ($line); > > my ($user, @user_groups) = split ('\s+', $line); > > for my $group (@user_groups) { > > push (@{$group_to_users{$group}}, $user) if > $group_to_users{$group}; > > $group_to_users{$group} = [$user] unless > $group_to_users{$group}; > > } > > } > > close (USERFH); > > for my $group (@groups) { > > print $group . ":\n"; > > for my $user (@{$group_to_users{$group}}) { > > print $user . "\n"; > > } > > print "\n"; > > } > > Or a little shorter: > > #!/usr/bin/perl > use strict; > use warnings; > open my $GROUPS, 'File1' or die $!; > open my $USERS, 'File2' or die $!; > > my %groups; > chomp and $groups{$_} = [] for <$GROUPS>; > > while ( <$USERS> ) { > my ($user, @groups) = split; > $groups{$_} and push @{ $groups{$_} }, $user for @groups; > } > > for ( sort keys %groups ) { > print "$_:\n", ( join "\n", @{ $groups{$_} } ), "\n\n"; > } > > __END__ > It seems to me that Olakunle Banjo shall not be an Perl Expert. Can you also explain him the code?
Note: Usually is good policy closeing the opened files. Marcos -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>