Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Fellow Monks. I want to populate an array of referenced values; (I believe that is the correct term, please forgive me if it is not). My code attempt (chunk of code) is shown below:
#!/usr/bin/perl -w use strict; my @envars; $envars[0]->{value}="23"; $envars[0]->{description}="temperature in Celsius"; $envars[1]->{value}="56"; $envars[1]->{description}="relative humidity in \%"; $envars[2]->{value}="1008.1"; $envars[2]->{description}="pressure in millibars \%"; $envars[3]->{value}="4"; $envars[3]->{description}="dewpoint in degrees Celsius"; $envars[4]->{value}="344"; $envars[4]->{description}="total column ozone in Dobson Units";
Is there a more efficicent/streamlined way of doing this?
Perhaps a near one liner?
Any help/insight provided is greatly appreciated. Thanks!

Replies are listed 'Best First'.
Re: Populating an Array of Referenced Values
by duff (Parson) on Feb 02, 2004 at 20:50 UTC

    Depends on many things as to how "efficient" you can get . Here's one possibility:

    my @envars; push @envars,{ value => 23, description => 'temperature in Celsius' }, { value => 56, description => 'relative humidity in %' } +, { value => 1008.1, description => 'pressure in millibars +' }, { value => 4, description => 'dewpoint in degrees Celsiu +s' }, { value => 344, description => 'total column ozone in Do +bson Units' };

    You could do better by not repeating the text "value" and "description" several times.

    # say you've got 2 parallel arrays @values and @descriptions my @envars = map({ value => $values[$_], description => $descriptions[ +$_] }, 0..$#values);

    Or perhaps you really want to just push them onto the array rather than clobbering it each time...

    # Same assumption as before ... for my $i (0..$#values) { push @envars, { value => $values[$i], description => $descriptions[$ +i] }; }
Re: Populating an Array of Referenced Values
by Not_a_Number (Prior) on Feb 02, 2004 at 21:32 UTC

    Perhaps you are looking for an over-complicated data structure. But please ignore this if it's not applicable. What I would do with the data you have shown us is create a simple hash, with the keys equivalent to your 'description' and the values, er.. equivalent to your 'value's:

    use warnings; use strict; my @fields = ( 'temperature in Celsius', 'relative humidity in %', 'pressure in millibars %', 'dewpoint in degrees Celsius', 'total column ozone in Dobson Units', ); my @values = qw ( 23 56 1008.1 4 344 ); my %hash; @hash{@fields} = @values; # Print the whole thing: print "$_: $hash{$_}\n" for keys %hash; # Print one element: print $hash{'temperature in Celsius'};

    But perhaps you have a reason for making it more complicated?

    dave

Re: Populating an Array of Referenced Values
by Enlil (Parson) on Feb 02, 2004 at 20:51 UTC
Re: Populating an Array of Referenced Values
by Limbic~Region (Chancellor) on Feb 02, 2004 at 20:53 UTC
    Anonymous Monk,
    Each element of the array points to an anonymous hash containing exactly two keys. I doubt you would prefer this syntax any better:
    @{$envars[0]}{qw(value description)} = ('23' , 'temperature in Celsius +'); @{$envars[1]}{qw(value description)} = ('56' , 'relative humidity in % +');
    If you can, you might want to change to a hash of arrays:
    @{ $envars{value} } = qw(23 56 1008.1 4 344); @{ $envars{description} } = ( 'temperature in Celsius', 'relative humidity in %', 'pressure in millibars %', 'dwpoint in degrees Celsius', 'total column ozone in Dobson Units' );
    Cheers - L~R