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

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 (Abbot) 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]
[1nickt]: Thank you haukex and hippo. I'm thinking of using IPC::System:: Simple::capture instead. But will go reread Hauke's treatise first :-)
[1nickt]: herveus is so weird. Love his reply in that SQLite thread! Much more effective warning system to have diverse monks debunk on technical merits...
[haukex]: IPC::System:: Simple does all the $? handling for you, and does it well
[erix]: weird problem btw (that SQLite question) - I asked astroboy to let us know if he finds the cause
[hippo]: The syntax is warning enough to me.
[1nickt]: hippo lately I can tell just by the fact that Anonymous posts a reply after some hours that it is more than likely That Monk.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-12-13 11:44 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (360 votes). Check out past polls.