Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

773 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