AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Append next line in a parsing loop

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Mon Nov 26, 2007 2:22 pm    Post subject: Append next line in a parsing loop Reply with quote

Hi,

I've read a file into a variable (CurrentFile), and am using a parsing loop to process each line of the file. As part of the processing, I want to append the next line from CurrentFile to the current line, without using Continue to jump back to the top of the parsing loop.
Code:
Loop, parse, CurrentFile, `n
{
  step 1
  step 2
  ...
  step n
    append the next line in CurrentFile to the current line (Line`nNextLine)
    process the combined lines
  step n+1
  ...
}

I want the next iteration of the parsing loop to start on the line after the one I appended.

I haven't found any way to get the current position of the parsing loop, so I don't know how to tell it to get the next line and append it to the current line. Is there an easy way to do this inside a parsing loop?

Thanks,
John B.
Back to top
View user's profile Send private message
wOxxOm



Joined: 09 Feb 2006
Posts: 326

PostPosted: Mon Nov 26, 2007 3:03 pm    Post subject: Reply with quote

maybe
Code:
bJoin=
loop, parse, CurrentFile,`r,`n
{
   IfNotEqual,bJoin,
   {   bJoin=
      CurLine=%CurLine%`n%A_LoopField%
   }
   ;.........
   if (NeedToJoin)
   {   bJoin=1
      CurLine=%A_LoopField%
      continue
   }
   ;Process CurLine instead of A_LoopField
   ;........
}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Mon Nov 26, 2007 3:21 pm    Post subject: Reply with quote

Hi wOxxOm,

That's what I originally considered, but it has to be done without using "Continue".

Is there any way to access and increment the index/pointer in a parsing loop? If not, then maybe reading the lines into an AHK pseudo-array is the answer?

Thanks,
John B.
Back to top
View user's profile Send private message
wOxxOm



Joined: 09 Feb 2006
Posts: 326

PostPosted: Mon Nov 26, 2007 3:29 pm    Post subject: Reply with quote

no, there isn't. What's wrong with continue?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Mon Nov 26, 2007 3:40 pm    Post subject: Reply with quote

x0xx0m wrote:
Quote:
no, there isn't. What's wrong with continue?


The test for needing to append the next line comes in the middle of the processing (as shown in the original post).

1. After appending the next line, I want to keep processing the combined line down to the end of the loop. There are a number of steps after the append.

2. I don't want to process the combined line with the part of the loop above the text step where I append the next line. If I use a continue, the processing jumps back to the beginning of the loop.

Thanks,
John B.
Back to top
View user's profile Send private message
wOxxOm



Joined: 09 Feb 2006
Posts: 326

PostPosted: Mon Nov 26, 2007 3:56 pm    Post subject: Reply with quote

just add 'else' part with parentheses
Code:

bJoin=
loop, parse, CurrentFile,`r,`n
{
   IfNotEqual,bJoin,
   {   bJoin=
      CurLine=%CurLine%`n%A_LoopField%
   }
   else
   {
      ;.........
      if (NeedToJoin)
      {   bJoin=1
         CurLine=%A_LoopField%
         continue
      }
   }
   ;Process CurLine instead of A_LoopField
   ;........
}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Mon Nov 26, 2007 4:16 pm    Post subject: Reply with quote

So it would look like this?

Code:

bjoin = false
Loop, parse, CurrentFile, `n
{
  If bjoin = false
  {
    step 1
    step 2
    ...
    step n     ; test for need to join
    If need to join
    {
      bJoin=true
      CurLine=%A_LoopField%
      continue
     }
  }
    step n+1
    ...
    bjoin = false
}


I think this would work if I had only one instance of needing to join lines, but there are multiple instances, and in each case, I'd have to add another if condition for bjoin1, bjoin2, bjoin3, bjoin4.... It rapidly gets unmanageable. So again, I'm looking for a method that doesn't use "continue".

Thanks,
John B.
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Mon Nov 26, 2007 8:27 pm    Post subject: Reply with quote

just a thought, but have you considered using [StringSplit] on your input file and having a normal [loop] iterate through the array elements ?

With that method, you'd have more control over which element gets accessed next.
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Mon Nov 26, 2007 8:50 pm    Post subject: Reply with quote

Hi [VxE],

Yes, I thought about using an array for just that reason, although I was going to use a loop to build the array. If I go with the array, your suggestion of using StringSplit to create the array seems the easiest way.

Thanks,
John B.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Mon Nov 26, 2007 10:02 pm    Post subject: Reply with quote

a stringsplit and a loop of the stringsplit results is the same as a loop, parse.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Tue Nov 27, 2007 12:27 am    Post subject: Reply with quote

Hi engunneer,

You wrote:
Quote:
a stringsplit and a loop of the stringsplit results is the same as a loop, parse.


The difference, I'm hoping, is that a stringsplit results in an "array", which would allow me to address individual lines (so I can append the next line to the current line and increment the array index without using "continue"). If I use loop, parse, I haven't found any way to append the next line to the current line without iterating through the loop again.

Right?

Thanks,
John B.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Tue Nov 27, 2007 12:29 am    Post subject: Reply with quote

you are correct. I didn't read closely enough
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group