Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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]
[choroba]: Dangers of WFH: a mailman rings, brings a parcel for my wife, I need to go outside, take my son with me, sign a paper *and* into the postman's mobile app, get back. Guess what the soup was doing meanwhiles?
[Discipulus]: soup without heat protections as CPUs are doomed to burned..
[1nickt]: The dangers of milk-based soup, you mean?
[Corion]: choroba: Ouch! But you don't need that many external events for that. I have on several occasions set up milk to cook and then programmed a bit, only to find the milk burned and congealed over the stove :-/
[choroba]: it's still edible, just need to scour the pot
[1nickt]: Corion I have a large site I need to check for broken links and absolute links. Making a scraper is easy of course; a spider that crawls a whole site is a little more involved ... I was planning a queue-based tool. Intersted to see what you do...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-10-18 11:37 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.