CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Can you explain me the following code?

 



Shai
New User

Nov 26, 2012, 11:59 PM

Post #1 of 5 (1617 views)
Can you explain me the following code? Can't Post

my $keyHash = sub {
my $hash = shift;
my @keys = grep {s!^\{|\}$!!g; $_} split /->/ => "@_";

$hash = $$hash{$_} for @keys;
$hash
};

my $string1 = "{books}";
my $string2 = "{31335}->{book_name}";
print $hash_ref->$key($string1)->$key($string2); # prints 'perl'

My questions are:

1. What the $_ inside the grep mean?

2. Please explain the regular expression in the grep? How does it remove the curly brackets?

Thanks,

Shai


rovf
Veteran

Nov 27, 2012, 12:35 AM

Post #2 of 5 (1616 views)
Re: [Shai] Can you explain me the following code? [In reply to] Can't Post

For (1): See perldoc -f grep, second paragraph.

For (2): You can see that the right part of the substitution is empty (i.e. we have the construct s!SOMETHING!!). This means that every occurance of SOMETHING is removed. In this case it means that either an open curly at the start of the string and a closed curly at the end of the line is removed.


Laurent_R
Enthusiast / Moderator

Nov 27, 2012, 3:18 AM

Post #3 of 5 (1614 views)
Re: [rovf] Can you explain me the following code? [In reply to] Can't Post

The split procedure returns a list of value that are analysed by the grep. In a grep, each value in the list supplied by the split is successively stored in $_. The s!!! statement operates by default on $_ and removes from $_ a leading { or a trailing }. Then the $_ at the end of the grep block ensures that each $_ thus modified is returned into the @keys array.

In this case, unless I miss something, I think that a map function would probably be better than grep, which is normally used to filter the values, where as no filtering occurs here.


rovf
Veteran

Nov 27, 2012, 5:10 AM

Post #4 of 5 (1610 views)
Re: [Laurent_R] Can you explain me the following code? [In reply to] Can't Post


Quote
unless I miss something


I think you do. In the OP's code, grep removes all lines which are either

* initially empty, or
* consist only of a curly brace (or just an empty pair of curlies), or - as a funny side effect -
* consist only of the digit zero (after removal of appropriate curlies)

Don't ask me whether this makes sense, but this is different from using map - unless it is me who is missing something now ;-)


Laurent_R
Enthusiast / Moderator

Nov 27, 2012, 10:51 AM

Post #5 of 5 (1605 views)
Re: [rovf] Can you explain me the following code? [In reply to] Can't Post


In Reply To
I think you do. In the OP's code, grep removes all lines which are either

* initially empty, or
* consist only of a curly brace (or just an empty pair of curlies), or - as a funny side effect -
* consist only of the digit zero (after removal of appropriate curlies)

Don't ask me whether this makes sense, but this is different from using map - unless it is me who is missing something now ;-)


Yes, you are absolutely right, I reacted too quickly, I should have explained better what I meant. It does indeed some filtering if $_ is reduced to 0 or "". What I really meant to say is that I wasn't sure this was really the desired effect to do this.

But perhaps it makes sense, after all...

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives