Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Extract the middle part of a list

by Zaxo (Archbishop)
on Jun 29, 2007 at 02:12 UTC ( [id://624017]=note: print w/replies, xml ) Need Help??


in reply to Extract the middle part of a list

You want grep, with some data extraction in the choice routine.

our ($starttime, $endtime) = init(); # . . . my @selected = grep { my $time = (split /[_.]/)[2]; $time > $starttime and $time <= $endtime;; } </path/to/*>;
Not having to sort helps, since we don't need to keep values for comparison. If there are files there which don't follow the naming scheme, you may need to filter them out with another grep or with map, or a refinement of the glob in angles.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Extract the middle part of a list
by chrism01 (Friar) on Jun 29, 2007 at 04:51 UTC
    Guys,

    Thx for both of those. I decided to go with Zaxo because it's simpler and works down the page (I think).
    However, it's complaining about the curr/parent dir files (., ..) which i tried to fix, but I'm not good with these nested code layouts.
    I tried:

    @t_arr = grep { grep { $_ !~ /^\./ } $var3 = (split /[_.]/)[2]; $var3 > $var1 and $var3 <= $var2; } readdir(EVT_DIR);
    and a couple of variations, but still get warnings:
    Use of uninitialized value in pattern match (m//) at ./t.pl line 341. Use of uninitialized value in numeric gt (>) at ./t.pl line 345. Use of uninitialized value in pattern match (m//) at ./t.pl line 341. Use of uninitialized value in numeric gt (>) at ./t.pl line 345.
    I also need to ignore any dirs that exist.
    Any chance of the correct code?
    Guess I need a tutorial article on nested code blocks (if that's the correct description)

    Cheers
    Chris

      As you say, your blocking is incorrect. Here's how to write that:

      @t_arr = grep { $var3 = (split /[_.]/)[2]; $var3 > $var1 and $var3 <= $var2; } grep { !-d and $_ !~ /^\./ } readdir(EVT_DIR);
      where I've added !-d to exclude directories. You might want to use -f instead to admit only regular files.

      After Compline,
      Zaxo

        Zaxo,
        close... it ignores curr, parent dirs fine, but still trips up over 'normal' dir. Tried it both as you said { !-d and $_ !~ /^\./ } and replace whole line with { -f }, no go.

        Chris
        BTW, am I right in believing that the logic is up/back for outer sub-blocks ie greps, but fwd/down for sub-block contents?
        Also, why 'and' not '&&'?
        This is a very educational thr (for me at least) :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://624017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found