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

Re: Working with Binary Numbers

by blokhead (Monsignor)
on Sep 24, 2007 at 21:09 UTC ( #640824=note: print w/replies, xml ) Need Help??

in reply to Working with Binary Numbers

Perl already has a nice built-in way to expand wildcards, it's called glob. It's just a simple matter of converting your wildcard syntax into one that glob recognizes.
my @data = qw( 000- 0101 011- 1-0- ); my @expanded = map { (my $s = $_) =~ s/-/{0,1}/g; glob($s) } @data;
Update: There is also an easier built-in way to convert them to integers from binary:
my @integers = map { oct "0b$_" } @expanded;


Replies are listed 'Best First'.
Re^2: Working with Binary Numbers
by akho (Hermit) on Sep 25, 2007 at 10:23 UTC
    I believe glob returns a list of filenames, not all possible combinations (which would be useless with * wildcards).

    Why does this work?

      This is the glob function. If you don't supply an EXPR to glob EXPR then it uses $_.
      His map makes @data become $_ in the glob scope.
      I think.

      Thanks Again!

      Update: removed my garbage...

      Read CountZero's much better/accurate explanation

Re^2: Working with Binary Numbers
by salva (Canon) on Sep 25, 2007 at 13:55 UTC
    It seems to me that you are abusing a bug in glob

    After expanding the pattern it should check that the entries actually exist on the file system and return only those that do.

      This rule does not apply to {x,y,z} alternations in the glob pattern. The documentation I could find isn't great on this point, and its language is generally in terms of filenames. But I'm 99% sure it's not a bug. I've seen tons of uses of glob in this way on PM!

      BTW, the same behavior should happen in your shell (though it may be a different glob under the hood).

      $ ls {x,y,z} ls: x: No such file or directory ls: y: No such file or directory ls: z: No such file or directory
      First, glob expands {...} and then it checks the filesystem to expand * and ? and the resulting pattern(s).
      $ ls *{p,q,r} ls: *p: No such file or directory ls: *q: No such file or directory ls: *r: No such file or directory


        That's what bash docs says about the matter:

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2021-03-01 17:07 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (15 votes). Check out past polls.