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

Hello, I am having troubles with running an array through a foreach loop. Let's start with the background:
I am creating an array with the following code:
push(@userData, $user->getID(), ";",$user->getExternalID(), ";", $user +->getExternalParentID(), ";", $user->getSid(), ";", $user->getDomain( +), ";", $user->getLockedTimes() );
where $user->getSomething() is defined elsewhere. The above push statement may be called more than one time. But for now, let's just assume that it is only called once. When I print out the above array, I get the following (which is what I am expecting):

206;homefoldertest1238;TODO;F030624;NAEAST;19

The next thing that I do is run the above array through a foreach loop to process the data in it. To do that I have the following code:
foreach (@currentData) { my ($recordNum, $externalID, $externalParent, $ssid1, $domain1 +, $timesLocked) = split(/;/, $_); print @currentData; print "\nin the foreach loop for $recordNum\n"; }
I was expecting the loop to run once, printing the following:
206;homefoldertest1238;TODO;F030624;NAEAST;19
in the foreach loop for 206


To my surprise, I recieved the following:
206;homefoldertest1238;TODO;F030624;NAEAST;19
in the foreach loop for 206
;homefoldertest1238;TODO;F030624;NAEAST;19
in the foreach loop for
homefoldertest1238;TODO;F030624;NAEAST;19
in the foreach loop for homefoldertest1238
;TODO;F030624;NAEAST;19
in the foreach loop for
TODO;F030624;NAEAST;19
in the foreach loop for TODO
;F030624;NAEAST;19
in the foreach loop for
F030624;NAEAST;19
in the foreach loop for F030624
;NAEAST;19
in the foreach loop for
NAEAST;19
in the foreach loop for NAEAST
;19
in the foreach loop for
19
in the foreach loop for 19


What am I doing wrong here?! Any help would be greatly appreciated.

Frank

Replies are listed 'Best First'.
Re: arrays and foreach
by chromatic (Archbishop) on Aug 16, 2012 at 17:14 UTC

    Your split is useless. You're pushing eleven distinct elements onto the array, and you're iterating over them one at a time. Print $_ within your loop to see that.

    (You can also print your array with Data::Dumper.)

    You could instead concatenate the array elements together with . instead of ,, but you're better off using an anonymous array and avoiding the split altogether. (What happens if one of the textual elements contains a semicolon?)


    Improve your skills with Modern Perl: the free book.

Re: arrays and foreach
by Anonymous Monk on Aug 16, 2012 at 17:15 UTC

    You're confused about, whats is in an array, and what that array stringifies as

    You can see the difference if you Dumper as recommended in Basic debugging checklist

    $ perl -le " use Data::Dumper; @f = qw( 1 ; 2 ; 3 ); print Dumper( \@f + ); print @f; " $VAR1 = [ '1', ';', '2', ';', '3' ]; 1;2;3

      I forgot about Dumper. Thanks for reminding me. It seems my was trying to get too tricky with my original code. I fixed the error by doing the following when I "pushed" data into the array:

      my $id1=$user->getID(); my $eID=$user->getExternalID(); my $epID=$user->getExternalParentID(); my $sid1=$user->getSid(); my $idD=$user->getDomain(); my $idL=$user->getLockedTimes(); push(@userData, "$id1;$eID;$ipID;$sid1;$idD;$idL");

      It all works as expected now.

        Instead of push(@userData, "$id1;$eID;$epID;$sid1;$idD;$idL");, may I recommend

        push(@userData, join(";", $id1, $eID, $ipID, $sid1, $idD, $idL), );
        or
        push(@userData, [ $id1, $eID, $epID, $sid1, $idD, $idL ], );
        or
        push(@userData, { id => $id1, externalId => $eID, externalParentId => $epID, sid => $sid1, domain => $idD, lockedTimes => $idL, }, );

        The first alternative makes it easier to change the delimiter, not repeat yourself, etc. The second allows you to avoid the split later on in your code. The third documents (for some definition of documentation) the structure that you are using.

        Depending on your needs, one of these may be more appropriate.

        --MidLifeXis