×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

how to pull name value pairs from an array variable...

how to pull name value pairs from an array variable...

how to pull name value pairs from an array variable...

(OP)
i have this variable to work with, below is an example (but correct syntax) or the information carried by the variable.

CODE

$variable1 = [ { 'RaceTime' => '15', 'RaceNumber' => '15126', 'RaceID' => '84ee45s89' }, { 'RaceTime' => '15', 'RaceNumber' => '15126', 'RaceID' => '84ee45s89' }, { 'RaceTime' => '10', 'RaceNumber' => '17894', 'RaceID' => '1ee547987' } ]; 

i want to cycle through each group. i cannot change the form of the variable, this is what i have to work with, including the '[]' and '{}'...


any ideas?

- g

RE: how to pull name value pairs from an array variable...

Hi

Something like this ?

CODE --> Perl

$variable1 = [
  { 'RaceTime' => '15', 'RaceNumber' => '15126', 'RaceID' => '84ee45s89' },
  { 'RaceTime' => '15', 'RaceNumber' => '15126', 'RaceID' => '84ee45s89' },
  { 'RaceTime' => '10', 'RaceNumber' => '17894', 'RaceID' => '1ee547987' }
];

my $nr = 0;
foreach my $item (@{$variable1}) {
  print ' - item ', $nr++, " :\n";
  foreach my $key (keys $item) {
    print '   - ', $key, ' = ', $item->{$key}, "\n";
  }
} 

CODE --> output

- item 0 :
   - RaceTime = 15
   - RaceID = 84ee45s89
   - RaceNumber = 15126
 - item 1 :
   - RaceTime = 15
   - RaceID = 84ee45s89
   - RaceNumber = 15126
 - item 2 :
   - RaceTime = 10
   - RaceID = 1ee547987
   - RaceNumber = 17894 

Feherke.
http://feherke.github.com/

RE: how to pull name value pairs from an array variable...

(OP)
Yes. Exactly.

I actually came up with this last night:

CODE

foreach $v (@{$variable1}) {

 $rtime=$v->{RaceTime};
 $rnumber =$v->{RaceNumber};
 $rid=$v->{RaceID};

#html code here

} 

it works, but i do like your example, as it allows for new keyvalue pairs without having to update code.

curiously, that @{$variable1} part turns the information stored in the variable to an array, right? or does it go deeper than that, since originally the variable contained array elements but they were further enclosed in the []...? i remember i realized that although the information stored in $variable1 looked like an array, when i tried counting the elements in it, the result kept coming back as 1 instead of the actual number of items.

thanks.

- g

RE: how to pull name value pairs from an array variable...

Hi,

@spewn:

You are describing "references". The Perl docs explain references well ( http://perldoc.perl.org/perlref.html ).

With regards to counting, i'm interested in your approach which resulted in a value of 1 being returned. A reference in scalar context is simply a string representing its storage location in memory. In array context, the correct count should have been returned.



@feherke:

CODE

foreach my $key (keys %{$item}) {
	print '   - ', $key, ' = ', $item->{$key}, "\n";
} 

I think you meant to dereference $item. I also personally prefer to retrieve both the key and the value inside a while condition (atleast in this case):

CODE

while (my ($key, $value) = each ( %$hash ) )
{
	print '   - ', $key, ' = ', $value, "\n";
} 

RE: how to pull name value pairs from an array variable...

Hi

Quote (Zhris)

CODE

foreach my $key (keys %{$item}) {
	print '   - ', $key, ' = ', $item->{$key}, "\n";
} 
I think you meant to dereference $item.
Nope. I just forgot to mention that it is not backward ( maybe neither forward ? ) compatible :

Quote (man perlfunc)

Starting with Perl 5.14, keys can take a scalar EXPR, which must contain a reference to an unblessed hash or array. The argument will be dereferenced automatically. This aspect of keys is considered highly experimental. The exact behaviour may change in a future version of Perl.
Thanks for throwing this in attention.

Feherke.
http://feherke.github.com/

RE: how to pull name value pairs from an array variable...

@feherke,

Thanks alot for the information regarding keys.

Chris

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close