On Sep 18, 10:55 am, [EMAIL PROTECTED] (Travis
Hervey) wrote:
> How do you Create an array of a struct in perl?

You have to be more careful in your phrasing of your question.  You're
using the Class::Struct module without telling anyone you're using the
Class::Struct module, so people are assuming you just mean the generic
CompSci term 'struct'.  That's why John and Narthring told you to use
a hash.

> Is this even possible in perl?

Yes,

> So far I have...
>
> struct Carrier_Info => {
>         name    => '$',
>         abbrev  => '$'
> };

So far, so good.

> my @carriers = Carrier_Info->new();

No.  the new() method that Class::Struct created for you returns one
single Carrier_Info struct.  It does not make @carriers into an array
of structs.  You define the array just as you would define any other
Perl array.  Then you put into it whatever you want to put into it.
Here's a short-but-complete script that demonstrates a few different
techniques.  Put a "last;" statement in whichever two methods' blocks
you don't want to test.

#!/usr/bin/perl
use strict;
use warnings;
use Class::Struct;

struct Carrier_Info => {
          name => '$',
          abbrev => '$',
};

my @carriers;
{
    #method 1
    $carriers[0] = Carrier_Info->new();
    $carriers[0]->name("Paul Lalli");
    $carriers[0]->abbrev("PL");

    $carriers[1] = Carrier_Info->new();
    $carriers[1]->name("Travis Hervey");
    $carriers[1]->abbrev("TH");
}
{
    #method 2
    push @carriers, Carrier_Info->new(name => "Paul Lalli", abbrev =>
"PL");
    push @carriers, Carrier_Info->new(name => "Travis Hervey", abbrev
=> "TH");
}
{
    #method 3
    @carriers = map { Carrier_Info->new() } 1..2;
    $carriers[0]->name("Paul Lalli");
    $carriers[0]->abbrev("PL");
    $carriers[1]->name("Travis Hervey");
    $carriers[1]->abbrev("TH");
}
use Data::Dumper;
print Dumper([EMAIL PROTECTED]);
__END__

Hope that helps,
Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to