Вопрос

I'm reviewing my homework and I am confused on a statement from my notes. If someone could explain what the tilde is doing as well as the s/\d that would be great.

@name = ("Name: Bruce Grade: 85", "Name: Jill Grade: 87");
@GradeA = map { $entry = $_; $entry = ~ s/\d{2,3}/A/; $entry} @GradeA;
Это было полезно?

Решение

= ~ is two operators: assignment and bitwise negation. Seeing as it is followed by a regex substitution it is likely that you have confused it with =~, which is the binding operator, used with regexes (among other things).

Assuming that = ~ is a typo, the map statement simply applies a regex substitution s/// to a list of strings, changing 2-3 numbers (e.g. 12 or 123) to A. It is written somewhat redundantly, and can be reduced to

s/\d{2,3}/A/ for @GradeA;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top