in reply to Make array from field of another array

Saved,
I agree that you have done a poor job of explaining. Because you have indicated you can accomplish the job using split, I will assume you have some type of delimited data. That might simply look like:
my @new_array = split /:/, $old_array[0];
Then you go on to say iterate which makes me think you want the new array to be the result of all the elements of the old array:
my @new_array = map {split /:/, $_} @old_array;
You might also mean something completely different so I would recommend you provide an example of how you are doing it now that you don't care for and see if we can improve upon that.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Make array from field of another array
by Saved (Beadle) on Apr 07, 2011 at 16:25 UTC
    your Code:
    my @new_array = map {split /:/, $_} @old_array;
    Is nearly what I was hoping for, but could you do something to get only one field?
    my @new_array = map {split /:/, ${_[3]}} @old_array;
      You can access only one element of the split result by using a list index.

      my @new_array = map { (split /:/)[3] } @old_array;

      Note that I have relied on split's default of splitting $_ when not provided with an explicit string. So the above is exactly equivalent to

      my @new_array = map { (split /:/, $_)[3] } @old_array;

        That is it exactly, Thanx very much.
        That is it exactly, Thanx very much.
        This appears to work, thanx for your help.
        #!/usr/bin/perl -w #<UnusedGrp.pl> Find unused Groups in /etc/group use strict; my $GROUPfile="/etc/group"; my $gfpid = open(GFILE, "<$GROUPfile") or die "$GROUPfile File Not Fou +nd: $!\n"; my $PASSWD="/etc/passwd"; my $pfpid = open(PFILE, "<$PASSWD") or die "$PASSWD File Not Found: $! +\n"; my @PWLINES = <PFILE>; my @GIDS = map { (split /:/)[3] } @PWLINES; while (my $line = <GFILE>) { my @GRP = split(/:/, $line); if (length($GRP[3]) < 2){ #print "$GRP[0]:$GRP[2] is zero length\n"; if ( ! (grep $_ eq $GRP[2], @GIDS)) { print "$GRP[0]:$GRP[2] is Empty & not referenced in /etc/passwd +\n"; } } }
Re^2: Make array from field of another array
by Saved (Beadle) on Apr 07, 2011 at 15:52 UTC
    I have updated to post with more detail of what I am trying to do, and some code so far.