Jump to content
Sign in to follow this  
sree161

Help needed in For loop

Recommended Posts

sree161

I need to run For loop until it has no lines to produce as output. i tried the below code and unable to get output.
For $l=1 To @error Step 1
    $Out = "$Sp[" & $l & "]:" & $Sp[$l]
    MsgBox($MB_SYSTEMMODAL , "Split", $Out )
    Next

 

Can anyone help me on this??
Thanks in advance.

Share this post


Link to post
Share on other sites
czardas

Has an error been thrown? If not then the loop will run 0 times because the start is greater than the end ==> @error = 0.

Share this post


Link to post
Share on other sites
sree161

Local $Input = _
        "sajfkjdsaf" & @CRLF & _
        "name" & @CRLF & _
        "ajhsdfkajfdajsjdflkjafa" & @CRLF & _
        "afdhjsahlfkaf" & @CRLF & _
        "ajfkhdsakfasf" & @CRLF & _
        "akjfkjafd" & @CRLF & _
        "name" & @CRLF & _
        "asfjksajf" & @CRLF & _
        "kdajsfpa" & @CRLF & _
        "alkdshfahsf" & @CRLF & _
        "nameajslkfja" & @CRLF & _
        "aslkfajnbfajn"
        $Sp = StringSplit ( $Open , "name" , $STR_ENTIRESPLIT )
        For $l=1 To @error Step 1
    $Out = "$Sp[" & $l & "]:" & $Sp[$l]
    MsgBox($MB_SYSTEMMODAL , "Split", $Out )
    Next

This is my whole pgm and i am not getting any output......can i know the reason where i went wrong??

 

1 hour ago, czardas said:

Has an error been thrown? If not then the loop will run 0 times because the start is greater than the end ==> @error = 0.

 

Share this post


Link to post
Share on other sites
czardas

Is this what you want?

#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

Local $Input = _
    "sajfkjdsaf" & @CRLF & _
    "name" & @CRLF & _
    "ajhsdfkajfdajsjdflkjafa" & @CRLF & _
    "afdhjsahlfkaf" & @CRLF & _
    "ajfkhdsakfasf" & @CRLF & _
    "akjfkjafd" & @CRLF & _
    "name" & @CRLF & _
    "asfjksajf" & @CRLF & _
    "kdajsfpa" & @CRLF & _
    "alkdshfahsf" & @CRLF & _
    "nameajslkfja" & @CRLF & _
    "aslkfajnbfajn"
    $Sp = StringSplit ($Input , "name" , $STR_ENTIRESPLIT )

For $l = 1 To $Sp[0] Step 1
    $Out = "$Sp[" & $l & "]:" & $Sp[$l]
    MsgBox($MB_SYSTEMMODAL , "Split", $Out )
Next

 

Share this post


Link to post
Share on other sites
sree161
5 minutes ago, czardas said:

Is this what you want?

#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

Local $Input = _
    "sajfkjdsaf" & @CRLF & _
    "name" & @CRLF & _
    "ajhsdfkajfdajsjdflkjafa" & @CRLF & _
    "afdhjsahlfkaf" & @CRLF & _
    "ajfkhdsakfasf" & @CRLF & _
    "akjfkjafd" & @CRLF & _
    "name" & @CRLF & _
    "asfjksajf" & @CRLF & _
    "kdajsfpa" & @CRLF & _
    "alkdshfahsf" & @CRLF & _
    "nameajslkfja" & @CRLF & _
    "aslkfajnbfajn"
    $Sp = StringSplit ($Input , "name" , $STR_ENTIRESPLIT )

For $l = 1 To $Sp[0] Step 1
    $Out = "$Sp[" & $l & "]:" & $Sp[$l]
    MsgBox($MB_SYSTEMMODAL , "Split", $Out )
Next

 

What if i want to increase my $l count till end. How to terminate loop when there is no output to display??
That is where i get stuck?
All the sample programs are going descending in For loop, but i want a ascending loop from 1 to some x...i want the loop to be terminated when there is no output to display.
In above program u are going descending.

Share this post


Link to post
Share on other sites
czardas

It does stop when it reaches the array element/item count at $Sp[0] : which is 4 in your example. You can reverse the order by starting at the end instead - negative step value. So you can loop whichever direction you want.

For $l = $Sp[0] To 1 Step -1
    $Out = "$Sp[" & $l & "]:" & $Sp[$l]
    MsgBox($MB_SYSTEMMODAL , "Split", $Out )
Next

In fact 'Step 1' is never needed and can be removed from the first example.

Also you could split the string differently to produce more array elements (using a different delimiter). Try it!

$Sp = StringSplit ($Input, @CRLF, $STR_ENTIRESPLIT)

I recommend you look at _ArrayDisplay(). It will help you to understand how arrays work.

Edited by czardas
  • Like 1

Share this post


Link to post
Share on other sites
jdelaney

You can use:

While Not @error
 SomeFunctionThatReturnsError()
Wend

Or

While True
  SomeFunctionThatReturnsError()
  If @error Then ExitLoop
Wend

 


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
sree161

Thanks fr ur help....i have solved this issue

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.