in reply to Re^4: 36 Conditions in If Statement [sendhelp]]
in thread 36 Conditions in If Statement [sendhelp]]

When possible I try to assign and use something like $i instead of $_. Is this a bad habit?

This is, IMHO, a very good habit! I tend to write all my block-structured for-loops in the form

for my $loop_iterator_variable (@whatever) { ... do_something_with($loop_iterator_variable); ... }

However, for statement-modifier forms of the for-loop and things like map and grep statements, I'm quite comfortable using  $_ in
    do_something_with($_) for @whatever;
or
    my @new_array = map  { do_something_with($_) } @whatever;
    my @new_array = grep { do_something_with($_) } @whatever;
and similar, fairly simple statements, and see no harm; the idiom seems quite clear and useful in these cases. Indeed, it's a bit awkward to use a named variable here:
    do { my $n = $_;  do_something_with($n) } for @whatever;

But rest assured that the use of  $_ in these cases is perfectly safe and does what you expect — as long as you expect that  $_ is an alias, and assigning to it assigns to items of the list or array over which you are iterating! (Of course, the same is true of the iterator variable, named or default, in a block-structured for-loop.)


Give a man a fish:  <%-{-{-{-<