Help reading Perl - what is this doing?

C

caelica

New to Perl and trying to read my way through some Perl scripts. I've
looked up the individual characters to find their meaning but I can't
put it together.

Can anyone tell me what these lines are doing to the variable?

$var_in_for_loop =~ s/^\?+$//;
$var_in_for_loop =~ s/^ +//;
 
J

Jim Gibson

caelica said:
New to Perl and trying to read my way through some Perl scripts. I've
looked up the individual characters to find their meaning but I can't
put it together.

Can anyone tell me what these lines are doing to the variable?

$var_in_for_loop =~ s/^\?+$//;
$var_in_for_loop =~ s/^ +//;


Both of those lines would seem to contain an error. I say "seem"
because the lines will compile and execute, but they don't make sense.

You likely mean this:

$var_in_for_loop[$i] =~ s/^\?+$//;
$var_in_for_loop[$i] =~ s/^ +//;

Notice that I have added a '$' character before each 'i'. $i is a
scalar variable that can contain, for example, an integer index into
the @var_in_for_loop array. The original 'i' is a "bareword", that may
be interpreted as a character string and converted into the value zero,
which is probably not what you want.

The variable $var_in_for_loop[$i] is a member of the array
@var_in_for_loop (with index starting from zero).

The first line looks for a string consisting only of one or more '?'
characters and, if it finds such a string, deletes all of the '?'
characters, replacing them with nothing, resulting in an empty string.

The second lines looks for a string with one or more spaces at the
beginning of the string and removes them, leaving any subsequent
characters as they are.

To read these lines you need to know that:

=~ is the binding operator that applies the substitution operator to
the right of the binding operator to the variable to its left.

s/// is the substitution operator.

The first pair of // encloses the pattern (regular expression, or RE).

The second pair of // encloses the replacement string, empty in both of
these cases.

^ is the RE "meta-character" that matches "start-of-string".

\? is a literal question-mark character (escaped because an unescapoed
'?' is a meta-character).

+ is a meta-character meaning "one or more of the preceding character".

$ is a meta-character matching "the end of the string".
 
C

caelica

caelica said:
New to Perl and trying to read my way through some Perl scripts.  I've
looked up the individual characters to find their meaning but I can't
put it together.
Can anyone tell me what these lines are doing to the variable?
$var_in_for_loop =~ s/^\?+$//;
$var_in_for_loop =~ s/^ +//;


Both of those lines would seem to contain an error. I say "seem"
because the lines will compile and execute, but they don't make sense.

You likely mean this:

$var_in_for_loop[$i] =~ s/^\?+$//;
$var_in_for_loop[$i] =~ s/^ +//;

Notice that I have added a '$' character before each 'i'. $i is a
scalar variable that can contain, for example, an integer index into
the @var_in_for_loop array. The original 'i' is a "bareword", that may
be interpreted as a character string and converted into the value zero,
which is probably not what you want.

The variable $var_in_for_loop[$i] is a member of the array
@var_in_for_loop (with index starting from zero).

The first line looks for a string consisting only of one or more '?'
characters and, if it finds such a string, deletes all of the '?'
characters, replacing them with nothing, resulting in an empty string.

The second lines looks for a string with one or more spaces at the
beginning of the string and removes them, leaving any subsequent
characters as they are.

To read these lines you need to know that:

=~ is the binding operator that applies the substitution operator to
the right of the binding operator to the variable to its left.

s/// is the substitution operator.

The first pair of // encloses the pattern (regular expression, or RE).

The second pair of // encloses the replacement string, empty in both of
these cases.

^ is the RE "meta-character" that matches "start-of-string".

\? is a literal question-mark character (escaped because an unescapoed
'?' is a meta-character).

+ is a meta-character meaning "one or more of the preceding character".

$ is a meta-character matching "the end of the string".


Yes Jim, you are correct. I was typing those lines in from my code
and accidentally left out the $ in front of i in the variable. They
are correct in the code, typo on my part. Thank you for the
explanation! I could see the end result but needed a breakdown of the
line to understand. This is helpful.

Kyla
 
U

Uri Guttman

c> Yes Jim, you are correct. I was typing those lines in from my code
c> and accidentally left out the $ in front of i in the variable. They
c> are correct in the code, typo on my part. Thank you for the
c> explanation! I could see the end result but needed a breakdown of the
c> line to understand. This is helpful.

then learn this lesson: never retype code for posting. always cut/paste
it so we can read exactly what you are running.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top