Re: Confusion on @array vs $array[] vs $array

2002-12-19 Thread kevin christopher
Hope this doesn't further belabor the issue, but just to put my 
two cents in, Perl syntactic rules for prefixing "$", "@", "%" are 
very consistent, IMHO: You just need to keep in mind the types of 
the values/data types ultimately being expressed, and it should 
become clearer. "$" always prefixes scalars or references, "@" 
always prefixes lists, and "%" always prefixes associative arrays 
(a.k.a hashes).

@array is a list
$array[n] is a scalar/reference 
%hash is a hash
$hash{'key'} is a scalar/reference
@$ref dereferences a reference to an array, accessing the array In 
this case, "print $ref;" would give you a reference scalar, 
something like "ARRAY(0x4E3FB1C)"; "print @$ref;" would output the 
actual array list.

Also try @hash{keys %hash}, which returns a list of the hash's 
values.

In all these cases, the prefixed "sigils" consistently describe 
the data type ultimately expressed, rather than the data type of 
the originating variable's data structure.

Kevin



-- Original Message --
From: "Octavian Rasnita" <[EMAIL PROTECTED]>
Date:  Wed, 18 Dec 2002 20:13:58 +0200

>@var is an array
>$var is a scalar
>
>$var[0] is also a scalar even though is an array element.
>@var[0] is an array which contains more array elements, but in 
this case it
>contains just a single element.
>
>To create an array slice with more elements, you'll need 
something like
>@var[0 .. n]
>
>You need to always use $ for scalars and @ for arrays.
>
>
>Teddy,
>Teddy's Center: http://teddy.fcc.ro/
>Email: [EMAIL PROTECTED]
>
>- Original Message -
>From: "Lance Murray" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Sent: Wednesday, December 18, 2002 7:02 PM
>Subject: Confusion on @array vs $array[] vs $array
>
>
>Hello:
>
>I have been fairly confused on the intermixing of array naming 
standards.
>If I
>understand correctly, the @array syntaxt is used to refer to the 
whole
>array,
>while $array[n] is refered to specific elements.
>
>In my thinking, it would have been less confusing to use @array
[n] to
>address
>specific elements, but Perl complaines bitterly when warnings 
&/or strict is
>enabled.
>
>Is the rational for the "$"array[] character for 
identifying "@"array
>elements
>due to the fact that the $ character has special meaning (denotes 
variable
>or
>element) within double quoted text, while the @ sign has no 
special meanign
>within double quotes.
>
>Anyway, perhaps one of you syntactical thought police could give 
me some
>insights to the rational.  I find it very confusing that the $ 
and @
>characters
>are supposed to be used interchangeably to denote arrays, 
particularyly when
>I'm
>trying to fathom the relation to other issues such as scalar or 
list
>context.
>As I recall, hashes arso use % and @ interchangeably.
>
>Thanks,
>
>Lance
>
>--
--
>--
>#!/usr/bin/perl
>print "Lance Murray, Information and Communications Systems\n",  #
>department
>"The Church of Jesus Christ of Latter-Day Saints\n",  # employer 
and
>religion
>"(801) 240-6583, murraylk\@ldschurch.org\n\n",  # work phone 
number and
>email
>"\"Better to do a little well than a lot poorly (Socrates)\"\n"; #
>philosophy
>@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona 
tsuJ";sub
>p{
>@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map
{$P=$P[$f^or
>d
>($p{$_})&6];$p{$_}=/
>^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
>close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep
>rand(2)if/\S/;print
>
>--
--
>--
>This message may contain confidential information, and is 
intended only for
>the use of the individual(s) to whom it is addressed.
>
>
>==
==
>==
>
>
>
>--
--
>
>
>
>--
>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]
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




beginners-cgi@perl.org

2002-06-07 Thread kevin christopher

Janek: 

Thank you for explaining the difference between calling 
subroutines with vs. without the ampersand.

I'm glad I'm on the beginners list.

Sincerely,

Kevin Christopher


-- Original Message --
From: Camilo Gonzalez <[EMAIL PROTECTED]>
Date:  Thu, 6 Jun 2002 08:23:33 -0500 

>Janek,
>
>Wouldn't it print:
>foo:
>&foo:A B C
>
>Also, I believe that you must declare the subroutine before you 
are allowed
>to reference it without the &. Am I right about that?
>
>-Original Message-
>From: Janek Schleicher [mailto:[EMAIL PROTECTED]]
>Sent: Wednesday, June 05, 2002 5:10 AM
>To: [EMAIL PROTECTED]
>Subject: Re: subroutine or &subroutine
>
>
>Kevin Christopher wrote at Wed, 05 Jun 2002 04:58:38 +0200:
>
>> Yes, you can call subroutines either way, with or without 
the "&". The
>only case when the
>> subroutine must be prefixed with an ampersand is, I believe, 
when you're
>assigning a reference
>> variable, eg:
>> 
>> $reference_x = \&subroutine_y;
>> 
>> But that's another story.
>> 
>
>Oh, I'm afraid that's not the truth :-)
>
>&subroutine without any arguments calls the subroutine with the 
implicit @_
>array,
>while subroutine only calls subroutine() without any argument.
>
>Look at this snippet:
>@_ = qw(A B C);
>
>print 'foo:'; foo; print "\n";
>print '&foo:'; &foo; print "\n";
>
>sub foo {
>   print @_;
>}
>
>It prints:
>foo:
>&foo:ABC
>
>
>Greetings,
>Janek
>
>-- 
>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]
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




beginners-cgi@perl.org

2002-06-05 Thread kevin christopher

Yes, you can call subroutines either way, with or without the "&".
The only case when the subroutine must be prefixed with an
ampersand is, I believe, when you're assigning a reference
variable, eg:

$reference_x = \&subroutine_y;

But that's another story.

Kevin


-- Original Message --
From: "Octavian Rasnita" <[EMAIL PROTECTED]>
Date:  Tue, 4 Jun 2002 21:06:19 +0300

>Hi all,
>
>I've seen some subroutines are ran without the & sign in front of the
>subroutine name, like:
>
>subroutine_name;
>instead of
>&subroutine_name;
>
>Is it the same thing or there is a difference?
>
>Thank you.
>
>
>Teddy,
>[EMAIL PROTECTED]
>
>
>
>-- 
>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]