FAQ
I am looking to send a mail message to a large group of users and
indicate which groups they are assigned to. Currently I have a tab
deliniated file which has in column 1 the email address, column 2 the
group, and column 3 the user's name. Using sample data I would like:

s.t.dog@nowhere.com\tFootball\tSpot T. Dog
jon.doe@nowhere.com\tFootball\tJonathon Doe
s.t.dog@nowhere.com\tBaseball\tSpot T. Dog
jon.doe@nowhere.com\tBaseball\tJonathon Doe
jane.doe@nowhere.com\tBaseball\tJane Doe
s.t.dog@nowhere.com\tHockey\tSpot T. Dog
jane.doe@nowhere.com\tHockey\tJane Doe
dick.doe@nowhere.com\tHockey\tRichard Doe

To be formatted like:

s.t.dog@nowhere.com\tBaseball, Football, Hockey\tSpot T. Dog
jon.doe@nowhere.com\tBaseball, Football\tJonathon Doe
jane.doe@nowhere.com\tBaseball, Hockey\tJane Doe
dick.doe@nowhere.com\tHockey\tRichard Doe

...so that I do not have to send multiple emails. I am not sure if I
should be using a hash or an array to accomplish this task. I would
like to know how to loop through the data to pull out the groups for
each email address. It would also be nice if I could sort each group
since some users have over 30+ groups assigned to them.

Any assistance would be greatly appreciated,

Search Discussions

  • Mr. Shawn H. Corey at Sep 27, 2008 at 11:38 am

    On Fri, 2008-09-26 at 10:03 -0700, cca.johnson@gmail.com wrote:
    I am looking to send a mail message to a large group of users and
    indicate which groups they are assigned to. Currently I have a tab
    deliniated file which has in column 1 the email address, column 2 the
    group, and column 3 the user's name. Using sample data I would like:

    s.t.dog@nowhere.com\tFootball\tSpot T. Dog
    jon.doe@nowhere.com\tFootball\tJonathon Doe
    s.t.dog@nowhere.com\tBaseball\tSpot T. Dog
    jon.doe@nowhere.com\tBaseball\tJonathon Doe
    jane.doe@nowhere.com\tBaseball\tJane Doe
    s.t.dog@nowhere.com\tHockey\tSpot T. Dog
    jane.doe@nowhere.com\tHockey\tJane Doe
    dick.doe@nowhere.com\tHockey\tRichard Doe

    To be formatted like:

    s.t.dog@nowhere.com\tBaseball, Football, Hockey\tSpot T. Dog
    jon.doe@nowhere.com\tBaseball, Football\tJonathon Doe
    jane.doe@nowhere.com\tBaseball, Hockey\tJane Doe
    dick.doe@nowhere.com\tHockey\tRichard Doe

    ...so that I do not have to send multiple emails. I am not sure if I
    should be using a hash or an array to accomplish this task. I would
    like to know how to loop through the data to pull out the groups for
    each email address. It would also be nice if I could sort each group
    since some users have over 30+ groups assigned to them.

    Any assistance would be greatly appreciated,
    #!/usr/bin/perl

    use strict;
    use warnings;

    use Data::Dumper;
    $Data::Dumper::Sortkeys = 1;
    $Data::Dumper::Indent = 1;
    $Data::Dumper::Maxdepth = 0;

    my @names = ();
    my %list = ();

    while( <> ){
    chomp;
    my ( $email, $group, $name ) = split /\t/, $_;
    push @names, $name unless exists $list{$name};
    $list{$name}{email} = $email;
    $list{$name}{groups}{$group} = 1;
    }
    # print '%list : ', Dumper \%list;

    for my $name ( @names ){
    my $groups = join( ', ', sort keys %{ $list{$name}{groups} } );
    print join( "\t", $list{$name}{email}, $groups, $name ), "\n";
    }

    __END__


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Linux is obsolete.
    -- Andrew Tanenbaum
  • John W. Krahn at Sep 27, 2008 at 1:17 pm

    cca.johnson@gmail.com wrote:
    I am looking to send a mail message to a large group of users and
    indicate which groups they are assigned to. Currently I have a tab
    deliniated file which has in column 1 the email address, column 2 the
    group, and column 3 the user's name. Using sample data I would like:

    s.t.dog@nowhere.com\tFootball\tSpot T. Dog
    jon.doe@nowhere.com\tFootball\tJonathon Doe
    s.t.dog@nowhere.com\tBaseball\tSpot T. Dog
    jon.doe@nowhere.com\tBaseball\tJonathon Doe
    jane.doe@nowhere.com\tBaseball\tJane Doe
    s.t.dog@nowhere.com\tHockey\tSpot T. Dog
    jane.doe@nowhere.com\tHockey\tJane Doe
    dick.doe@nowhere.com\tHockey\tRichard Doe

    To be formatted like:

    s.t.dog@nowhere.com\tBaseball, Football, Hockey\tSpot T. Dog
    jon.doe@nowhere.com\tBaseball, Football\tJonathon Doe
    jane.doe@nowhere.com\tBaseball, Hockey\tJane Doe
    dick.doe@nowhere.com\tHockey\tRichard Doe

    ...so that I do not have to send multiple emails. I am not sure if I
    should be using a hash or an array to accomplish this task. I would
    like to know how to loop through the data to pull out the groups for
    each email address. It would also be nice if I could sort each group
    since some users have over 30+ groups assigned to them.

    Any assistance would be greatly appreciated,
    Probably something like this:

    #!/usr/bin/perl
    use warnings;
    use strict;

    my $old_file = 'tab_deliniated_file';
    my $new_file = 'tab_deliniated_new';

    open my $OLD, '<', $old_file or die "Cannot open '$old_file' $!";
    open my $NEW, '>', $new_file or die "Cannot open '$new_file' $!";

    my %data;
    while ( <$OLD> ) {
    my ( $email, $group, $name ) = split /\t/;
    push @{ $data{ $email, $name } }, $group;
    }

    for my $key ( keys %data ) {
    local $" = ', ';
    $key =~ s/$;/\t@{$data{$key}}\t/;
    print $NEW $key;
    }

    __END__



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 26, '08 at 5:03p
activeSep 27, '08 at 1:17p
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase