[ale] Converting C data structures to Perl

David Corbin dcorbin at machturtle.com
Mon Apr 21 21:41:58 EDT 2003


You can definately do complex data structures in Perl.  Here's the version that works.  See comments below.


%device1 = (
    ip => "192.168.2.231",
    name => "bubba",
    pass => "password",
);
%device2 = (
    ip => "192.168.2.231",
    name => "bubba",
    pass => "password",
);
@unit = (\%device1, \%device2);

print $unit[0]->{name};

1) when you say %device = { x=>'y' }, you're attempt to assign  
reference to a hash to a hash. 
2) when you say (%device1) that is no different than saying %device1.  
You want a reference to device1, hence the backslash.
3) you had array instead of unit.
4) Now that you have an array of hash references, you need to use -> to 
dereferrence the data.

References really are the key to doing complex data structures.

Chris Fowler wrote:

>Maybe I'm doing something wrong with it,  Because even if I simplify
>down to an array of hashes like this,
>
>%device1 = {
>    ip => "192.168.2.231",
>    name => "bubba",
>    pass => "password",
>};
>%device2 = {
>    ip => "192.168.2.231",
>    name => "bubba",
>    pass => "password",
>};
>@unit = (%device1, %device2);
>
>print $array[0]{name};
>
>I do not get a name displayed.
>
>
>On Mon, 2003-04-21 at 20:57, Fletch wrote:
>  
>
>>>>>>>"Christopher" == Christopher Fowler <cfowler at outpostsentinel.com> writes:
>>>>>>>              
>>>>>>>
>>    Christopher> Hello,
>>
>>    Christopher> I've got my data parsed and having a issue try to
>>    Christopher> make it accessible throughout the program.
>>
>>Erm, if it's parsed in perl then why are you talking about C
>>structures . . . /me is confuzlled . . .
>>
>>    Christopher> Here is a refernce in C
>>
>>    Christopher> struct device { char *name; char *user; char *pass; }
>>
>>    Christopher> struct unit { char ip; struct device **d; };
>>
>>    Christopher> struct unit **unit; // Array of units
>>
>>If you really do have it as C pointery-fun data you could use unpack
>>and some deep wizardry, but for something as involved as this it'd
>>probably be much easier to write your own API using either XS or
>>Inline::C.  Start with perldoc perlxstut and perldoc perlxs for the
>>former, or look for the Inline documentation on search.cpan.org for
>>the later (which might be easier to pick up).
>>
>>
>>-- 
>>Fletch                | "If you find my answers frightening,       __`'/|
>>fletch at phydeaux.org   |  Vincent, you should cease askin'          \ o.O'
>>770 294-0820 (m)      |  scary questions." -- Jules                =(___)=
>>                      |                                               U
>>_______________________________________________
>>Ale mailing list
>>Ale at ale.org
>>http://www.ale.org/mailman/listinfo/ale
>>    
>>
>
>
>_______________________________________________
>Ale mailing list
>Ale at ale.org
>http://www.ale.org/mailman/listinfo/ale
>
>  
>


_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list