For Loop

S

shapper

Hello,

I don't know PERL and I need to convert a simple code to C# or VB.NET.

Could someone, please, help me with the conversion?
A description of how the loop works would be enough.

for $row (1..$h)
{ for ($s=0,$i=$row; $s<$c; $i+=($h+($s<$rm)), ++$s)
{ printf "%7u", $i }
print "\n";

}

++$h;
for ($s=0,$i=$h; $s < $rm; $i+=$h, ++$s)
{ printf "%7u", $i }

Thanks,
Miguel
 
P

Paul Lalli

I don't know PERL

Perl. Not PERL.
and I need to convert a simple code to C# or VB.NET.

Could someone, please, help me with the conversion?
A description of how the loop works would be enough.

for $row (1..$h)

This will loop through the numbers 1 through $h, incrementing by 1
each time, and each iteration setting $row to be the current value.
So if, for example, $h was 5, this would loop five times, setting $row
to 1, 2, 3, 4, and 5, in turn.

{ for ($s=0,$i=$row; $s<$c; $i+=($h+($s<$rm)), ++$s)

This starts $s at 0 and $i at whatever $row is in this iteration. It
continues to loop so long as $s is less than $c. At the end of each
iteration, it increments $s by 1, and adds $h to $i. If $s is less
than $rm, it will add an aditional 1 to $i.
{ printf "%7u", $i }

This prints the current value of $i, padded to 7 spaces.
print "\n";

This prints a newline.

Increment $h by one.
for ($s=0,$i=$h; $s < $rm; $i+=$h, ++$s)

Starts $s at 0, $i at $h. Loops while $s is less than $rm. At the
end of each iteration, adds $h to $i, and increments $s by 1.
{ printf "%7u", $i }

Prints the current value of $i, again padded 7 spaces.

You're welcome.

Paul Lalli
 
T

Tad McClellan

I don't know PERL


We can tell, because you did not spell "Perl" correctly. :)

and I need to convert a simple code to C# or VB.NET.

Could someone, please, help me with the conversion?
A description of how the loop works would be enough.


Descriptions of Perl syntax are in the perlsyn.pod documentation:

perldoc perlsyn

for $row (1..$h)


That one is covered in the "Foreach Loops" section.

{ for ($s=0,$i=$row; $s<$c; $i+=($h+($s<$rm)), ++$s)


That one is covered in the "For Loops" section.
 
D

Dr.Ruud

Paul Lalli schreef:
shapper:
{ for ($s=0,$i=$row; $s<$c; $i+=($h+($s<$rm)), ++$s)

[...] At the end of each
iteration, it increments $s by 1, and adds $h to $i. If $s is less
than $rm, it will add an aditional 1 to $i.

The behaviour of $s could well be undefined (un-guaranteed) there.
Because of the comma-operator, I would expect that the ++$s is done
*after* $i is updated.
 
S

shapper

Paul Lalli schreef:
[...] At the end of each
iteration, it increments $s by 1, and adds $h to $i. If $s is less
than $rm, it will add an aditional 1 to $i.

The behaviour of $s could well be undefined (un-guaranteed) there.
Because of the comma-operator, I would expect that the ++$s is done
*after* $i is updated.

Hi,

I tried to convert the code but until now I wasn't success ... I am
maybe doing something wrong here.

Does anyone knows what would be the equivalent code in C# or VB.NET?

Thanks,
Miguel
 
P

Paul Lalli

I tried to convert the code but until now I wasn't success ... I am
maybe doing something wrong here.

Undoubtedly you are, but you didn't show any code for anyone to tell
you *what* you're doing wrong.
Does anyone knows what would be the equivalent code in C# or
VB.NET?

You've been told what the Perl code does. If you don't know how to
translate the English algorithm into C# or VB.NET code, then you have
a C# and/or VB.NET problem, not a Perl problem. You are far more
likely to receive good answers to this query in a newsgroup devoted to
C# or VB.NET.

Paul Lalli
 
I

Ian Wilson

I tried to convert the code but until now I wasn't success ... I am
maybe doing something wrong here.

Does anyone knows what would be the equivalent code in C# or VB.NET?

I'd post in a C# newsgroup, quoting Paul's explanation of the Perl, also
cut & paste the C# you have written, state what output you get, state
why you think the output is wrong and ask for suggestions.

I'd try newsgroup microsoft.public.dotnet.languages.csharp.
 
S

Skye Shaw!@#$

Hello,

I don't know PERL and I need to convert a simple code to C# or VB.NET.

I'm sorry
Could someone, please, help me with the conversion?
A description of how the loop works would be enough.
for $row (1..$h)
{ for ($s=0,$i=$row; $s<$c; $i+=($h+($s<$rm)), ++$s)
{ printf "%7u", $i }
print "\n";

}

++$h;
for ($s=0,$i=$h; $s < $rm; $i+=$h, ++$s)
{ printf "%7u", $i }


for $row (1..$h)
For Row=1 To H 'VB
for( int row=1; row<h; row++ ) //C#

printf "%7u", $i
Console.WriteLine("{0:D7}",i) //.NoT
 
B

Brad Baxter

I'm sorry


for $row (1..$h)
For Row=1 To H 'VB
for( int row=1; row<h; row++ ) //C#

printf "%7u", $i
Console.WriteLine("{0:D7}",i) //.NoT

for( int row=1; row<=h; row++ ) //C#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top