Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How For loop will work in perl

Tags:

perl

#!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
  s/\w<([^>]+)>/\U$1/g;
  print;
}

How will the expression s/\w<([^>]+)>/\U$1/g;work?

like image 889
Rock Avatar asked May 31 '26 07:05

Rock


1 Answers

The substitution does this:

s/             
    \w<         # look for a single alphanumeric character followed by <
    ([^>]+)     # capture one or more characters that are not <
    >           # followed by a >
/               ### replace with
   \U           # change following text to uppercase
   $1           # the captured string from above
/gx             # /g means do this as many times as possible per line

I added the /x modifier to be able to visualize the regex. The character class [^>] is negated, as denoted by the ^ character after the [, which means "any character except >".

For example, in the output from the perldoc command

X<atan2> X<arctangent> X<tan> X<tangent>

Is changed to

ATAN2 ARCTANGENT TAN TANGENT
like image 191
TLP Avatar answered Jun 02 '26 06:06

TLP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!