[ create a new paste ] login | about

Link: http://codepad.org/1XvvYiyx    [ raw code | output | fork ]

Perl, pasted on Mar 7:
use Data::Dumper;

# Programming Perl Chapter 1 / p.34
# Note that the loop variable refers to the element itself, rather than a copy of the
# element. Hence, modifying the loop variable also modifies the original array.

my @hoge = 1 .. 10;
my @fuga = @hoge;
my @foo  = @hoge;

print "-" x 30 . "\n";
print Dumper \@hoge, \@fuga, \@foo;

foreach (@hoge) {
  $_ += 30;
}

foreach my $item (@fuga) {
  $item += 30;
}

foreach my $item (@foo) {
  my $bar = $item;
  $bar += 30;
}

print "-" x 30 . "\n";
print Dumper \@hoge, \@fuga, \@foo;


Output:
------------------------------
$VAR1 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10
        ];
$VAR2 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10
        ];
$VAR3 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10
        ];
------------------------------
$VAR1 = [
          31,
          32,
          33,
          34,
          35,
          36,
          37,
          38,
          39,
          40
        ];
$VAR2 = [
          31,
          32,
          33,
          34,
          35,
          36,
          37,
          38,
          39,
          40
        ];
$VAR3 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10
        ];


Create a new paste based on this one


Comments: