> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf
> Of Olivier CLERE
> Sent: Thursday, May 18, 2000 11:23 AM
> To: Perl-Win32-Users Mailing List
> Subject: Need some tips on pointers
>
>
> Here is my Perl code:
>
> %berval= (
> bv_len=>3,
> bv_val=>"123",
> );
>
> $ldapctrl = {
> ldctl_oid=>"2.16.840.1.113730.3.4.2",
> ldctl_iscritical=>1,
> ldctl_value=> %berval,
> };
>
> I would like to define the Perl equivalent of
> what we would call in C:
> "an array of pointers to the structure ldapctrl",
All array elements are scalars, you can't define
any specific type for an array. A scalar is simply
a single value of any type. The equivalent of a
pointer in C is a reference, which is a scalar
value. The reference can refer to just about anything,
both named scalars and structures as well as anonymous
data.
Your scalar variable $ldapctrl is actually a reference to
a hash.
#define and init to empty
my @arr = ();
Now just push your reference on to the array
push @arr, $ldapctrl;
Be aware of the fact that you have 2 references to the same
data. You could have pushed the reference directly
onto the array. Notice how I added a \ in front of the
%berval hash, this is like C's adressoperator, &.
#add an anonymous reference
push @arr, { ldctl_oid => "2.16.840.1.113730.3.4.2",
ldctl_iscritical => 1,
ldctl_value => \%berval
}
Some expressions to access the data, first arrow notation
$arr[0]->{ldctl_oid}
$arr[0]->{ldctl_value}->{bv_val}
and second, by adding a type character at the left to
"dereference" the "pointer"
${$arr[0]}{ldctl_oid}
${$arr[0]->{ldctl_value}}{bv_val}
${${$arr[0]}{ldctl_value}}{bv_val} # same as previous line
the similarities with C are probably not coincidental :) Notice the
extra brackets, which are needed to get the precedence right.
Being a C programmer you might instinctively iterate the
array in this manner
for($i = 0; $i < $#arr; $i++)
{
# do something with $arr[$i]
}
This is valid perl but not idiomatic. Try this instead
for (@arr)
{
# use $_ instead of $arr[n]
}
regards,
--
robert friberg, ensofus ab
+46(0)708 98 57 01
---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
[EMAIL PROTECTED]
For non-automated Mailing List support, send email to
[EMAIL PROTECTED]