0

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;
  • The accepted answer tells what `=~` and what `~~` represent in Perl, duplicate of [What does " ~~ " mean in Perl?](http://stackoverflow.com/questions/3094916/what-does-mean-in-perl) – Prix Mar 04 '14 at 01:34
  • @Prix I don't see ~~ here – user4035 Mar 04 '14 at 01:41
  • @user4035 no you don't but you see `=~` don't you? Have you also read my comment prior the link? Guess not. – Prix Mar 04 '14 at 01:41
  • @user3353920 you can use the [**`Edit`**](http://stackoverflow.com/posts/22161152/edit) link below your question to update new information to it. – Prix Mar 04 '14 at 01:43
  • 1
    `= ~` 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). – TLP Mar 04 '14 at 01:44
  • @Prix Look at TLPs comment. Looks like, "`=~`" is not the same as "`= ~`" (with space between them) – user4035 Mar 04 '14 at 01:45
  • @user4035 you're pointing it to the wrong person. regardless the OP is trying to run a regex and he wants to know what `=~` does on it obviously. Typo happens. – Prix Mar 04 '14 at 01:46
  • Thank you, that does help explain the tilde. I am confused on after the tilde as well. What is that statement doing? My confusion is the s/\d{2,3} part. – user3353920 Mar 04 '14 at 01:47
  • Thank you everyone! As I was practicing I was putting the space between the = and ~ and that is why I was getting numbers instead of my intended output. Your explanations and guidance were beneficial. – user3353920 Mar 04 '14 at 01:49

1 Answers1

3

= ~ 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;
TLP
  • 66,756
  • 10
  • 92
  • 149