in reply to Perl SDL instantiation with Sprites
As said there a lot of options to deal with lists and arrays: foreach while map shift pop ...my @coord_pairs = ( [1,2], [3,7], [5,9]); my @enemy_container; foreach my $index (coord_pairs) { my $enem = { sprite => $enemy_sprite , v_y => $coord_pairs[$index]->[0], # element 0 of the current p +air v_x => $coord_pairs[$index]->[1], # element 1 of the current p +air }; push @enemy_container, $enem; }
As little side note, avoid name like $20 for your variables: if you are starting coding choice meaningful name for them as i tried to do in my little example. There is chance to other parts of the program change the value (accidentally) of $20 ending with $20 containing 30. Perl let you to define true constant as described in the docs. In this case you can use constant TWENTY => 20; because number are not allowed as constant name (but you can use _20).my @coord_pairs = ( [1,2], [3,7], [5,9]); my @enemy_container; foreach my $index (coord_pairs) { my $temp_enemy_sprite = SDLx::Sprite->new ( width => $20, height => + $20 ); $enemy_sprite->load('example.png'); my $enem = { sprite => $temp_enemy_sprite , v_y => $coord_pairs[$index]->[0], # element 0 of the current p +air v_x => $coord_pairs[$index]->[1], # element 1 of the current p +air }; push @enemy_container, $enem; }
|
|---|