1 2 3 4 5 6
my ($ref, $back); $back = {}; $ref = {}; print $ref," ",$back, "\n"; $back = $ref; print $ref," ",$back, "\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
use strict; use warnings; use Data::Dumper; my $ref = { bla => 99, foo => 'ööö', blubb => 9, z => 'zett' }; my $back; print "Adressen: $ref # $back \n"; print Dumper ($ref); print Dumper ($back); %$back = %$ref; print "\n\nAdressen: $ref # $back \n"; print Dumper ($ref); print Dumper ($back);
1 2 3 4 5
my @names = qw(anton berta cäsar dora); foreach my $name(@names){ $name = 'unknown'; } print "@names"; # surprise ;)
QuoteIf any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. (...)
In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over.
2012-12-22T15:52:02 GwenDragonManchmal ist mir da etwas zu viel Perl-Magie.
1 2 3 4 5 6 7 8 9
#!/usr/bin/env python # coding: iso-8859-1 names = ["anton", "berta", "cäsar", "dora"] for name in names: name = 'unknown' print names # Geht nicht.
1 2 3 4 5 6 7 8 9
#!/usr/bin/env python # coding: iso-8859-1 names = ["anton", "berta", "cäsar", "dora"] for namenum in range(len(names)): names[namenum] = 'unknown' print names
1
2
3
4
5
6
# beachte die Adressierung mit AmpersAnd in der Schleife
$names = array('anton','berta');
foreach($names as &$name){
$name = 'nobody';
}
print_r($names); # alles 'nobody'
2012-12-22T21:29:51 hlubenow[...]
Die Frage ist dann aber, wie man's denn macht, wenn man's haben möchte. Also, ich mach' das so:
Code (python): (dl )1 2 3 4 5 6 7 8 9#!/usr/bin/env python # coding: iso-8859-1 names = ["anton", "berta", "cäsar", "dora"] for namenum in range(len(names)): names[namenum] = 'unknown' print names
[...]
names[:] = ['unknown'] * len(names)
@names = ('unknown') x @names