AutoHotkey Community

It is currently May 27th, 2012, 1:29 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: July 3rd, 2005, 5:00 pm 
Offline

Joined: May 18th, 2004, 7:25 am
Posts: 7
I'm trying to figure out how to switch lines in a text file. For example:

INPUT TEXT:
Chicago
Illinois
Denver
Colorado
Miami
Florida
Atlanta
Georgia

OUTPUT TEXT:
Illinois
Chicago
Colorado
Denver
Florida
Miami
Georgia
Atlanta

_________________
Must nothing. There's always another way.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 3rd, 2005, 5:05 pm 
Offline

Joined: March 2nd, 2004, 10:10 pm
Posts: 443
Location: SLC, Utah
There are multiple ways to do it, the question is your output in a predetermined order? Because it looks just random.

thanks,
beardboy


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 3rd, 2005, 8:57 pm 
Offline

Joined: February 11th, 2005, 6:31 am
Posts: 174
Location: Germany
One idea to switch the elements is the following...

Code:
Loop, read, %A_ScriptDir%\InputTest.txt
  {
    ++CountIndex ;Var that counts the lines
    If Not Mod(CountIndex, 2) ;If the line is even: 2, 4, 6 etc.
      ;write in the var output the contents of the even line and the odd line (switched)
      Output = %Output%%A_LoopReadLine%`n%ReadLine%`n
    Else
      ReadLine = %A_LoopReadLine% ;If the line is odd: 1, 3, 5 etc. save it contents in the var Readline
  }

;Insert the next two lines, when in case of an odd number of elements the last one should copied
If Mod(CountIndex, 2)
   Output = %Output%%ReadLine%`n

FileAppend, %Output%, %A_ScriptDir%\OutputTest.txt


Here some explanation:

Mod(CountIndex, 2) retrieves 0 or 1 if the line is even or odd.
When the row number is divided by 2, the remainder is always 0 when it's even or 1 when it's odd. Then comes a trick: The value 1 stands for true and 0 for false in the if statement.

for the mod() function see helpfile : http://www.autohotkey.com/docs/Functions.htm


Last edited by Andi on July 4th, 2005, 8:04 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 3rd, 2005, 9:46 pm 
Offline

Joined: May 18th, 2004, 7:25 am
Posts: 7
Hey Beard. What I wanted was to switch a pair of related lines. So lines 1 & 2 which is Chicago & Illinois is the city and state. Likewise with lines 3 & 4 as well as lines 5 & 6 being city and state. I have a list of just above 500 such related lines that I need to switch around. It'll be too tedious to do manually. I haven't tried Andi's code yet. Actually I'm scratching my head right now just trying to figure out your code Andi. I can tell you that it would have taken me some time to figure it out. Let me try & I'll get back to you guys.

_________________
Must nothing. There's always another way.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 3rd, 2005, 11:31 pm 
Offline

Joined: May 18th, 2004, 7:25 am
Posts: 7
Hey the code work Andi. Thanks a bunch. Though I'm still scratching my head :?: :?:

_________________
Must nothing. There's always another way.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, chaosad, oldbrother and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group