Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
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
 
Hi

Something like this ?
Perl:
[navy]$variable1[/navy] [teal]=[/teal] [teal][[/teal]
  [teal]{[/teal] [green][i]'RaceTime'[/i][/green] [teal]=>[/teal] [green][i]'15'[/i][/green][teal],[/teal] [green][i]'RaceNumber'[/i][/green] [teal]=>[/teal] [green][i]'15126'[/i][/green][teal],[/teal] [green][i]'RaceID'[/i][/green] [teal]=>[/teal] [green][i]'84ee45s89'[/i][/green] [teal]}[/teal][teal],[/teal]
  [teal]{[/teal] [green][i]'RaceTime'[/i][/green] [teal]=>[/teal] [green][i]'15'[/i][/green][teal],[/teal] [green][i]'RaceNumber'[/i][/green] [teal]=>[/teal] [green][i]'15126'[/i][/green][teal],[/teal] [green][i]'RaceID'[/i][/green] [teal]=>[/teal] [green][i]'84ee45s89'[/i][/green] [teal]}[/teal][teal],[/teal]
  [teal]{[/teal] [green][i]'RaceTime'[/i][/green] [teal]=>[/teal] [green][i]'10'[/i][/green][teal],[/teal] [green][i]'RaceNumber'[/i][/green] [teal]=>[/teal] [green][i]'17894'[/i][/green][teal],[/teal] [green][i]'RaceID'[/i][/green] [teal]=>[/teal] [green][i]'1ee547987'[/i][/green] [teal]}[/teal]
[teal]];[/teal]

[b]my[/b] [navy]$nr[/navy] [teal]=[/teal] [purple]0[/purple][teal];[/teal]
[b]foreach[/b] [b]my[/b] [navy]$item[/navy] [teal]([/teal]@[teal]{[/teal][navy]$variable1[/navy][teal]}[/teal][teal])[/teal] [teal]{[/teal]
  [b]print[/b] [green][i]' - item '[/i][/green][teal],[/teal] [navy]$nr[/navy][teal]++,[/teal] [green][i]" :\n"[/i][/green][teal];[/teal]
  [b]foreach[/b] [b]my[/b] [navy]$key[/navy] [teal]([/teal][b]keys[/b] [navy]$item[/navy][teal])[/teal] [teal]{[/teal]
    [b]print[/b] [green][i]'   - '[/i][/green][teal],[/teal] [navy]$key[/navy][teal],[/teal] [green][i]' = '[/i][/green][teal],[/teal] [navy]$item[/navy][teal]->[/teal][teal]{[/teal][navy]$key[/navy][teal]}[/teal][teal],[/teal] [green][i]"\n"[/i][/green][teal];[/teal]
  [teal]}[/teal]
[teal]}[/teal]
Code:
 - 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.
[link feherke.github.com/][/url]
 
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
 
Hi,

@spewn:

You are describing "references". The Perl docs explain references well ( ).

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 [red]%{[/red]$item[red]}[/red]) {
	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";
}
 
Hi

Zhris said:
Code:
foreach my $key (keys [red]%{[/red]$item[red]}[/red]) {
	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 :
man perlfunc said:
Starting with Perl 5.14, [tt]keys[/tt] 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 [tt]keys[/tt] is considered highly experimental. The exact behaviour may change in a future version of Perl.
Thanks for throwing this in attention.


Feherke.
[link feherke.github.com/][/url]
 
@feherke,

Thanks alot for the information regarding keys.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top