Dartanion has asked for the wisdom of the Perl Monks concerning the following question:

Here is teh piece of code i need an answer for.
@pages[$PG]=~/(.*\nend\n).*/s; @pages[$PG]=$1 ;
Thanks for all the help

Edit by myocom: Added code tags and fixed formatting.

Replies are listed 'Best First'.
Re: Can anyone tell me what this is doing?
by dws (Chancellor) on Aug 08, 2002 at 18:54 UTC
    Can anyone tell me what this is doing?

    It looks like what someone is trying to do is take the text in $pages[$PG] and trim off everything after the final "end" line. It also looks like they don't understand array slices.

Re: Can anyone tell me what this is doing?
by DamnDirtyApe (Curate) on Aug 08, 2002 at 18:55 UTC

    Your code appears to discard everything in $pages[$PG] after the word "end" (on a line by itself.)

    Note that this should probably be referring to $pages[$PG], rather than @pages[$PG].


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      ...what's frustrating (to a project lead, a maintenance programmer, or an instructor) is that it would work as published.

      Sometime, programmers go for many months (or longer) thinking that:

      @table[1]

      is the same as:
      $table[1]

      because it works the same most of the time. It's only when something breaks that we start to truly care about the correct way to do things.

      And of course we all know what happens next year. :-)

      ...all the world looks like -well- all the world, when your hammer is perl.
      ---v

Re: Can anyone tell me what this is doing?
by thelenm (Vicar) on Aug 08, 2002 at 20:26 UTC
    In addition to what others have pointed out, another thing to notice about this code is that when the pattern doesn't match (i.e, the string "\nend\n" never occurs within $pages[$PG]), then $pages[$PG] will be set to undef whatever the previous value of $1 was. This may or may not be is probably not what you want. , but I point it out just in case it's not.

    If you want to leave $pages[$PG] alone if the pattern doesn't match, use a substitution instead:

    $pages[$PG] =~ s/(.*\nend\n).*/$1/s;

    -- Mike

    --
    just,my${.02}

Re: Can anyone tell me what this is doing?
by Dartanion (Initiate) on Aug 08, 2002 at 19:04 UTC
    Thanks everyone