in reply to arrays and foreach

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

Replies are listed 'Best First'.
Re^2: arrays and foreach
by fritz1968 (Sexton) on Aug 16, 2012 at 17:29 UTC

    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