Solved

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

Posted on 2010-11-29
4
666 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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