Trying to understand Perl

P

Patrick

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
 
P

Paul Lalli

Patrick said:
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
 
T

Tiro Verus

Patrick said:
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
 
T

Tad McClellan

Patrick said:
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.
 
P

parv

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
 
T

Tad McClellan

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

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?
 
P

parv

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
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top