CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Associative array of Class::Struct objects?

 



bigteks
Novice

Nov 5, 2015, 3:09 PM

Post #1 of 5 (1576 views)
Associative array of Class::Struct objects? Can't Post

Trying to make an associative array of Class::Struct objects.

The error message is:
Can't call method "id" on an undefined value at ./test.pl line 13.


Code
#!/usr/local/bin/perl 

use Class::Struct;

struct( Array => {
id => '$',
container => '$',
serial => '$'
});

%a = Array->new();

$a{'first'}->id("id");
$a{'first'}->container("con");
$a{'first'}->serial("ser");


Am I just leaving something out or am I trying to do something you can't do? I tried this with a standard array instead of associative array and it worked so wondering if I just can't do this with an associative array or do I need another initialization step in there for associatives that is not needed for regular arrays?


FishMonger
Veteran / Moderator

Nov 6, 2015, 6:43 AM

Post #2 of 5 (1552 views)
Re: [bigteks] Associative array of Class::Struct objects? [In reply to] Can't Post

You should ALWAYS use the strict and warnings pragmas. They would have pointed out your mistakes.


Code
%a = Array->new();

The constructor creates/returns an object, which is a reference not a plain hash. Change it to:

Code
my $obj = Array->new();



Code
$a{'first'}->id("id");

Where did {'first'} come from? It's not defined in the struct.


Code
#!/usr/local/bin/perl 

use strict;
use warnings;
use Class::Struct;
use Data::Dumper;

struct( Array => {
id => '$',
container => '$',
serial => '$'
});

my $obj = Array->new();

$obj->id("id");
$obj->container("con");
$obj->serial("ser");

print Dumper $obj;



bigteks
Novice

Nov 6, 2015, 7:31 AM

Post #3 of 5 (1549 views)
Re: [FishMonger] Associative array of Class::Struct objects? [In reply to] Can't Post

Right - thanks - I do already know how to do what you outlined here, the problem is it doesn't give me what I want which is an array of hashes each of which points to a unique instance of the named struct object.

For example this works:

Code
#!/usr/local/bin/perl 

use Class::Struct;

struct( Array => {
id=>'$',
container=>'$',
serial=>'$'
});

@a=Array->new();

$a[0]->id("id0");


This seems to not be correct even though it works on $a[0]. How would I correctly populate $a[0..2] with "id0" - "id2",

and how would I populate $a{'first'} - $a{'third'} similarly?


FishMonger
Veteran / Moderator

Nov 6, 2015, 7:54 AM

Post #4 of 5 (1546 views)
Re: [bigteks] Associative array of Class::Struct objects? [In reply to] Can't Post

Please ALWAYS use the strict and warnings pragmas and proper indenation. If you don't, I will stop helping you.

As I've already said, the new() constructor returns reference to a single blessed object, not a hash or an array. If you want to populate an array, then use a loop.


Code
!/usr/local/bin/perl 

use strict;
use warnings;
use Class::Struct;
use Data::Dumper;

struct( Array => {
id => '$',
container => '$',
serial => '$'
});


my @a;
for (1..3) {
push @a, Array->new();
}

$a[0]->id("id0");
$a[1]->id("id1");
$a[2]->id("id2");

print Dumper \@a;


my %hash;
for ( qw(first second third) ) {
$hash{$_} = Array->new();
}

print Dumper \%hash;

$hash{second}->id(2);
print Dumper $hash{second}->id;



bigteks
Novice

Nov 6, 2015, 8:07 AM

Post #5 of 5 (1543 views)
Re: [FishMonger] Associative array of Class::Struct objects? [In reply to] Can't Post

Thanks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives