Gossamer Forum
Home : General : Perl Programming :

grep and $_ question

Quote Reply
grep and $_ question
can you please explain what the line:
if (grep {$_ eq $name} @players) {
in the code below does? what is $_ in this context? thanks!

Code:
my (@players,$pEmail,@sent_to,$user_sent,$name,@signed_up,@not_signed_up);
# my ($to,$reply_to,$returnpath,$cc,$bcc,$found,$subscriber);
my ($found,$subscriber);
$reply_to = $in{'email'};
$returnpath = $admin_email;

@players = split (/\|/, $rec{'Players'});

my (%sav) = %in;
&switch_to_members;
undef %in;
$in{'Member'} = 'Yes';
$in{'sb1'} = '1';
my ($status2, @hits2) = &query2("view");
%in = %sav;
if($status2 eq 'ok') {
my ($numhits2) = ($#hits2+1) / ($#db2_cols+1);
for (0 .. $numhits2 - 1) {
$email_msg = $orig_msg; # 05/10/2022
$email_msg .= $rec{'DayOfWeek'} . " " . $date . "~"; # 06/30/2022
$email_msg .= $rec{'Title'} . "~~";
$found = 0; # 06/03/2022
$subscriber = ''; # 07/22/2022
%rec2 = &array_to_hash2($_, @hits2);
if (($rec2{'Email'} ne $default_email) && ($rec2{'Email'} =~ /.+\@.+\..+/)) { # valid email
$name = $rec2{'Userid'};
$to = $rec2{'Email'};
if ($rec2{'Subscriptions'} =~ /EventNotifications/) { # 07/22/2022
$subscriber = 'Subscriber';
}
if (grep {$_ eq $name} @players) {
$found = 1;
$subject = "Signup Confirmation Notice";
$email_msg .= qq|~You are signed up to play $rec{'DayOfWeek'} $date. ~~
Online signups are now closed. Please call the pro shop if you need to cancel.|;
$email_msg .= '~~' . $players; # 06/02/2022
#push (@signed_up,$to); # collect the email addresses
push (@signed_up,$rec2{'Userid'} . " " . $rec2{'Firstname'} . " " . $rec2{'Lastname'} . " " . $to . " " . $subscriber );
}
else {
$subject = "Online Signups Closed";
$email_msg .= qq|~You are NOT signed up to play $rec{'DayOfWeek'} $date. ~
Online signups are now closed. Please call the pro shop to sign up.|;
#push (@not_signed_up,$to); # collect the email addresses
push (@not_signed_up,$rec2{'Userid'} . " " . $rec2{'Firstname'} . " " . $rec2{'Lastname'} . " " . $to . " " . $subscriber);
}

Quote Reply
Re: [delicia] grep and $_ question In reply to
$_ is from a foreach () loop. So $_ will contain the value from the current loop

BTW, posting your code like this makes it impossible to read. The best option is copy and paste into the PLAIN editor (and wrap [code][/code] tags around it, so it formats nicely)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] grep and $_ question In reply to
i copied and pasted code from notepad plus inside the code tags. what am i doing wrong? and re: my question, what is the value of $_ in that loop? thanks!
Quote Reply
Re: [delicia] grep and $_ question In reply to
Hi,

It should all be indented if you did it right :) (which it isn't). Could be your code isn't indented in Notepad?

Anyway, to answer your question:

Code:
if (grep {$_ eq $name} @players) {

this is basically looking at each value of @players, and checking if the value matched $name. Its a shorthand way of doing something like:

Code:
foreach (@players) {
if ($_ eq $name) {
print "its a match!"
}
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] grep and $_ question In reply to
is
Code:
if (grep {$_ eq $name} @players) {
the same as


Code:
if (grep {$name eq $_ } @players) {
Quote Reply
Re: [delicia] grep and $_ question In reply to
I guess so - but it makes for weird "reading" of the code :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!