Perl: Creating new variables inside a loop -
i have code following:
$player_2ubid = grep { $_->ubid eq "2ubid" } @{$room_members }; $player_3ubid = grep { $_->ubid eq "3ubid" } @{$room_members }; # .... $player_11ubid = grep { $_->ubid eq "11ubid" } @{$room_members };
to avoid repetition, want go loop
for $i ( 2 .. 11 ){ $player_.$i.ubid = grep { $_->ubid eq "$i.ubid" } @{$room_members }; }
however produces syntax error:
can't modify concatenation (.) or string in scalar assignment @ ***.t line 100, near "};"
what doing wrong here?
first have write $i."ubid"
or "${i}ubid"
instead of "$i.ubid"
.
and i'd use hash like:
my %player_ubid; $i ( 2 .. 4 ){ $player_ubid{$i} = grep { $_->ubid eq $i."ubid" } @{$room_members }; }
Comments
Post a Comment