[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Basic Question



On Thu, Sep 07, 2000 at 09:20:23PM -0400, Chip G. wrote:
> 
> <begin script segment>
> 
> for ($i=0; $i<=$#h; $i++) {
>    printf(DB "%c",$h[$i]);
> }
> 
> for ($i=0; $i<520; $i++) { printf(DB "%c%c", $erg[$i]/256, $erg[$i]%256); }
> 
> for ($i=0; $i<520; $i++) { printf(DB "%c%c", $fix[$i]/256, $fix[$i]%256); }
> 
> for ($i=0; $i<20; $i++) {
>    printf(DB "%s", $mann[$i]);
>    for ($j=0; $j<18-length($mann[$i]); $j++) { printf(DB "\0"); }
> }
> 
> for ($i=0; $i<52; $i++) {
>    printf(DB "%c%c%c%c", ($datum[$i]&0xff000000)>>24,
> 	($datum[$i]&0xff0000)>>16, ($datum[$i]&0xff00)>>8, $datum[$i]&0xff);
> }
> 
> close(DB);
> 
> <end script segment>

Yikes...  This code looks like it was written in C.
Here it is rewritten in Perl.  :)

print DB pack(('C' x @h) . 'n520n520' . ('a18' x 20) . 'N52',
              @h, @erg[0..519], @fix[0..519], @mann[0..19], @datum[0..51]);
# pack
#   as many characters as there are in @h
#   520 big-endian shorts from @erg and 520 from @fix
#   20 null-padded strings from @mann
#   52 big-endian longs from @datum


Of course, There's More Than One Way To Do It, and admittedly pack is one
of the least intuitive ways, but it makes the code so short.  :)


> Before this segment is a portion that appears to be writing using 
> hex, but I may be misreading it. I've studied enough of C/C++ to 
> recognize some of what is being done above, but I'm not sure I know 
> what the '$' or '#' in front of a variable mean.

Sounds like you need to learn Perl...

Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org