DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 18th November 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default Basic Perl arrays question

Hello,

What does this code snippet do?

Code:
my $attributes = ['objectClass'     => ['top', 'posixAccount', 'shadowAccount', 'inetOrgPerson'],
                  'loginShell'      => "/bin/bash"];
In my understanding, $attributes is a reference to an array. I though Perl arrays could not have indexes? What will the => do then?


Thanks!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
  #2   (View Single Post)  
Old 18th November 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by stukov View Post
In my understanding, $attributes is a reference to an array.
Close. $attributes is a reference to an anonymous hash.
Quote:
What will the => do then?
This is used to establish the (key,value) pairs.
Reply With Quote
  #3   (View Single Post)  
Old 18th November 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

The '=>' separates the hash key from the hash value. It is a more visual appealing replacement then the ',' which also can be used for this purpose
Code:
 %shell = (
         Theo  => '/bin/sh',
         Linus =>  '/bin/bash' ) ;

print "\nTheo's shell : $shell{'Theo'} " ;
print "\nLinus' shell : $shell{'Linus'} " ; 

$ perl hash-shell

Theo's shell : /bin/sh 
Linus' shell : /bin/bash
Another example but now using a comma
Code:
%os = ( 
        'Theo',  'OpenBSD',
        'Linus', 'Linux'
);

print "\nTheo's OS : $os{'Theo'} " ;
print "\nLinus' OS : $os{'Linus'} " ;

$perl hash-os

Theo's OS : OpenBSD 
Linus' OS : Linux
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #4   (View Single Post)  
Old 18th November 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default

Thanks for the replies. As I wrote a few scripts, I am getting familiar with the "hash" concept and Perl. But this is where it becomes confusing:

For the same hash I pasted in my first post, whenI print $attributes I get:

Code:
ARRAY(0x811a214)
Now, is a HASH an array itself?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
  #5   (View Single Post)  
Old 18th November 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

Quote:
Originally Posted by stukov View Post
Code:
my $attributes = ['objectClass'     => ['top', 'posixAccount', 'shadowAccount', 'inetOrgPerson'],
                  'loginShell'      => "/bin/bash"];
$attributes is an array reference.

the => operator is effectively a comma operator where the left hand object is taken as a double quoted string. in the given code its probably used to make the association clear (like J65nko said).

i am not sure why they haven't used a hash reference. possibly for a small performance gain?

Last edited by ephemera; 18th November 2008 at 07:12 PM.
Reply With Quote
  #6   (View Single Post)  
Old 18th November 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

Quote:
Originally Posted by stukov View Post
Now, is a HASH an array itself?
No.

hashes can not have duplicate keys.

also, the keys in a hash are not ordered.

For example,
Code:
%hash1 = @array1; @array2 = %hash1 ; %hash2 = @array2;
array2 will likely have a different ordering from array1, hence different. but, hash1 will be the same as hash2.


arrays have a small performance advantage. and neither is particularly space efficient in perl.

Last edited by ephemera; 18th November 2008 at 07:13 PM.
Reply With Quote
  #7   (View Single Post)  
Old 18th November 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default

Quote:
Originally Posted by ephemera View Post
i am not sure why they haven't used a hash reference. possibly for a small performance gain?
Maybe I can make the thing clearer: the array/hash I want to build will be given to the add method of the Perl::LDAP:

Code:
$ldapHandle->add($dn, attr => $attributes);
A function example looks like this:
Code:
$result = $ldap->add("uid=john,ou=People,dc=leapster,dc=org", 
                attr => [ 'cn' => 'John Smith',
                          'sn' => 'Smith',
                          'uid' => 'john',
                          'givenName' => 'John',
                          'homePhone' => '555-2020',
                          'mail' => 'john@domain.name',
                          'objectclass' => [ 'person', 'inetOrgPerson']
                        ]
           );
Am I expected to work with an array or an hash?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
  #8   (View Single Post)  
Old 18th November 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default

Quote:
Originally Posted by stukov View Post
Am I expected to work with an array or an hash?
Furhter testing indicates that :
Code:
foreach my $i (@$attributes)
{
    print $i . ENDL;
}
works but
Code:
foreach my $i (%$attributes)
{
    print $i . ENDL;
}
does not.

Now, how am I expected to provide a key-value pair when I need to work with an array?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
  #9   (View Single Post)  
Old 18th November 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

Quote:
Originally Posted by stukov View Post
Code:
$result = $ldap->add("uid=john,ou=People,dc=leapster,dc=org", 
                attr => [ 'cn' => 'John Smith',
                          'sn' => 'Smith',
                          'uid' => 'john',
                          'givenName' => 'John',
                          'homePhone' => '555-2020',
                          'mail' => 'john@domain.name',
                          'objectclass' => [ 'person', 'inetOrgPerson']
                        ]
           );
Am I expected to work with an array or an hash?
Use just the way it is in your example.

The format is given in the POD: http://search.cpan.org/~gbarr/perl-l...b/Net/LDAP.pod

Last edited by ephemera; 18th November 2008 at 07:40 PM.
Reply With Quote
Old 18th November 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

Quote:
Originally Posted by stukov View Post
Furhter testing indicates that :
Code:
foreach my $i (@$attributes)
{
    print $i . ENDL;
}
works but
Code:
foreach my $i (%$attributes)
{
    print $i . ENDL;
}
does not.

Now, how am I expected to provide a key-value pair when I need to work with an array?
like i said before, $attributes is an array reference and an array is not equivalent to a hash. you can't use an array reference as a hash reference or the other way around. (internally they have a different DS)

though, assigning an array to a hash and vice-versa is valid (with a caveat) , please see the example i gave earlier (#6).

Last edited by ephemera; 18th November 2008 at 07:34 PM.
Reply With Quote
Old 18th November 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default

Thanks for the explanations ephemera. Understood and script fixed.

Thanks!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
Old 18th November 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

Most of these things you can find in the man page perldsc (perl data sctructures cookbook) and perlreftut
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
Old 18th November 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default

Thanks J65nko, I'll take good notes.
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic networking fail. diw OpenBSD General 13 31st March 2009 09:29 AM
Basic sshd hardening anomie Guides 12 12th September 2008 03:39 AM
C 2D arrays jgroch Programming 16 2nd August 2008 01:54 AM
need some basic help on ifconfig daemon-dd FreeBSD General 4 29th July 2008 03:21 PM
ksh arrays mtx Programming 11 7th May 2008 10:00 AM


All times are GMT. The time now is 10:19 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick