in reply to Re^3: Arrays manipulation (no $i either)
in thread Arrays manipulation

Six of one, half a dozen of the other. You made the array explicit, I made the counter explicit. Personally, I prefer for split /,/, $row because it keeps the meat all on one easy-to-eat line. I'm also comfortable enough with C to be happy with the autoincrementing counter.

Didja benchmark them? (Kidding, of course.)

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^5: Arrays manipulation (no $i either)
by Aristotle (Chancellor) on May 03, 2003 at 00:57 UTC

    I'm not sure, but pretty certain that your version is more efficient, and you're also right that I traded one variable for another.

    The reason I prefer the version I posted is another, though: it has one less side effect. (I tried to do without the push too, but no practicable solution presented itself.) The fewer side effects, the more decoupled things become - f.ex, try changing your version such that it inserts an element from a different data source into a column determined by one of the fields of $row. That's a trivial change for the code I posted.

    Makeshifts last the longest.

      f.ex, try changing your version such that it inserts an element from a different data source into a column determined by one of the fields of $row. That's a trivial change for the code I posted.

      OK, let's be specific. Say the other data source was another array (call it @b) and say column 2 held the index into that array. Then I might do it like this:

      my @cols; for my $row (@array) { my $i = 0; push @{$cols[$i++]}, $i % 2 ? $_ : $b[$_] for split /,/, $row; }

      I'd call that a pretty trivial change.

      In general, that bit with the ternary operator could be replaced by any arbitrary code in a do{} block or a function that took, minimally, the index $i and the value $_ as arguments.

      Six of one...

      So, how would you do it with yours? :-)

      -sauoq
      "My two cents aren't worth a dime.";
      

        You didn't read all of what I wrote. If you ask me, your line is already getting very laden, and if you actually account for the kicker I added, it's likely to get.. ugly. That is, what happens if it's not always the same column, but changes depending on some field in $row?

        For my version, fixed column:

        my @cols; for my $row (@array) { my @f = split /,/, $row; $f[2] = $b[$f[2]]; push @{$cols[$_]}, $f[$_] for 0 .. $#f; }

        Only a very simple line to add. Same change, only more complex code, for varying column:

        my %col_for = ( foo => 2, bar => 4, baz => 5, ); my @cols; for my $row (@array) { my @f = split /,/, $row; splice @f, $col_for{$f[1]} || 6, 0, shift @b; push @{$cols[$_]}, $f[$_] for 0 .. $#f; }
        (Yes, I realize I introduced a side effect. You can just as easily have do by assigning slices to my @ff or something though, it doesn't change the structure of the code.)

        Makeshifts last the longest.