| Store | Cart

Re: it's probably just me

From: nonlin <non...@erols.com>
Wed, 28 Oct 2015 13:20:46 -0500
Hi again,

2 points from below.

It's not the print stamen defining new variables, it's just them showing up
anyware in the code that makes them. How about this as an example:
my $car = 1;
my $ontheroad = 12;
$vehicles = $cars = $car * $ontheroad + $brockdown;

Purl would have made $verhicles, $cars from the equation and $brockdown
would have also been made and thus added 0 to the equation when you would
have expected an error. One thing I love about Purl is that it like the
energizer bunny. It keeps going and going and going.

Second
I like using the var and my stamen to define my variables.
my is local and you can add data to it as it's being define. Hear one you
sound know
sub howmanycars (
my $car = $_[0];
so if you call the subroutine $howmanycars (10), guess what $car has in it?
Yup 10
But the define command I use the most is var
var is global, you can define a hundred variables with just one var command,
and you can define single variable, arrays, and hashes, plus you can put
tons of data in them as you define them.
So I would recommend Var and My for your work. I don't know Our but it
probably just a good. too

Stefan


----- Original Message ----- 
From: "John DePasquale" <joh...@johndepasquale.net>
To: "'Francisco Zarabozo'" <fzar...@hotmail.com>;
<acti...@listserv.ActiveState.com>
Sent: Wednesday, October 28, 2015 11:17 AM
Subject: RE: it's probably just me


> 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:fzar...@hotmail.com]> Sent: Wednesday, October 28, 2015 11:48 AM> To: John DePasquale; acti...@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> Acti...@listserv.ActiveState.com> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
ActivePerl mailing list
Acti...@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Recent Messages in this Thread
nonlin Oct 28, 2015 06:20 pm