Sign in to follow this  
Followers 0
sree161

Help needed in For loop

8 posts in this topic

#1 ·  Posted

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



#2 ·  Posted

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

#3 ·  Posted

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

#4 ·  Posted

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

#5 ·  Posted

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

#6 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted

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

1 person likes this

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  
Followers 0