Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I would format it slightly, and then just read from the bottum up. Yes map and grep can be scary, but normaly reading them isn't hard (just figuring out how/when to use them is.

sub _file_containing { my($self, $id) = @_; opendir(my $dir, $self->{update_dir}) or return; my @files = map { "$self->{update_dir}/$_" } sort { $a <=> $b } grep { /^\d+$/ && $_ <= $id } readdir $dir; return $files[-1]; }

So now start at the bottom of the statment and read up. readdir $dir takes all the files in the directory $self->{update_dir} and pipes them into the grep above it. grep { /^\d+$/ && $_ <= $id } is a filter that only lets files that look like a number and are less than $id through. sort { $a <=> $b } sorts the files using a numeric sort (instead of cmp that would do an alphanumeric). map  { "$self->{update_dir}/$_" } takes that list and adds the actual directory to the front of each element. The map in this case is kinda pointless because you then return only the last element, however the map means that you don't have to check if there was indeed a last element in the list to return. If you replaced the last return with return $self->{update_dir} || '/' || $files[-1]; then you would wrongly return the directory itself when there where no matching files.


___________
Eric Hodges

In reply to Re: How do I un-map this code? by eric256
in thread How do I un-map this code? by Plankton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found