Autovivication. Interesting concept. I didn't realize that such a reference in a print statement was causing new array rows to be created. Explains a great deal about the vexing bugs I've been wrestling with today. Much thanks for the information, Francisco. Regarding LOCAL vs.OUR, I've been using LOCAL for a long time to declare variables that I need visible throughout a program, seems to work very well. you would use OUR to accomplish the same need?
John DePasquale Chief Executive Officer Paradigm Consulting "Proudly presenting the Vinopedia System" www.vinopedia.us 49 Dalby Street Newton, MA 02458 Mobile: 617-610-2424 Fax: 617-600-7326 -----Original Message----- From: Francisco Zarabozo [mailto:fzarab...@hotmail.com] Sent: Wednesday, October 28, 2015 11:48 AM To: John DePasquale; activeperl@listserv.ActiveState.com Subject: Re: it's probably just me > From: John DePasquale, Sent: Wednesday, October 28, 2015 9:13 AM >The following snippet prints a 1 > local @aList; > push @aList, ['first','second','third']; > my $nCount = @aList; > print "count: $nCount"; Yes, of course it does. After your push, @aList contains a single element, which is an arrayref. So $aList[0] = ['first', 'second', 'third'], which is a single element. > BUT, if I add one seemingly irrelevant line ( the second one below ), > something crazy happens: > local @aList; > print "non-existent value: $aList[0][0]\n"; > push @aList, ['first','second','third']; > my $nCount = @aList; > print "count: $nCount"; Not crazy at all. Perl has this thing called "autovivification", where it creates an element on the fly when requested by your own code, like your second line is creating not only the first element of @aList, but it's also creating it as an arrayref by specifying its own first element too. >instead of printing a 1, the last line prints a 2 After that autovivification, you are then pushing again a hashref containing on its own 3 elements, but it is a single element by itself to @aList. That's why it prints a 2. So, first things first. Always use strict and warnings. That would've have you noticed that "local" is not the keyword you want to use. That keyword is not for declaring a variable. Instead, use "my" or "our". Here's an example: use strict; use warnings; my @aList; push @aList, (1, 2, 3); # Notice I used parenthesis, not brackets. my $nCount = @aList; print "count: $nCount\n"; # This will print 3 # Now, I can push an arrayref by using brackets. An arrayref is a single element. push @aList, [4, 5, 6]; print "Number of aList elements: ".@aList."\n"; # This will print 4 # Now I can push more elements by using parenthesis: push @aList, (7 .. 10); print "Number of aList elements: ".@aList."\n"; # This will print 8 # @aList now contains the following structure of 8 elements: @aList = ( 1, 2, 3, [4, 5, 6], 7, 8, 9, 10, ); # And I can access the elements on that arrayhref by doing this: print "Arrayref elements are $aList[3][0], $aList[3][1] and $aList[3][2]\n"; HTH Francisco Zarabozo _______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs