Travis Hervey wrote:
How do you Create an array of a struct in perl?  Is this even possible
in perl?


Yes, but it's not called a struct in perl. It's typically called a hash reference.

`perldoc -q struct` is a good place to look first. After that I would look at `perldoc perlreftut`.

So far I have...

struct Carrier_Info => {

        name    => '$',

        abbrev  => '$'

};

...


Ahh, it looks like you are using Class::Struct. That's useful information [grin].


my @carriers = Carrier_Info->new();



Class::Struct provides you with a "new" constructor method, but it doesn't return you a list of struct objects. It returns you a struct object.



I have tried several different methods of loading data into the struct
but none have been successful so far.  I've tried:

$carriers{$x} = [$temp1, $temp2];

$carriers{$x}->name($temp1);

$carriers{$x}->abbrev($temp2);

$carriers[$x]->name($temp1);

$carriers[$x]->abbrev($temp2);

Where $x is an incrementing counter and the temp variables are my data I
am trying to load.



The Class::Struct documentation tells us that it provides us with accessor methods for each element of the struct object created by new, or that we can initialize the elements when we call new.

Putting that together we get something like this:

#!/usr/bin/perl

use warnings;
use strict;

use Class::Struct;

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

# make a new struct object
my $carrier = Carrier_Info->new();

# assign data to the elements
$carrier->name( "Bob Ross" );
$carrier->abbrev( "BR" );

# access data in the elements
my $carrier_name   = $carrier->name();
my $carrier_abbrev = $carrier->abbrev();



Hope that helps a bit.

-- Douglas Hunter

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


Reply via email to