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

Re^7: Working with Binary Numbers

by shoness (Friar)
on Sep 25, 2007 at 17:52 UTC ( [id://641003]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Working with Binary Numbers
in thread Working with Binary Numbers

Okay, so it's clear I don't understand how blokhead is using glob in this clever way. If I had files in the current directory that looked like one of the bit-vector patterns ("0000", "1-01", etc.) would that break his/her scheme? Can I trouble someone to break down that magic line of his:
my @data = qw( 000- 0101 011- 1-0- ); my @expanded = map { (my $s = $_) =~ s/-/{0,1}/g; glob($s) } @data;
There is a CPAN module Text::Glob that looks like it does what this usage intends, without involving file globbing. The disadvantage is that it takes hundreds of lines to do what blokhead does in one line.

That's Perl though!
Thanks Again!

Replies are listed 'Best First'.
Re^8: Working with Binary Numbers
by CountZero (Bishop) on Sep 25, 2007 at 20:48 UTC
    Beginning with perl 5.6.0 glob uses File::Glob which despite its name is not limited to be used on files.

    Actually the argument to glob uses a little language of its own which describes the pattern to search for with meta-characters: (from the docs)

    \ Quote the next metacharacter [] Character class {} Multiple pattern * Match any string of characters ? Match any single character ~ User name home directory The metanotation a{b,c,d}e is a shorthand for abe ace ade. Left to rig +ht order is preserved, with results of matches being sorted separatel +y at a low level to preserve this order. As a special case {, }, and +{} are passed undisturbed.

    blokhead (ab)uses this pattern-description language by replacing each '-' with {1, 0} to give that placeholder a possible value of 1 OR 0.

    glob("1{1,0}1") will happily expand to 111 and 101, since unless you include a wildcard character (* or ?) or a character class in your pattern it will NOT go to the filesystem to give you back the list of files but rather expands immediately to all possible values which are allowed by the pattern. It seems to be a (useful) side-effect of the pre-filesystem lookup pre-processing of the pattern to expand {pat,pat,...} strings.

    Updated: as per tye's comments

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      glob(1{1, 0}1) will happily expand to 111 and 101

      To be clearer, make that glob("1{1,0}1") since my quotes and your space each make a big difference.

      - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-03-19 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found