Perform an operation on every item of a list, e.g.
for the list ["ox", "cat", "deer", "whale"] calculate
the list of sizes of the strings, e.g. [2, 3, 4, 5]
perl
my @list = qw{ox cat deer whale};
my @lengths = map {length($_)} @list;
print "@list\n"; print "@lengths\n";
my @list = qw{ox cat deer whale};
my @lengths = map {length($_)} @list;
print "@list\n"; print "@lengths\n";
java
public class SolutionXX { public static void main(String[] args) { String[] list = {"ox", "cat", "deer", "whale"}; for (String str : list) { System.out.println(str.length() + " "); } } }
public class SolutionXX { public static void main(String[] args) { String[] list = {"ox", "cat", "deer", "whale"}; for (String str : list) { System.out.println(str.length() + " "); } } }