Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Trying to understand Perl

Reply
Thread Tools

Trying to understand Perl

 
 
Patrick
Guest
Posts: n/a
 
      11-30-2004
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      11-30-2004
"Patrick" <(E-Mail Removed)> wrote in message
news:coihi3$p2t$(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
Tiro Verus
Guest
Posts: n/a
 
      11-30-2004
Patrick <(E-Mail Removed)> 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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-01-2004
Patrick <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
parv
Guest
Posts: n/a
 
      12-01-2004
in message <Wa4rd.1965$zK1.85@trndny05>,
wrote Paul Lalli ...

> "Patrick" <(E-Mail Removed)> wrote in message
> news:coihi3$p2t$(E-Mail Removed)...
>>
>> (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.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-01-2004
parv <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
parv
Guest
Posts: n/a
 
      12-01-2004
in message <(E-Mail Removed)>,
wrote Tad McClellan ...

> parv <(E-Mail Removed)> 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.

 
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
Noob trying to understand simple Perl grep statement walterbyrd Perl Misc 18 12-16-2008 01:44 AM
Newbie trying to understand some perl code. ed Perl Misc 11 11-24-2005 05:22 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Trying to Understand Layer 2 scadav Cisco 6 07-07-2005 05:24 PM
Trying to understand... Paul K ASP .Net 2 11-19-2003 11:39 AM



Advertisments