AutoHotkey Community

It is currently May 26th, 2012, 1:36 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: December 3rd, 2008, 9:03 am 
Offline

Joined: February 5th, 2007, 3:37 pm
Posts: 31
Hi,

I have a text file with 30 lines. I want line 1-10 into variable1, line 11-20 into variable2 and line 21-30 into variable3
Is there any short solution for this without the need of a big function call ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 9:19 am 
shouldnt be that hard
just use loop read line and output as line_array
then declare:
var1 = %line_1%`n%line_2%`n .......%line_10%
var2 = %line_11%`n%line_12%`n .......%line_20% ...etc


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 9:20 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
One method:
Code:
file= ; replace by fileread
(
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
)
start1=1
end1=3
start2=4
end2=6
start3=7
end3=9

Loop, Parse, File, `n
   {
   if A_Index between %start1% and %end1%
      Var1 .= A_LoopField "`n"
   if A_Index between %start2% and %end2%
      Var2 .= A_LoopField "`n"
   if A_Index between %start3% and %end3%
      Var3 .= A_LoopField "`n"
   }
   
MsgBox % Var1   
MsgBox % Var2   
MsgBox % Var3

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 9:58 am 
Offline

Joined: February 5th, 2007, 3:37 pm
Posts: 31
I was close to your solution HugoV, but couldn't figure out
Code:
.= A_LoopField "`n"

Many thanks to both :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 10:26 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Code:
Var1 := Var1 . A_LoopField . "`n"
would also work

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 1:55 pm 
Offline

Joined: July 18th, 2006, 12:18 pm
Posts: 403
easy.

Code:
filename := "file.txt"
count = 0
index = 1

FileRead, content, %filename%
Loop, parse, content, `n, `r
{
if(count == 10)
{
   index++
   count = 0
}
filepart%index% := filepart%index% . A_LoopField . "`n"
count++
}

Loop, %index%
   Msgbox % filepart%A_Index%


replace your filename in the filename variable. the second loop is for debugging only you can remove that after, and access the parts of the file with "filepart" filepart1 contains first 10 lines. filepart2 contains next 10 lines.. etc.. wil work on files with hundreds of lines


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Newl, patgenn123, poserpro, Yahoo [Bot] and 17 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