For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > dereferencing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author dereferencing
Christopher Spears

2005-08-24, 9:55 pm

I'm trying to brush up on my Perl by learning object
oriented Perl! This may not be the best way to brush
up on this subject, but I am sure that I will learn a
lot! Here is a script:

#!/usr/bin/perl -w
use strict;

my @array = [1, 2, ['a','b','c','d']];

my $arrayref = \@array;

my $arraySize = scalar $arrayref->@array;

print $arraySize."\n";

I received the following error message:
Array found where operator expected at ./anonymousData
line 8, at end of line
(Missing operator before ?)
syntax error at ./anonymousData line 8, near
"->@array"
Execution of ./anonymousData aborted due to
compilation errors.

What am I doing wrong? Out of curiousity, if the
array was anonymous, then how can I find it's size.

$arrayref = [1, 2, ['a', 'b', 'c', 'd']];
How can I find the size of this array with dereferencing?
John W. Krahn

2005-08-25, 3:55 am

Christopher Spears wrote:
> I'm trying to brush up on my Perl by learning object
> oriented Perl! This may not be the best way to brush
> up on this subject, but I am sure that I will learn a
> lot! Here is a script:
>
> #!/usr/bin/perl -w
> use strict;
>
> my @array = [1, 2, ['a','b','c','d']];


You are assigning to $array[0] a reference to an anonymous array.


> my $arrayref = \@array;


You are assigning to $arrayref a reference to @array.


> my $arraySize = scalar $arrayref->@array;


$arrayref is a reference to @array so:

my $arraySize = @$arrayref;

will give you the size of @array and:

my $arraySize = @{ $arrayref->[0] };

will give you the size of the anonymous array in $array[0] and:

my $arraySize = @{ $arrayref->[0][2] };

will give you the size of the anonymous array in $array[0][2].


> print $arraySize."\n";
>
> I received the following error message:
> Array found where operator expected at ./anonymousData
> line 8, at end of line
> (Missing operator before ?)
> syntax error at ./anonymousData line 8, near
> "->@array"
> Execution of ./anonymousData aborted due to
> compilation errors.
>
> What am I doing wrong? Out of curiousity, if the
> array was anonymous, then how can I find it's size.
>
> $arrayref = [1, 2, ['a', 'b', 'c', 'd']];
> How can I find the size of this array with dereferencing?


There are a few Perl documents on data structures and references:

perldoc perldata
perldoc perldsc
perldoc perllol
perldoc perlreftut
perldoc perlref



John
--
use Perl;
program
fulfillment
Edward WIJAYA

2005-08-25, 3:55 am

On Thu, 25 Aug 2005 10:31:18 +0800, Christopher Spears
<cspears2002@yahoo.com> wrote:

> I'm trying to brush up on my Perl by learning object
> oriented Perl! This may not be the best way to brush
> up on this subject, but I am sure that I will learn a
> lot! Here is a script:
>
> #!/usr/bin/perl -w
> use strict;
>
> my @array = [1, 2, ['a','b','c','d']];


I think you have unnecessarily used square [] bracket.
You want this instead:

my @array = (1, 2, ['a','b','c','d']);


As for the overall approach I don't see why you
need to go via reference to find the array size.
It's like overkilling.

Perhaps this is what you want?


$ perl -MData::Dumper -e '
@array = (1,2,['a','b','c','d']);
$aref = \@array; $asize = scalar(@$aref);
print "Array Size: $asize\n"; print Dumper $aref;'

Prints:
Array Size: 3
$VAR1 = [
1,
2,
[
'a',
'b',
'c',
'd'
]
];


--
Regards,
Edward WIJAYA
SINGAPORE
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com