AutoHotkey Community

It is currently May 24th, 2012, 2:01 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: August 19th, 2008, 8:43 am 
Offline

Joined: August 19th, 2008, 8:36 am
Posts: 3
I have a problem . I have data in text file and I read all line in text but i don't want first line and last line how can I do sir.
thanks :D

[ Moderator!: subject line altered from: Hi there I need help sir.. ]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 19th, 2008, 9:35 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
Code:
FileRead, DataText, C:\SomeFile.txt
DataText := RegExReplace(DataText, "(^.*?\R|\R[^\n\r]*$)")


Reads file and removes first + last line :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 19th, 2008, 9:57 am 
UselessSubjectLineAlert :x


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 19th, 2008, 11:41 am 
Offline

Joined: August 19th, 2008, 8:36 am
Posts: 3
Thank you Krogdor....
But i still have
Code:
                      <--blank line
data
data
data


How can i do?

Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 19th, 2008, 1:16 pm 
Offline

Joined: August 19th, 2008, 8:36 am
Posts: 3
i'm solved it by use this..
Code:
 RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")


^^


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 9th, 2009, 6:50 am 
Online

Joined: April 27th, 2008, 5:28 pm
Posts: 489
poopup wrote:
i'm solved it by use this..
Code:
 RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")


^^
How can this be altered to only delete the last line of text?

DataLife

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 9th, 2009, 12:26 pm 
poopup wrote:
i'm solved it by use this..
Code:
 RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")


^^

That might be a good solution but if you have a large number of lines in the file, you will find that using ahk's string commands and functions much faster.
By replacing the above regex
Code:
Trimmed1 :=  RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")
with
Code:
 StringGetPos,lastNewLine, DataText,`n,r,1
 Trimmed1 := SubStr(DataText,firstNewLine := Instr(DataText,"`n")+1,lastNewLine-firstNewLine)
you will find that for small files (10 lines) using regex is 2 times slower that string commands. For files containing more lines (or longer lines), regex is about 80 times slower than string commands. While the code below is not a thorough benchmark, run it several times to see the speed advantage of the solution using ahk's string commands.
Code:
#SingleInstance, force
SetBatchLines, -1

lines = 10
header = results for %lines% lines of data`n`n
loop, %lines%            ; create test data set
  DataText .= "line " A_Index "`r`n"
msgbox, Input test data`n`n>>%DataText%<<
gosub, compareTime
msgbox, First and last line trim %header%string result`n`n>>%Trimmed1%<<`n`n`n`nregex result`n`n>>%Trimmed2%<<

result = Time %header%%Time_strings% seconds using string functions`n%Time_regex% seconds using regex
result .= "`n`nRegex Time / String Time = " Time_regex / Time_strings
lines = 10000
msgbox, %result%`n`n`nPress OK to start %lines% line test

DataText =
header = Results for %lines% lines of data`n`n
loop, %lines%            ; create test data set
  DataText .= "line " A_Index "`r`n"
;msgbox, data set`n`n>>%data%<<
gosub, compareTime
result = Time %header%%Time_strings% seconds using string functions`n%Time_regex% seconds using regex
result .= "`n`nRegex Time / String Time = " Time_regex / Time_strings
msgbox, %result%
ExitApp

compareTime:
; get time for data trim using string instructions
  T()   ; start timer
  StringGetPos,lastNewLine, DataText,`n,r,1
  Trimmed1 := SubStr(DataText,firstNewLine := Instr(DataText,"`n")+1,lastNewLine-firstNewLine)
  Time_strings := T() ; get elapsed time

; get time for data trim using regex instructions
  T()   ; start timer
  Trimmed2 :=  RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")
  Time_regex := T() ; get elapsed time
  return

T() {
    Static freq, last_count
    if !freq
        DllCall("QueryPerformanceFrequency","int64*",freq)
    DllCall("QueryPerformanceCounter","int64*",count)
    return (count-last_count)/freq, last_count:=count
}
You can see a more thorough benchmark comparing string commands and regex in this post TIP - When to NOT use RegEx


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 5:07 pm 
Online

Joined: April 27th, 2008, 5:28 pm
Posts: 489
SpeedFreak wrote:
Code:
 StringGetPos,lastNewLine, DataText,`n,r,1
 Trimmed1 := SubStr(DataText,firstNewLine := Instr(DataText,"`n")+1,lastNewLine-firstNewLine)


How can I modify this to only delete the last line?

DataLife

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 5:47 pm 
DataLife wrote:
SpeedFreak wrote:
Code:
 StringGetPos,lastNewLine, DataText,`n,r,1
 Trimmed1 := SubStr(DataText,firstNewLine := Instr(DataText,"`n")+1,lastNewLine-firstNewLine)


How can I modify this to only delete the last line?

DataLife
@DataLife
Well I'm not sure how we are defining a line, I wrote the above script to duplicate the final regex solution that poopup used for his data (and to show the speed advantage of ahk's string functions). But if your data looks like this:
Code:
  .
  .
 line#x-2`r`n
 line#x-1`r`n
 line#x`r`n

This code will delete line#x`r`n leaving all lines from the start of data through linex-1`r`n.
Code:
#SingleInstance, force

lines = 10
loop, %lines%                        ; create test data set
  DataText .= "line " A_Index "`r`n"
msgbox, Input test data`n`n>>%DataText%<<

StringGetPos,lastNewLine, DataText,`n,r,1
Trimmed := SubStr(DataText,1,lastNewLine)
msgbox, >>%Trimmed%<<
ExitApp


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 11:25 pm 
Online

Joined: April 27th, 2008, 5:28 pm
Posts: 489
Works great,

thank you very much
DataLife

_________________
Check out my scripts.
(MyIpChanger) (XPSnap) (SavePictureAs)

All my scripts are tested on Windows 7, AutoHotkey_L 32 bit Ansi unless otherwise stated.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Albireo, batto, fragman, jyloup, Pulover, Yahoo [Bot] and 22 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