?
Solved

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

Posted on 2010-11-29
4
Medium Priority
?
671 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 2000 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 real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

777 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