Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Two variables changed by tr

by hippo (Archbishop)
on Jul 03, 2024 at 18:36 UTC ( [id://11160355]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11160355]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2025-06-22 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.