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

    #Load some data
$Carrier_Info->[0] = {'name', 'Federal Express',        'abbrev',
'FedEx'};
$Carrier_Info->[1] = {'name', 'United Parcel Services', 'abbrev',
'UPS'  };

    #Print the hash of arrays
print Dumper($Carrier_Info);                #show everyting in the
'$Carrier_Info' data structure

    #Print a single data element
print "Abbreviation for carrier 1:  $Carrier_Info->[1]->{abbrev}\n";


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


Reply via email to