Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

What Happened...(perils of porting from c)

by Madams (Pilgrim)
on Jan 05, 2001 at 17:30 UTC ( [id://50050]=perlquestion: print w/replies, xml ) Need Help??

Madams has asked for the wisdom of the Perl Monks concerning the following question:

Ok..trolling through my old journal mags i found a C replacement for nested for statements. So being a masochist i decided to port it to perl ... I'm getting wierd errors please take a look and see what's causing my camel to kick and spit...the original and the port are at:

My yahoo briefcase

if people can't access them i'll follow up with a full posting...in the port i added lotsa comments as i went so to keep me mindful of what i shoulda been doing...(pointer arithmetic & references & dereferences ... OH MY!)

especially the pointer arithmetic...i KNEW there was a reason i always felt pointer arithmetic was EVIL EVIL EVIL!

playing with your pointer like that can make you go blind (or at least crazy ;] )
  • Comment on What Happened...(perils of porting from c)

Replies are listed 'Best First'.
Re (tilly) 1 (perl): What Happened...(perils of porting from c)
by tilly (Archbishop) on Jan 05, 2001 at 23:43 UTC
    Here is a native Perlish thing that basically does what I think you really want. Note how much shorter this is than the C version.
    use strict; nested_for( sub {print "@_\n";}, [1..2], ['a'..'c'], ['A'..'D'] ); sub nested_for { ret_iter(@_)->(); } sub ret_iter { my $fn = shift; my $range = shift; my $sub = sub {$fn->($_, @_) for @$range}; return @_ ? ret_iter($sub, @_) : $sub; }
    Also note the lack of pointer arithmetic to mess up on. :-)
      Oh Wow!

      I bow to tilly!

      I am going to REALLY study this: it works! correctly! it's one-third the size! it looks tricky but not f**** insane! and NO POINTER ARITHMETIC!!!!

      This is the sort of answer that shows that i'm a tyro at perl AND why! I'm obviously still stuck in some sort of tunnel vision caused by programming in c/c++.

      To me this is a great Zen Koan to meditate on!

      Thanks tilly, and everyone who responded to this query!

      ---madams :) x $POSIX::LDBL_MAX
      That was beautiful. I'm writing primarily so more people can see it. I also did a little rewrite that isn't an improvement, per se, but is AWTDI and may help people understand what's going on. (Iterating instead of recursing.)
      sub ret_iter { my $sub = shift; while (my $range = pop) { my $inner = $sub; $sub = sub { $inner->(@_, $_) for @$range }; } return $sub; }
      I made it so it changes elements on the right fastest, like a counter.

      Caution: Contents may have been coded under pressure.
Re (tilly) 1: What Happened...(perils of porting from c)
by tilly (Archbishop) on Jan 05, 2001 at 18:09 UTC
    I went and looked.

    First of all I strongly suggest using strict up at the top, it will catch even more errors for you. Of course you need to use vars as well.

    After that fixing it to work is just a matter of walking through and fixing complaints. You have an extra ')' before a ||. One-line for loops need to either change the format or need the braces. There are a few undeclared variables. A few more cases where // is used instead of # for a comment. A few $ missing. (ie You want --$level rather than --level.) Your final return statement should not be there.

    All of this is pretty mechanical, but I am loathe to post the final result since the resulting code is very ugly. A native Perl version of this would be easy to write and would look much nicer. Perhaps if I get some time...

      Hey thanx for the critique ...

      Added "use strict" , switched to a perl aware editor (boy that caught alot just switching editors)(changed @curlevel to $curlevel..the GOOD editor caught that)

      split up the stmt that had the "||" in it (that somehow fixed that), fixed the munged comments, and that oneline for loop. Now no syntax errors (yeah!)

      Mostly i am looking for someone to tell me if i got the pointer arithmetic related crap ported right .. when i use the module i'm getting some nonsense output (examples are uploaded to here).

      One thing why get rid of the "return 1;" at the end? every example of writing a module says it should return "1" to signal proper loading.
        The reason for removing the return statement is that you are not supposed to use that outside of functions (eval, do).

        Just end with:

        1;
        and it will work.

        As for the pointer logic, sorry. No energy to go through it. It looked like a mess FWIW. I would just use recursion for this problem. (OK, so you take a performance hit.)

Re: What Happened...(perils of porting from c)
by jynx (Priest) on Jan 06, 2001 at 02:42 UTC
    i have to make a disclaimer:
    i understand what your code is trying to do according to what the english says it is doing, but some of your pointer derefs i'm not certain i follow. Such as the following:
    #oops...pointer arithmetic.. #curlevel = curvals + level; $curlevel = $level; # shouldn't this be more like: $curlevel = @$curvals + $level; # or some such? i'm not entirely certain as to why curvals got dr +opped here...
    All i did with your code is a more perl-esque rewrite dropping out variables that aren't needed (or aren't used properly) and streamlining here and there. Also, i took out the infinite for loop since it seemed there are better ways to check for what you're doing. This could be not what you're looking for at all, but it seems to be what you have written already, just simplified.

    NOTE: there is no error checking for incoming variables in this version.

    #!/usr/bin/perl -w package forFun; use strict; use Exporter; @ISA = qw(Exporter); @EXPORT = qw(&ForFun); sub ForFun { my ($startval, $endval, $workfunc) = @_; my ($level, $curvals) = (0, []); push @$curvals, $_ foreach (@$startval); while ($level > -1) { $level = @$startval - 1; while ($curvals->[$level] <= $endval->[$level]) { &$workfunc($curvals); ($curvals->[$level])++; } $level--; while (++($startval->[$level]) > $endval->[$level]); last if (--$level < 0); } } return; } 1;
    i tried to follow the pointer logic and your codes' logic as well as possible. i'd definitely suggest going with tilly's code, as it seems much more adaptable and extensible.

    Hope this helps,

    jynx

    ps the code above is untested but should be able to work pretty much as is...

Re: What Happened...(perils of porting from c)
by Madams (Pilgrim) on Jan 05, 2001 at 18:04 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://50050]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-18 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found