Narthring wrote:
On Sep 18, 9:55 am, [EMAIL PROTECTED] (Travis
Hervey) wrote:
How do you Create an array of a struct in perl? Is this even possible
in perl?
So far I have...
struct Carrier_Info => {
name => '$',
abbrev => '$'
};
...
my @carriers = Carrier_Info->new();
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.
Thanks,
Travis Hervey
One solution would be to use an array of hashes to emulate a struct.
use strict;
use warnings;
use Data::Dumper; #Import to show what the data structure looks like.
#Create an array reference
my $Carrier_Info = (); # the 'struct'; actually will contain an array of
hashes
Why use a scalar instead of an array? The original poster used an array: let's
assume
he wants one.
And why initialise it with (), which will simply leave it undefined? Did you
mean
my $Carrier_Info = [];
which is unnecessary but makes more sense?
#Load some data
$Carrier_Info->[0] = {'name', 'Federal Express', 'abbrev', 'FedEx'};
$Carrier_Info->[1] = {'name', 'United Parcel Services', 'abbrev', 'UPS' };
Use the Perl => operator to visually pair up hash keys and values, and to avoid
having to quote bareword keys:
$Carrier_Info->[0] = {
name => 'Federal Express',
abbrev => 'FedEx',
};
$Carrier_Info->[1] = {
name => 'United Parcel Services',
abbrev => 'UPS',
};
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/