Not_a_Number has asked for the wisdom of the Perl Monks concerning the following question:
which outputs “foo”.my @x; push @x, [ 0 .. 3 ] for 0 .. 3; # creates a basic 2D array my @y = @x; $x[0][2] = 'foo'; print $y[0][2];
use strict; use warnings; my @word = qw ( F O O B A R ); my $letter = 'X'; # no prob with FOOBAR if $letter = ‘A’ # Two dimensional: doesn't work my @matrix2D; push @matrix2D, [ split '', '.......' ] for 0 .. 2; $matrix2D[0][4] = $letter; my @temp_matrix2D = @matrix2D; for my $i ( 0 .. $#word ) { unless ( $temp_matrix2D[0][$i] =~ /[.$word[$i]]/ ) { print 'Error!: '; @temp_matrix2D = @matrix2D; last; } $temp_matrix2S[0][$i] = $word[$i]; } @matrix2D = @temp_matrix2D; print "@{$matrix2D[0]}\n"; # prints “Error: F O O B X . .’, which I don’t want # One dimensional: works, but no good to me... my @matrix = split '', '.......'; $matrix[4] = $letter; my @temp_matrix = @matrix; for my $i ( 0 .. $#word ) { unless ( $temp_matrix[$i] =~ /[.$word[$i]]/ ) { print 'Error!: '; @temp_matrix = @matrix; last; } $temp_matrix[$i] = $word[$i]; } @matrix = @temp_matrix; print "@matrix\n"; # prints “Error!: . . . . X . .’, which is what I want
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Copying two-dimensional arrays
by tall_man (Parson) on May 17, 2003 at 20:48 UTC | |
Re: Copying two-dimensional arrays
by broquaint (Abbot) on May 17, 2003 at 21:53 UTC | |
•Re: Copying two-dimensional arrays
by merlyn (Sage) on May 17, 2003 at 22:55 UTC | |
Re: Copying two-dimensional arrays
by graff (Chancellor) on May 17, 2003 at 21:02 UTC | |
Re: Copying two-dimensional arrays
by Not_a_Number (Prior) on May 18, 2003 at 10:30 UTC |