> -----Original Message----- > From: Gajo Csaba [mailto:[EMAIL PROTECTED]] > Sent: Thursday, October 31, 2002 10:29 AM > To: [EMAIL PROTECTED] > Subject: A very, very simple question > > > > Hi, > I've started learning Perl a few days ago, but I'm a > computer science student, so I'm not a complete idiot > :) > > This is the problem: I'm trying to make a simple 2D > matrix that represents the product of the numbers 1-10 > (don't know how to tell it in English, it's that table > that kids learn when their 2nd grade) > > I've wrote the following: > > sub init > { > for ($a=0;$a<10;$a++) > { > $table[$a,0] = $a;
In Perl, this needs to be written as: $table[$a][0] = $a; When you write $table[$a,0], the $a,0 is treated as a list evaluated in scalar context, which always returns the last element. So: $table[$a,0] = $a is equivalent to: $table[0] = $a; $table[$a,0] is legal syntax in perl, but obviously not what you want. > $table[0,$a] = $a; > } > } > > sub count > { > for ($a=0;$a<10;$a++) > { > for ($b=0;$b<10;$b++) > { > $table[$a,$b] = $table[$a,0] * $table[0,$b]; > } > } > } > > sub print > { > etc... > } > > init; > count; > print; > > ----------- > > The count function might not have been written this > way, but the init function sure looks like that, and > that is the function I'm having a problem with. It > doesn't work, but can't figure out why? It should give > a result like this: > 0123456789 > 1000000000 > 2000000000 > 3000000000 > 4000000000 > 5000000000 > 6000000000 > 7000000000 > 8000000000 > 9000000000 > > But instead I get > 9999999999 > 9000000000 > 9000000000 > 9000000000 ... etc > > So I wrote a procedure like this: > > sub test > { > for ($a=0;$a<10;$a++) > { > print $a; > $table[$a,0] = $a; > } > } > > .... and when I print the table, I get the following: > 123456789 (from the print $a line) 999999999 > 99999999 > 99999999 > 99999999 > .... etc... > > It's like it counts to 9, and then fills the table > with nines. But why? > > Anyway, can someone help? > > Thanks, Csaba > > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]