in reply to Two variables changed by tr

foreach aliases into the list rather than copying. As you have 2 nested loops over the same array, half the time your two lexicals are aliases for the same item in the array.


🦛

Replies are listed 'Best First'.
Re^2: Two variables changed by tr
by glendeni (Sexton) on Jul 03, 2024 at 19:45 UTC
    Thanks - learned something new today
      Compare:
      use warnings; use strict; my @chars = ( 'a', 'b' ) ; foreach my $char1 ( @chars ) { foreach my $char2 ( @chars ) { print "BEFORE: $chars[0] $chars[1] \n" ; $char2 =~ tr/a-z/A-Z/ ; # $char1 ALSO UPPER-CASED !? print " AFTER: $chars[0] $chars[1] \n" ; } }
Re^2: Two variables changed by tr
by ikegami (Patriarch) on Jul 04, 2024 at 16:20 UTC

    Example:

    my @chars = qw( a b c ); for my $char ( @chars ) { $char = uc( $char ); } say "@chars"; # A B C