On 29 May 2007 13:04:42 -0700, Paul Lalli <[EMAIL PROTECTED]> wrote:
On May 29, 3:21 pm, [EMAIL PROTECTED] (Dr.Ruud) wrote:
> Brian schreef:
>
> > Changing @ to $ is confusing...
>
> Huh? @ means array, $ means scalar; there is nothing to change.

Presumably, he meant that
@array
identifies the entire array, while
$array[0]
identifies the first element of the array, thus "changing" the @ to a
$ to access a single element.

The mnemonic device is not "@ for array, $ for scalar", but rather:
$ is for single elements, be they single elments of arrays or hashes,
or actual scalar variables.
@ is for lists of data, whether entire arrays, slices of arrays, or
slices of hashes
% is for an entire hash.

Paul Lalli

Of course, that is changing in Perl 6.  Scalars (including references)
will always be prefixed with $, arrays will always be prefixed with @,
and hashes will always be prefixed with %.  Array slices and indexing
will be preformed thusly:

pugs> my @a = 1 .. 5 #the same in Perl 5
(1, 2, 3, 4, 5)
pugs> @a[2..4] #the same in Perl 5
(3, 4, 5)
pugs> @a[1] #$a[1] in Perl 5
2
pugs> my $aref = @a; #my $aref = [EMAIL PROTECTED] in Perl 5
pugs> $aref[1] #$aref->[1] in Perl 5
2
pugs> $aref[1 .. 3] [EMAIL PROTECTED] in Perl 5
(2, 3, 4)

And hash slices and indexing will be done like this

pugs> my %h = zip <one two three four>, [1 .. 4] #much more
complicated in Perl 5
(("four", 4), ("one", 1), ("three", 3), ("two", 2))
pugs> %h<one two> #%h{qw<one two>} in Perl 5
(1, 2)
pugs> %h<three> #$h{three} in Perl 5
3
pugs> %h{four} #oops, this is an error in Perl 6, {} does not
stringify barewords
*** No such subroutine: "&four"
   at <interactive> line 1, column 4-8
pugs> %h{'four'} #$h{four} in Perl 5 (note the necessary '' in Perl 6,
use <> instead)
4
pugs> my $href = %h #my $href = \%h; in Perl 5
(("four", 4), ("one", 1), ("three", 3), ("two", 2))
pugs> $href<one two> #%{$href}{qw<one two>} in Perl 5
(1, 2)
pugs> $href<three> #$href->{three} in Perl 5
3
pugs> $href{'four'} #$href->{four} in Perl 5, again Perl 6 needs the quotes
4

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to