Trying to understand Perl

Discussion in 'Perl Misc' started by Patrick, Nov 30, 2004.

  1. Patrick

    Patrick Guest

    Hello good people,

    I am trying to understand Perl and why things work. Could someone break
    down for me the snippet of code below and tell me exactly how the
    operation works. I guess I am mostly confused about the placement/order
    of elements of the code and how Perl interprets it. Oh and exactly how
    does $_ work. I think if I could get a better understanding of the how
    and why it might clear up my confusion. Thanks, Patrick

    (grep {$newfile eq $_} @files)


    --
    Patrick A. Smith Web Administrator
    Ocean Circulation Group – USF - College of Marine Science
    http://ocg6.marine.usf.edu Phone: 727 553-3334

    The trouble with doing something right the first time is that nobody
    appreciates how difficult it was. - La Rochefoucauld
    Patrick, Nov 30, 2004
    #1
    1. Advertising

  2. Patrick

    Paul Lalli Guest

    "Patrick" <> wrote in message
    news:coihi3$p2t$...
    > I am trying to understand Perl and why things work. Could someone

    break
    > down for me the snippet of code below and tell me exactly how the
    > operation works. I guess I am mostly confused about the

    placement/order
    > of elements of the code and how Perl interprets it. Oh and exactly how
    > does $_ work. I think if I could get a better understanding of the how
    > and why it might clear up my confusion. Thanks, Patrick
    >
    > (grep {$newfile eq $_} @files)


    The C<grep> function takes two arguments. The first is a block or
    expression that returns a boolean value. The second is a list of
    elements. In this case, the first argument is the block
    { $newfile eq $_ }
    and the second argument is the list of elements contained in
    @files

    The function goes through every element in the list, one at a time. On
    each iteration, it assigns the current element of @files to the special
    variable $_. It then executes the block. If the block returns a true
    value, the current element of the list is added to the resulting list.

    When the operation completes, the function will have returned a list of
    all items from the original list for which the block returned a true
    value.

    An example:

    @odds = grep { $_ % 2 == 1 } (1..10);

    This statement will take each number 1 through 10, and assign it to $_.
    It then executes the statement $_ % 2 == 1. If this statement is true
    for the current element, that number is added to @odds.

    The grep function can usually be written more expansively as a foreach
    loop:

    my @odds;
    foreach (1..10){ #go through the list, assigning each one to $_
    if ($_ % 2 == 1) { #make the comparison
    push @odds, $_; # if true, add the current element to the result
    }
    }

    I hope this explanation helps you,
    Paul Lalli
    Paul Lalli, Nov 30, 2004
    #2
    1. Advertising

  3. Patrick

    Tiro Verus Guest

    Patrick <> wrote:
    > Hello good people,


    > I am trying to understand Perl and why things work. Could someone break
    > down for me the snippet of code below and tell me exactly how the
    > operation works. I guess I am mostly confused about the placement/order
    > of elements of the code and how Perl interprets it. Oh and exactly how
    > does $_ work. I think if I could get a better understanding of the how
    > and why it might clear up my confusion. Thanks, Patrick


    > (grep {$newfile eq $_} @files)


    for $_ , read perldoc perlvar
    Tiro Verus, Nov 30, 2004
    #3
  4. Patrick <> wrote:

    > I am trying to understand Perl and why things work.



    Perl comes with lots of documentation covering such things.

    Have you looked there yet?


    > Could someone break
    > down for me the snippet of code below and tell me exactly how the
    > operation works.



    > (grep {$newfile eq $_} @files)



    grep is a function, read about it in:

    perldoc perlfunc

    eq is an operatior, read about it in:

    perldoc perlop

    $newfile and @files are data, read about them in:

    perldoc perldata

    The parenthesis do nothing there, as far as I can tell.

    The curlies form a BLOCK of code to execute.


    If you still don't understand the code after reading up on all of the
    components, then please post again and describe what part you need
    help with.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 1, 2004
    #4
  5. Patrick

    parv Guest

    in message <Wa4rd.1965$zK1.85@trndny05>,
    wrote Paul Lalli ...

    > "Patrick" <> wrote in message
    > news:coihi3$p2t$...
    >>
    >> (grep {$newfile eq $_} @files)

    >
    > The C<grep> function takes two arguments. The first is a block or
    > expression that returns a boolean value. The second is a list of
    > elements.


    First argument does not need to be a BLOCK, it can be an EXPRession
    too. See more in perldoc(1) about grep & map functions.


    > In this case, the first argument is the block { $newfile eq $_ } and
    > the second argument is the list of elements contained in @files


    In this case, it is indeed a block.


    - parv

    --
    As nice it is to receive personal mail, too much sweetness causes
    tooth decay. Unless you have burning desire to contact me, do not do
    away w/ WhereElse in the address for private communication.
    parv, Dec 1, 2004
    #5
  6. parv <> wrote:
    > in message <Wa4rd.1965$zK1.85@trndny05>,
    > wrote Paul Lalli ...


    >> The C<grep> function takes two arguments. The first is a block or

    ^^^^^^^^
    >> expression

    ^^^^^^^^^^

    > First argument does not need to be a BLOCK, it can be an EXPRession
    > too.



    Errr, that's what Paul said.

    You are acting like you are correcting him, what error did he make?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 1, 2004
    #6
  7. Patrick

    parv Guest

    in message <>,
    wrote Tad McClellan ...

    > parv <> wrote:
    >> in message <Wa4rd.1965$zK1.85@trndny05>,
    >> wrote Paul Lalli ...

    >
    >>> The C<grep> function takes two arguments. The first is a block or

    > ^^^^^^^^
    >>> expression

    > ^^^^^^^^^^
    >
    >> First argument does not need to be a BLOCK, it can be an EXPRession
    >> too.

    >
    > Errr, that's what Paul said.
    >
    > You are acting like you are correcting him, what error did he make?


    Yikes! My fault! I completely missed that; somehow "block" blocked
    my eyes.


    Sorry, Paul (and everybody else).


    - parv

    --
    As nice it is to receive personal mail, too much sweetness causes
    tooth decay. Unless you have burning desire to contact me, do not do
    away w/ WhereElse in the address for private communication.
    parv, Dec 1, 2004
    #7
    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. Paul K

    Trying to understand...

    Paul K, Nov 19, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    338
    Paul K
    Nov 19, 2003
  2. =?Utf-8?B?QmlsbCBCb3Jn?=

    Trying to understand ticket/cookie expiration

    =?Utf-8?B?QmlsbCBCb3Jn?=, Oct 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    346
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Oct 8, 2004
  3. jim

    trying to understand postback

    jim, Nov 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    361
    =?Utf-8?B?Sm9lbCBDYWRl?=
    Nov 22, 2004
  4. ed
    Replies:
    11
    Views:
    171
    Euclid Uranium
    Nov 24, 2005
  5. walterbyrd
    Replies:
    18
    Views:
    264
    C.DeRykus
    Dec 16, 2008
Loading...

Share This Page