Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

RE: my first try: japh

by tedv (Pilgrim)
on Nov 08, 2000 at 23:09 UTC ( #40600=note: print w/replies, xml ) Need Help??

in reply to my first try: japh

You might want to try using "special" variables to make your life easier and/or more obfuscated. For example, don't use @d. Use @_ instead. This means you can write shift instead of shift(@d). You can use $/, the newline separator, instead of \n. Similar to @_, you could use $_ instead of $d which gets you tighter regular expressions. And lastly, instead of making your for loop work like C code (for ($n=1;$n < 21; $n++)), you might try something like for $n (1..@_). Note that @_ is forced into scalar context, which translates to the length, which is 20.

Here is what your code might turn into after these changes:
#!/usr/bin/perl @_=(255,60,252,195,24,195,195,195,24,255,252,255, 216,195,192,195,112,195,192,195);for $n(1..@_){ $_ .=' '.unpack("B8",pack("v",shift));unless($n%4) {s/0/ /g;print $_,$/;$_='';}}

Other thoughts include using the list nature of pack to simplify the for loop. Also, the numbers 255, 195, and 192 come up multiple times in the data block. You might want to start with a smaller data block and graft multiple copies of those numbers into it. It might result in smaller code.


Replies are listed 'Best First'.
RE: RE: my first try: japh
by zzspectrez (Hermit) on Nov 09, 2000 at 10:01 UTC

    Cool!! Thanks for the help!! I tried using $_but then added some other bug when I did. I was unaware of both @_ and $/, so thank you for my introduction.

    When you using the construct for $n (1..@_) is this working like a foreach construct?? $n equals each item in the list i.e. 1..@_??

    One minor correction to the code, though.. I tried the moddifications and it didnt work properly unless
    pack("v",shift) is changed to

    As far as using the list nature of pack, Im not sure I would know how to go about it. This was the hardest part for me to figure out. I dont completly understand how to use pack or unpack properly. I orginaly kept pulling my hair out thinking that unpack("B8",shift (@D)) should work. I didnt realize that I need to pack the binary string into actual binary data.

    Thanks a lot for the suggestions!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-08-21 11:57 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (198 votes). Check out past polls.