techjohnny has asked for the wisdom of the Perl Monks concerning the following question:

My text file contains a line that consists of a group,e.g., 50005, following the group number, there are one more user name, e.g., usera, userb. Each group and user name is separated by a newline.


50000
Craig
Mark
50001
Craig
Steve
50002
Durlene
Floyd
Jeremy
Kent
Leroy
Jessie
Rocky
Sean
Craig
Jeffrey
Rick

#!/usr/bin/perl use warnings; use strict; # comments for myscript_342.pl my $groups; my @line; open my $file, "<", "groupsmembers.txt"; open my $output, ">", "/mnt/backup/output.txt"; while (<$file>) { push @line, $_; } chomp @line; for (@line) { s/^\d+/\n/; s/(\w+)/$1,/; print $output "$_"; }

The above script sorta does what I want, which is to separate the groups and list the user names like: usera, userb, userc each entire group members separated by a newline.

50000 Craig, Mark

Replies are listed 'Best First'.
Re: ADSI groups users
by GrandFather (Saint) on Mar 04, 2011 at 04:26 UTC

    and your question is what?

    As an aside the code you give doesn't do what you seem to want. The following may suit you better:

    #!/usr/bin/perl use warnings; use strict; my $groupCode; my @members; while (defined(my $line = <DATA>) || $groupCode) { if (defined($line) && $line !~ /^(\d+)$/) { chomp $line; push @members, $line if defined $groupCode; next; } if (defined $groupCode) { @members = '-- no members --' if !@members; print "$groupCode ", join(', ', @members), "\n"; } $groupCode = $1; @members = (); } __DATA__ 50000 Craig Mark 50001 Craig Steve 50002 Durlene Floyd Jeremy Kent Leroy Jessie Rocky Sean Craig Jeffrey Rick

    Prints:

    50000 Craig, Mark 50001 Craig, Steve 50002 Durlene, Floyd, Jeremy, Kent, Leroy, Jessie, Rocky, Sean, Craig, + Jeffrey, Rick
    True laziness is hard work
Re: ADSI groups users
by Anonymous Monk on Mar 04, 2011 at 04:59 UTC
    #!/usr/bin/perl -- use 5.010000; use strict; use warnings; use autodie; use Data::Dumper; Main(@ARGV); exit(0); sub Main { my $sample = join "\n", qw[ 0 a b c 1 d e f 666 lucy damian larry +]; open my $inMemory, '<', \$sample ; my $HoA = ReadADSI( $inMemory); close $inMemory; print Dumper($HoA); } sub ReadADSI { my ($fh) = @_; my %HoA; my $Group = 0; while (<$fh>) { chomp; given ($_) { when (/^\d+$/) { $Group = $_; $HoA{$Group} = []; } default { push @{ $HoA{$Group} }, $_; } } ## end given } ## end while (<$fh>) return \%HoA; } ## end sub ReadADSI __END__ $VAR1 = { '1' => [ 'd', 'e', 'f' ], '0' => [ 'a', 'b', 'c' ], '666' => [ 'lucy', 'damian', 'larry' ] };
Re: ADSI groups users
by locked_user sundialsvc4 (Abbot) on Mar 04, 2011 at 14:20 UTC

    I naturally look at such problems with an eye toward so-called finite state machine logic.   While this particular example is trivial, the idea is more-or-less like this:

    The algorithm will loop through all of the zero-or-more input records that exist.   As it does so, it will also keep some kind of “recollection of” what it has seen before ... memories of data that might have been gleaned from previous records, and, importantly, “what state we are now in.”   In this example, we might say the states are (say...) SKIPPING_FOR_GROUP_NAME and READING_USER_NAMES.   There is always an “initial state” that is established before the record-reading loop begins.

    Each input record is classified both in terms of its content (e.g. “it consists entirely of digits”), and by the current state.   In response, the algorithm (a) decides what to do with this value, and (b) maybe changes the value of “current state.”

    There are always activities that must occur both during the reading-loop and then one more time after the loop ends.

    As always, writing an actual algorithm in a production setting is a balancing-act between sufficiency, robustness, clarity, simplicity and overkill.

      Awesome!