Nesting while loop inside a while loop in cshell - while-loop

I need my code to read files that are numbered between 1 and 4000. It will then do something with the files, I am trying to break them up in blocks of 500 with the following.
#!/bin/tcsh
# x = 1
# looper = 1
while ($x < 3)
while ($looper < 500)
#filenumber = $x -1
#filenumber = $filenumber * 500
#filenumber = $filenumber + $looper
echo $filenumber
#looper += 1
done
#x += 1
done
I want this to count from 1 to 1000 in units of 500. However when I try this the script only counts to 500. Does anyone know why this is?
Thanks for your help

You need to initialize #looper = 1 inside the outer loop, otherwise it gets initialized only once, and starts the second iteration with the value 500.
# x = 1
while ($x < 3)
#looper = 1 <-- here
while ($looper < 500)
#filenumber = $x -1
#filenumber = $filenumber * 500
#filenumber = $filenumber + $looper
echo $filenumber
#looper += 1
done
#x += 1
done

The answer is that right beneath the #x +=1 line there needs to be a line resetting the $looper variable
#x += 1
#looper = 1
done
WHOOPS!!!

Related

How do I size and sapce

def function(foo):
print(foo)
``````python
def function(foo):
print(foo)
``````python
def function(foo):
print(foo)
this is what i needed and i appreciate it guys
For problems like these, try to associate the output with its indices (or in this case, current_row). Also, it seems like it's better to start from 0 for this one.
0: 1
1: 2 * 3
2: 4 * * * 5
3: 6 * * * * * 7
4: 8 * * * * * * * 9
max_row is 5, current_row is from 0 to 4.
There are max_row - current_row - 1 spaces on each row.
There are 2 * current_row - 1 stars on each row.
The numbers on the left is just twice current_row, or 2 * current_row. This is true for all except 0. We could use an if statement for that special case.
The numbers on the right is just left + 1, or 2 * current_row + 1.
space = '\t'
star = '*'
size = int(input("Enter number to make triangle: "))
def printRow(current_row, max_row) :
star_count = 2 * current_row - 1
line = space * (max_row - current_row - 1)
if current_row == 0 :
line += "1"
else :
line += str(2 * current_row) + space
line += (star + space) * star_count
if current_row > 0 :
line += str(2 * current_row + 1)
print(line)
if size <= 0 :
print("The value you entered is too small to display a triangle")
for i in range(0, size) :
printRow(i, size)
First of all you have to learn how to print python pyramid you can learn it from
This link , Then try to understand it after you can understand below code
space = '\t'
star = '*'
size = int(input("Enter number to make triangle: \n"))
def printRow(current_row, max_row) :
rPri = 0
lefNum = 0
star_count = 2*current_row - 3
if current_row == 1:
lefNum = 1
if current_row > 1:
lefNum = (current_row-1)*2
rPri = 1
rigNum = lefNum + 1
line = space * (max_row - current_row) + str(lefNum) + (space + star) * star_count + (space + str(rigNum))*rPri
print(line)
if size<=0 :
print("The value you entered is too small to display a triangle")
for i in range(1, size+1) :
printRow(i, size)

SQL Query To Find Proceeding Records

This is a hard question for me to verbalize. I'll do my best. This is my record set:
DealershipID PlacementDealershipID
40309 -1
787289 40309
787461 787289
787402 787461
787520 787402
You will notice that the top of the chain is ID 40309 that has a PlacementDealershipID of -1. If we start with ID 787520, the PlacementDealershipID goes "up" the chain, until we get to DealershipID 40309.
Desired results would look like this starting at DealershipID 787520.
Starting DealershipID ---->>> 787520
1 Level(s) Up ---->>> 787402
2 Level(s) Up ---->>> 787461
3 Level(s) Up ---->>> 787289
4 Level(s) Up ---->>> 40309
I want to print a list of DealershipID's in SQL in order of DealershipID from starting point to 40309. I have done this using a while loop and declared variables. (Code Below). Is there an easier way to do this with SQL built in functionality?
-- Loop and pull structure
set #PreceedingDealershipID = 999
set #LevelNumber = 1
set #ContinueLoop = 1
set #LoopCount = 0
-- Determin maximum level for this system
set #MaxLevel = 30
set #MaxLevel = coalesce(#MaxLevel,30)
PRINT N'Starting ID ---->>> ' + convert(varchar(40), #DealershipID);
while (#ContinueLoop = 1 and #PreceedingDealershipID <> -1 and #PreceedingDealershipID <> 0)
begin
set #LoopCount = #LoopCount + 1
if (#LoopCount > #MaxLevel)
begin
raiserror('THERE IS A LOOP IN THE DATABASE! CANNOT CONTINUE!',15,15)
return
end
SELECT #PreceedingDealershipID = d.PlacementDealershipID
FROM Sponsorship as d WITH (NOLOCK)
WHERE d.DealershipID = CASE WHEN #LevelNumber = 1 THEN #DealershipId ELSE #PreceedingDealershipID END
if (#PreceedingDealershipID <> 0 and #PreceedingDealershipID <> -1)
Begin
PRINT N'Level ' + convert(varchar(40), #LevelNumber) + ' Next Level Up ---->>> ' + convert(varchar(40), #PreceedingDealershipID);
set #LevelNumber = #LevelNumber + 1
End
end -- end main loop

For Loop running error

This code doesn't find the correct output
for say n= 1 (although it gives the correct output for say n= 2,3,4..etc.)
if we put n= 1 to find x then the i loop will continue from 1 to 0, hence the first term in x should vanish and leftover should be the second term 5; but it gives 0 ?
Is there any limitation on the input n to run the for loop ?I would appreciate any help.
Function math(n As Integer) As Double
Dim i As Integer
Dim x As Double
For i = 1 To n - 1
x = (n - 1) * 2 + 5
sum = sum + x
Next i
math = sum
End Function
Why not simply:
Function math(n As Integer) As Double
Math = ((n - 1) * 2 + 5) * Abs((n - 1) - (n = 1))
End Function
???
if the answer is correct then Math = (n * 2 + 3) * Abs((n - 1) - (n = 1)) would be easier to understand and make much more sense
In the for loop, if you don't precise the Step, the variable will only increment by 1.
And here, you start at 1 to go to 0, so the loop won't execute, you need to test n to cover both cases :
Function math(n As Integer) As Double
If n < 0 Then Exit Function
Dim i As Integer
Dim x As Double
Dim Summ As Double
Select Case n
Case Is > 1
For i = 1 To n - 1
x = (i - 1) * 2 + 5
Summ = Summ + x
Next i
Case Is = 1
Summ = (n - 1) * 2 + 5
Case Is = 0
Summ = 5
Case Else
MsgBox "This case is not supported", vbInformation + vbOKOnly
Exit Function
End Select
math = Summ
End Function
If n = 1, you end up with For i = 1 To 0 which is incorrect and
should be expressed For i = 1 To 0 STEP -1.
So I suggest you add the STEP BYand make sure it is either 1 to -1 depending on N.

For Next Loop - Increase Variable

I have a basic for next loop.
For x = 1 to 100
test = Test +110
if test > 500
counter = Counter +1
end if
MsgBox (thisisatest)
Next
What i'd like to do, increase thisisatest by 1 each time the counter increases. For example.
If counter = '1' it should be 'thisisatest1'
If Counter = '2' it should be 'thisisatest2'
If counter = '3' it should be 'thisisatest3'
For x = 1 to 100
test = Test +110
if test > 500
counter = Counter +1
end if
MsgBox (thisisatest+counter)
Next
you can increase your variable thisisatest by 1 each time the counter increases as below:
For x = 1 to 100
test = Test +110
if test > 500
counter = Counter +1
thisisatest &= x
end if
MsgBox (thisisatest)
Next

AutoIt - Split 1 variable into 4

I have one 4 digit variable i wish to split into 4 seperate variables fromt the range 0000 to 9999
Local $Data ="Element 1|Element 2|Element 3|Element 4"
Local $arr = StringSplit($Data, "|")
If IsArray($arr) Then
$Imax = Ubound($arr)
For $i = 0 to $Imax -1
Next
EndIf
This is what I got so far
I want it to do this:
Lets say the bigvar = 2345
$BigVar=2345 Then
$SmallVar1 = 2
$SmallVar2 = 3
$SmallVar3 = 4
$SmallVar4 = 5
Also the bigvar changes all the time so i need it to keep reading of that
LOL to all the overkill answers
#include <Array.au3>
Local $parts = StringSplit("1574", "")
_ArrayDisplay($parts)
String Approach
If you simply want to split it, you can go with
#include <Array.au3>
Func _Split($BigVar)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
Return $SmallVar
EndFunc
$Array = _Split("2345")
_ArrayDisplay($Array)
Now you can use
$Array[0] = 4 ;Amount of digits
$Array[1] = 2
$Array[2] = 3
$Array[3] = 4
$Array[4] = 5
If the Number might be 123 and you want to interpret it as 0123 therefore $SmallVar[1] being 0 not 1, this method might fit your needs:
#include <Array.au3>
Func _Split($BigVar, $Digits = 0)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
If $Digits = 0 Then Return $SmallVar
If $SmallVar[0] >= $Digits Then
For $i = 1 To $SmallVar[0] - $Digits
_ArrayDelete($SmallVar, $i)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndIf
For $i = 1 To $Digits - $SmallVar[0]
_ArrayInsert($SmallVar, 1, 0)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndFunc
$Array = _Split("123", 4) ;4 being the amount of digits
_ArrayDisplay($Array)
The code example above still works with this version, since digits is an optional parameter, and leaving it out, _Split will act as it did before.
Just use modulo 10 division to get the single integers.
#include <array.au3>
Global $BigVar=2345
Global $TmpVar=$BigVar
Global $aResult[StringLen(String($BigVar))]
For $i=UBound($aResult)-1 To 0 Step -1
$aResult[$i] = Int(Mod($TmpVar, 10))
$TmpVar /= 10
Next
_ArrayDisplay($aResult)
Now you got an array that has each number of the big integer stored in a separate field.