Help reading Perl - what is this doing?

Discussion in 'Perl Misc' started by caelica, Jan 25, 2011.

  1. caelica

    caelica Guest

    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/^ +//;
    caelica, Jan 25, 2011
    #1
    1. Advertising

  2. caelica

    Jim Gibson Guest

    In article
    <>,
    caelica <> 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 =~ 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".

    --
    Jim Gibson
    Jim Gibson, Jan 25, 2011
    #2
    1. Advertising

  3. caelica

    caelica Guest

    On Jan 24, 4:59 pm, Jim Gibson <> wrote:
    > In article
    > <>,
    >
    > caelica <> 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 =~ 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".
    >
    > --
    > 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
    caelica, Jan 25, 2011
    #3
  4. caelica

    Uri Guttman Guest

    >>>>> "c" == caelica <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Jan 25, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,784
  2. deech
    Replies:
    6
    Views:
    605
    Default User
    Feb 12, 2010
  3. Chris McMahon
    Replies:
    0
    Views:
    77
    Chris McMahon
    Mar 26, 2006
  4. Phil
    Replies:
    1
    Views:
    75
    Steve Grazzini
    Jul 27, 2003
  5. Marc S. Gibian

    Perl build script woes doing Source Safe get

    Marc S. Gibian, Aug 23, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    96
    Marc S. Gibian
    Aug 23, 2003
Loading...

Share This Page