Solved

Class::Struct array of structs possible? (Perl)

Posted on 2010-11-29
4
655 Views
Last Modified: 2013-12-25
Experts,

I'm using the Perl Class::Struct module and would like to find out if it's possible to create a struct that has an array member which should then contain other arrays or hash arrays.

Is that possible and how would I push and pull arrays to the array member?

This is what I had in mind:

my %body_element = ();
$body_element{'first'} = 1;
$body_element{'second'} = 2;

push @{ $my_message->message_body }, %body_element ;

struct Message => {
    message_header => '@',
    message_body => '@',
};
0
Comment
Question by:php-newbie
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34234530
Yes, that should work:
use Class::Struct;

 

struct Message => {

    message_header => '@',

    message_body => '@',

};



my $my_message = Message->new();



my %body_element = ();

$body_element{'first'} = 1;

$body_element{'second'} = 2;



push @{ $my_message->message_body }, %body_element ;



print "$_\n" for (@{$my_message->message_body});

Open in new window

0
 

Author Comment

by:php-newbie
ID: 34235436
Unfortunately using your solution when I dump the struct I see that the hash array is stored as elements of the regular array 'body_element':

'Message::body_element' => [
                                   'first',
                                   1,
                                   'second',
                                   '2'
                                 ]

0
 

Author Comment

by:php-newbie
ID: 34235489
One possible workaround I found was to define a second struct that can be stored in the array within the first struct. That seems to work.

For example:
push @{ $my_message->body_content }, Carrier_Info->new(name => "Paul Lalli", abbrev =>
"PL");
my $carrier_info = pop(@{$my_message->body_content});
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34239212
Sorry, you should be adding a reference to the hash (\%body_element) as below:


use Class::Struct;

 

struct Message => {

    message_header => '@',

    message_body => '@',

};



my $my_message = Message->new();



my %body_element;

$body_element{'first'} = 1;

$body_element{'second'} = 2;



push @{ $my_message->message_body }, \%body_element ;



for $e (@{$my_message->message_body})

{

	print "$_=${$e}{$_}\n" for keys%$e;

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now