Solved

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

Posted on 2010-11-29
4
653 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

743 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

11 Experts available now in Live!

Get 1:1 Help Now