Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: glob an array?

by Anonymous Monk
on Feb 13, 2021 at 12:15 UTC ( #11128326=note: print w/replies, xml ) Need Help??


in reply to Re: glob an array?
in thread glob an array?

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: glob an array?
by Marshall (Canon) on Feb 14, 2021 at 03:00 UTC
    Geez! I have no idea exactly what I was thinking 2 years ago and why my post was so terse - could be a lot of reasons behind that!

    There is no way to use glob() on an array.
    If I knew how to do that, I would have said so! The closest "glob like" solution is what ikegami pointed out in his also very terse post.
    use Text::Glob qw( match_glob glob_to_regex ); Of course those functions are not glob() itself. This module generates a regex "under the covers" and then uses it.

    My post shows the block form of grep, which I prefer. I guess I could have said things like this eliminates a module dependency and will execute faster. I could have also said: "When you think about a subset of an array, think grep. grep could have been called 'filter'". If you are having trouble with simple regexes, we can help you with that. For some reason, I didn't say any of that. I just showed how to do it with grep in a very terse post.

    Very few, if any Monk posts are destined to be included in the tomes of Western Literature.
    From the comment to my post, other Monks did examine and review what I said at the time. And I made adjustments to their messages with appropriate credit given.
    I find your critique of my post odd because you aren't asking about a clarification of details that you plan to use yourself in your own coding.

Re^3: glob an array?
by choroba (Archbishop) on Feb 14, 2021 at 21:45 UTC
    Watch for the replies to Perl Weekly Challenge 099/Task 1, it seems to be the same problem.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      What did I miss?

      use v5.12; use warnings; use Data::Dump qw/pp dd/; use Test::More; our $/="\n\n\n"; our @sets = <DATA>; for (@sets) { my ($example) = /^(Example \d*):/m; my ($input) = /^Input: (.*)$/m; my ($output) = /^Output: (.*)$/m; $input =~ s(\$P)(;\$P); my ($S,$P); eval $input; #pp [$S,$P]; my $re = glob2re($P); my $res = ($S =~ $re); is( 0+$res, $output, "$example $input"); } done_testing; sub glob2re { my ( $pat ) = @_ ; $_ = "\Q$pat"; s/\Q\*/\.\*/g; s/\Q\?/\./g; return qr/^$_$/; } __DATA__ Example 1: Input: $S = "abcde" $P = "a*e" Output: 1 Example 2: Input: $S = "abcde" $P = "a*d" Output: 0 Example 3: Input: $S = "abcde" $P = "?b*d" Output: 0 Example 4: Input: $S = "abcde" $P = "a*c?e" Output: 1

      C:/Perl_524/bin\perl.exe -w d:/tmp/pm/weekly_challenge.pl ok 1 - Example 1 $S = "abcde" ;$P = "a*e" ok 2 - Example 2 $S = "abcde" ;$P = "a*d" ok 3 - Example 3 $S = "abcde" ;$P = "?b*d" ok 4 - Example 4 $S = "abcde" ;$P = "a*c?e" 1..4

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        > What did I miss?

        It's possible to implement it without a regex.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: glob an array?
by LanX (Cardinal) on Feb 13, 2021 at 13:13 UTC
    You are complaining about a 2 year old post.

    If you think you can do better, then please show us!

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2021-05-07 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (91 votes). Check out past polls.

    Notices?