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

hi, i'm translating a perl code into c# and i've a problem with a strange "for" statement.i'dont how to translate it... could someone explain me what this "for" statement do and traduce it in a traditional "for" statement ( "for ($i=0,$i<=integer;$i++)")
before showing the problem
$tabs => each elements of $tabs looks like [ [integer,integer,integer] ,integer,integer]

here is the "for" statement
for ( my $L = $tabs ->[10] ; $L; $L = $L->[0] ) { $m->[ $L->[1] ] = $L->[2]; }
thanks for your help

Replies are listed 'Best First'.
Re: strange "for " statement
by Corion (Patriarch) on Oct 24, 2003 at 12:58 UTC

    The for statement you see here is the C-style for statement, so its translation to C should be fairly trivial (please note that I don't program C, I only know how to read it - I'm not sure with the pointer syntax, as on which side the asterisk goes when, and I don't know whether C '99 introduced the arrow syntax for dereferencing...):

    int *L; /* Aaah, C, the language where an int is a pointer and a point +er is an int */ int *m; for (L = *tabs[10]; L; L = L*) { *m[ *L[1] ] = *L[2]; }; /* But I think this should be clearer : */ int *L; /* Aaah, C, the language where an int is a pointer and a point +er is an int */ int *m; for (L = *tabs[10]; L; L = L*) { int index = *L[1]; int value = *L[2]; *m[ index ] = value; };
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: strange "for " statement
by dragonchild (Archbishop) on Oct 24, 2003 at 14:14 UTC
    This is very similar to the for-loops used in C to handle linked lists. IIRC, the syntax would look something like:
    for (temp = head; temp; temp = temp->next) { // Do stuff here with temp }

    For-loops only going from 0->N is actually a common, but limited subset of the for-loop. Remember, for-loops and while-loops are interchangeable. The above for-loop would be written as a while-loop as such:

    temp = head; while (temp) { // Do stuff here with temp temp = temp->next; }

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: strange "for " statement
by castaway (Parson) on Oct 24, 2003 at 13:33 UTC
    It looks like this for-statement is working recursively down a array-of-arrays-of-arrays-... type thing.. Thus there is no way to turn it into a 'traditional' 0->N for-loop.

    I could turn it into a while statement for you, maybe that helps:

    my $L = $tabs->[10]; while ($L) # ie, as long as $L has a value { $m->[ $L->[1] ] = $L->[2]; # assign the 3rd value referenced b +y $L to the arrayref $m # (indexing with the 2nd value refe +renced by $L) $L = $L->[0]; # assign $L the first value of the +arrayref its referencing # (or undef if not available) }
    No idea how C# would do this though..

    C.

Re: strange "for " statement
by NetWallah (Canon) on Oct 24, 2003 at 17:28 UTC
    I don't think c# has pointers, so corion's code is probably unusable.

    I believe you CAN do references in c#, so that is probably the way to go.

    Since this is a perl forum, we will leave you the exercise of figuring out the c# code.