Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > assignments of arrays

Reply
Thread Tools

assignments of arrays

 
 
Toralf Förster
Guest
Posts: n/a
 
      02-15-2013
In the following program I'd expect that line 29 fills @Values with zeros, but for i==2 this is counterproofed.

Why ?

$ nl -ba zero.pl
1 #!/usr/bin/perl
2
3 # Toralf Förster
4 # Hamburg
5 # Germany
6
7 use strict;
8 use diagnostics;
9 $diagnostics:RETTY = 1;
10
11 use warnings FATAL => 'uninitialized';
12 use Carp ();
13 $SIG{__DIE__} = \&Carp::confess;
14
15
16 my $Cols = 30;
17 my $Rows = 16;
18
19 my @Zero = ();
20 foreach my $r (0..$Rows+1) {
21 foreach my $c (0..$Cols+1) {
22 $Zero[$r][$c] = 0;
23 }
24 }
25
26 my $N = 10;
27 foreach my $i (1..$N) {
28
29 my @Values = @Zero;
30
31 foreach my $r (0..$Rows+1) {
32 foreach my $c (0..$Cols+1) {
33 if ($Values[$r][$c] != 0) {
34 die "i=$i\tr=$r\tc=$c\t$Values[$r][$c]\n";
35 }
36 }
37 }
38
39 foreach my $r (0..$Rows+1) {
40 foreach my $c (0..$Cols+1) {
41 $Values[$r][$c] = 1;
42 }
43 }
44 }
45
46
47 exit (0);
48


tfoerste@n22 ~/workspace/misc $ ./zero.pl
i=2 r=0 c=0 1
at ./zero.pl line 34



--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      02-15-2013
Be carefull Toralf Förster.
At your statement my @Values = @Zero;
you are "playing" with array references.
So when you fill the @Values with 1 what you are doing is at the same time
to fill the @Zero with 1
what you must do , to have what you mean is the following:
George Bouras
Athens, Greece


....

my $N = 10;
foreach my $i (1..$N)
{
my @Values = ();

for (my $i=0;$i<=$#Zero;$i++)
{
for (my $j=0;$j<=$#{$Zero[$i]};$j++)
{
$Values[$i][$j] = $Zero[$i][$j]
}

}

foreach my $r (0..$Rows+1)
{
foreach my $c (0..$Cols+1)
{
print "i=$i\tr=$r\tc=$c\t$Values[$r][$c]\n";
if ($Values[$r][$c] != 0) { die
"i=$i\tr=$r\tc=$c\t$Values[$r][$c]\n"; }
}


}

foreach my $r (0..$Rows+1) {
foreach my $c (0..$Cols+1) {
$Values[$r][$c] = 1 } }
}

exit 0;

 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      02-15-2013
Henry, try to avoid the hardcoded values of $Rows and $Cols by using the
array offsets !


Ο "Henry Law" *γραψε στο μήνυμα
news:(E-Mail Removed) ...

On 15/02/13 18:01, Toralf Förster wrote:
> In the following program I'd expect that line 29 fills @Values with zeros,
> but for i==2 this is counterproofed.
> 19 my @Zero = ();
> 20 foreach my $r (0..$Rows+1) {
> 21 foreach my $c (0..$Cols+1) {
> 22 $Zero[$r][$c] = 0;
> 23 }
> 24 }


 
Reply With Quote
 
Toralf Förster
Guest
Posts: n/a
 
      02-15-2013
On 02/15/2013 08:07 PM, George Mpouras wrote:
> you are "playing" with array references.
> So when you fill the @Values with 1 what you are doing is at the same
> time to fill the @Zero with 1


ick

@all
thx for the explanation


--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      02-16-2013
thanks nice
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      02-16-2013
On Friday, February 15, 2013 2:57:16 PM UTC-8, Ben Morrow wrote:
> Quoth "George Mpouras" <(E-Mail Removed) m.com.nospam>:
>
> > ...

>
> An easier way to do this is to use the Clone module:
>
>
>
> use Clone "clone";
>
>
>
> my @Values = map clone($_), @Zero;
>
> # or
>
> my @Values = @{ clone \@Zero };
>


Alternatively: Storable's dclone:

use Storable qw/dclone/;
@Values = @{ dclone \@Zero };

Storable is core which is a plus but I notice the docs mention:

"Clone is faster for data structures with 3 or less levels,
while dclone() can be faster for structures 4 or more levels deep."

--
Charles DeRykus

 
Reply With Quote
 
Toralf Förster
Guest
Posts: n/a
 
      02-17-2013
On 02/15/2013 08:07 PM, George Mpouras wrote:
> At your statement my @Values = @Zero;
> you are "playing" with array references.


OTOH this was so clear for me b/c if I pass parameters to a sub like foo
(@Values) then this means a call-by-value whereas foo (\@Values) passes
just the reference to the sub called "foo".


--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      02-17-2013
On 2013-02-17 08:16, Toralf Frster <(E-Mail Removed)> wrote:
> On 02/15/2013 08:07 PM, George Mpouras wrote:
>> At your statement my @Values = @Zero;
>> you are "playing" with array references.

>
> OTOH this was so clear for me b/c if I pass parameters to a sub like foo
> (@Values) then this means a call-by-value


No.

Parameters are passed by reference in Perl.

For example, consider this code:

sub foo {
$_[1] = 5;
}

my @x = (1, 2, 3);
foo(@x);
print "@x\n";

my ($x, $y, $z) = qw(a b c);
foo($x, $y, $z);
print "$x $y $z\n";
__END__

It prints
1 5 3
a 5 c
which clearly shows that the array @x and the variably $y were passed by
reference (in Perl jargon, we call this "aliasing", but it's the same
concept).

What creates the *illusion* that Perl function calls are by value is the
convention to immediately assign parameters to local variables. So you
would normally write foo as

sub foo {
my @p = @_;
$p[1] = 5;
}

or

sub foo {
my ($p1, $p2, $p3) = @_;
$p2 = 5;
}

Here the assignments in the second line alter only the local variable
(@p or $p2, respectively), not the parameters. But it's the assignment
in the first line which causes the values to be copied, not the function
call.

hp



--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpat. -- Ralph Babel
 
Reply With Quote
 
Toralf Förster
Guest
Posts: n/a
 
      02-17-2013
On 02/17/2013 03:41 PM, Peter J. Holzer wrote:
>What creates the *illusion* that Perl function calls are by value is the
>convention to immediately assign parameters to local variables. So you
>would normally write foo as

....
> Here the assignments in the second line alter only the local variable
> (@p or $p2, respectively), not the parameters. But it's the assignment
> in the first line which causes the values to be copied, not the function
> call.
>
> hp


ah - now I did understood it much better
Thx for this answer

--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
 
Reply With Quote
 
brakhagem@gmail.com
Guest
Posts: n/a
 
      07-11-2013
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Back-Annotate Assignments ALuPin VHDL 1 10-20-2004 09:21 PM
Using aggregates for assignments Gary Thorpe VHDL 4 06-15-2004 03:33 PM
Concurrent assignments to std_ulogic_vector slice is OK with ModelSim Nicolas Matringe VHDL 9 06-14-2004 10:10 PM
using entity attributes for pin number assignments Neil Zanella VHDL 2 10-26-2003 03:22 AM



Advertisments