Ah ha!  I understand now!  It took seeing it from several different
angles but I get it now.  I was assuming that I could make an array of
the structs.  But it seems in perl that in order to do so you make an
instance of the struct within the element of a regular array since
individual array elements are scalars!  I think part of my own confusion
came from trying to mash together pieced together information from the
internet on making structs in perl.  Some documentation showing how to
do it from the class Class::Struct while others explaining how to do
with hashes.  Thanks everyone for your aid in this!

Travis Hervey


-----Original Message-----
From: Paul Lalli [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 18, 2007 12:33 PM
To: beginners@perl.org
Subject: Re: How do you Create an array of a struct?

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/



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


Reply via email to