Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^5: Working with Binary Numbers

by ikegami (Patriarch)
on Sep 25, 2007 at 15:19 UTC ( [id://640966]=note: print w/replies, xml ) Need Help??


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

If bash had any bearing in the matter — glob is based on csh's expansion mechanism — it seems to me you just disproved yourself. "the filenames generated need not exist."

Replies are listed 'Best First'.
Re^6: Working with Binary Numbers
by salva (Canon) on Sep 25, 2007 at 15:36 UTC
    it seems to me you just disproved yourself.

    yes, that was my intention :-)

    I was not trying to maintain my position but just to follow the "The documentation I could find isn't great on this point" on blokhead response.

    That's also why I used bash docs instead of csh, because they looked more clear to me and with regard to brace expansion, both shells behave the same.

      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!

        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

Log In?
Username:
Password:

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

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

    No recent polls found