Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help reading Perl - what is this doing?

Reply
Thread Tools

Help reading Perl - what is this doing?

 
 
caelica
Guest
Posts: n/a
 
      01-25-2011
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[i] =~ s/^\?+$//;
$var_in_for_loop[i] =~ s/^ +//;
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      01-25-2011
In article
<(E-Mail Removed)>,
caelica <(E-Mail Removed)> wrote:

> 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[i] =~ s/^\?+$//;
> $var_in_for_loop[i] =~ 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".

--
Jim Gibson
 
Reply With Quote
 
 
 
 
caelica
Guest
Posts: n/a
 
      01-25-2011
On Jan 24, 4:59*pm, Jim Gibson <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
>
> caelica <(E-Mail Removed)> wrote:
> > 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[i] =~ s/^\?+$//;
> > $var_in_for_loop[i] =~ 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".
>
> --
> Jim Gibson


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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-25-2011
>>>>> "c" == caelica <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments