tags:

views:

59

answers:

2

let's say you have set of integer in the list.

List Declare:

@lists = (22, 10, 5, 2);

but if I do want all the elements to be divide in let's say 2, is there anyways to do other than manually computing in running loop?

Don't want to compute like this:

foreach $list (@lists)
{
    print (list/2);
}
+5  A: 
@lists = [22, 10, 5, 2];

should be

@lists = (22, 10, 5, 2);

then you can

@lists = map { $_ / 2 } @lists
David Dorward
Ah ha! so map is used for those purpose huh?Thumbs up! I'm beginner of perl but it looks amazing language!!Thanks.
Yoon Lee
@Yoon: `map` is wonderful for doing all kinds of list processing.
Ether
+6  A: 
my @numbers = (22, 10, 5, 2);

# Create a new list, as in David Dorward's answer.
my @halves = map { $_ / 2 } @numbers;

# Or modify the original list directly.
$_ /= 2 for @numbers;
FM