### Re^3: Replacing values in an array

by muba (Priest)
 on Jan 27, 2013 at 05:37 UTC Need Help??

in reply to Re^2: Replacing values in an array
in thread Replacing values in an array

map and grep can be an intimidating functions, but quite useful once you understand them.

Let's consider grep first, since this one is a little simpler to grasp — or at least, that was my experience. grep takes two arguments, the second being a list of elements to work with, the first being the work that you want to have done on that list. You can specify that as a BLOCK, a code reference, or just the name of a function. grep then loops over the list, aliasing \$_ to each element in turn, and calls the given piece of code. Then it returns every element for which the code returned a true value.

```my @numbers = (0, 0.5, 1, 1.5, 2, 2.5);

# Calling grep with a BLOCK:
my @integers = grep {int(\$_) == \$_} @numbers;
print join(", ", @integers), " are integers.\n";

# Calling grep with a function name:
my @basket = ("apple", undef, undef, undef, "banana", "cherry", undef,
+ "date");

+et_1), "\n";

See what happens? grep returns the elements of the list you gave it, for which the piece of code returns true. The non-grep equivalents would be:

```my @numbers = (0, 0.5, 1, 1.5, 2, 2.5);

# Calling grep with a BLOCK:
my @integers;
for (@numbers) {
push @integers, \$_ if int(\$_) == \$_;
}
print join(", ", @integers), " are integers.\n";

# Calling grep with a function name:
my @basket = ("apple", undef, undef, undef, "banana", "cherry", undef,
+ "date");

}
+et_1), "\n";

Now, map is pretty similar, except that it allows you to change the elements:

```my @numbers = 1..10;
my @times_ten = map { \$_ * 10 } @numbers;
print join(", ", @times_ten), "\n";

Of course, these are just the basics — the range of things you can do with them is astonishing. I hope this helps you along.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1015562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-09-13 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The PerlMonks site front end has:

Results (21 votes). Check out past polls.

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.