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 

Reverse lines in an `n variable or file
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
randallf



Joined: 06 Jul 2009
Posts: 678

PostPosted: Tue Jan 26, 2010 7:36 pm    Post subject: Reverse lines in an `n variable or file Reply with quote

Very simple but I thought I'd post it up as I couldn't find a simple example when I searched... as always I'd love to see better ways.

Code:
ReverseLines(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed := Reversed . "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
      
      a0--
   }

   Return Reversed

}
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Tue Jan 26, 2010 8:37 pm    Post subject: Reply with quote

http://www.autohotkey.net/~hugov/tf-lib.htm#TF_ReverseLines
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Thu Jan 28, 2010 9:32 am    Post subject: Reply with quote

simple attempt 1

Code:
var =
(Ltrim Join`n
  this
  is
  a
  test
)

msgbox % reverse(var)

reverse(var) {
   Loop,Parse,var,`n
      n := a_loopfield ( strlen( n ) ? "`n" : "" ) n
   return n
}


greets
dR
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Thu Jan 28, 2010 9:52 am    Post subject: Reply with quote

simple attempt II
Code:
var =
(Ltrim Join`n
   this
   is
   a
   test
)
msgbox % reverse2(var)
reverse2( a,b = "",c = "init" ) { ; adapted from sort help's last sample
   if ( c != "init" )
      return c
   else
      sort,a,% "F " A_ThisFunc
   return a
}


greets
dR
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
randallf



Joined: 06 Jul 2009
Posts: 678

PostPosted: Thu Jan 28, 2010 4:29 pm    Post subject: Reply with quote

Just for fun... I wrote a speed tester for these... I hope I did it right though it seems to work well!

With the current sets of variables it'll run for around 5 minutes. (100 runs of each of 3 functions at 1 second per run)

Updated a bit, totals aren't working exactly right (though it does report correctly per iteration) unf. I ran out of time to play with it Wink

Code:
;reverse lines speed tests

;_AHK
#SingleInstance FORCE

;VARS
TestDuration := 1000
Runs := 100

var =   
(Ltrim Join`n
  Line 000000000000000000000000000000 000000000000000000000000000000 000000000000000000000000000000
  Line 001001001001001001001001001001 001001001001001001001001001001 001001001001001001001001001001
  Line 002002002002002002002002002002 002002002002002002002002002002 002002002002002002002002002002
  Line 003003003003003003003003003003 003003003003003003003003003003 003003003003003003003003003003
  Line 004004004004004004004004004004 004004004004004004004004004004 004004004004004004004004004004
  Line 005005005005005005005005005005 005005005005005005005005005005 005005005005005005005005005005
  Line 006006006006006006006006006006 006006006006006006006006006006 006006006006006006006006006006
  Line 007007007007007007007007007007 007007007007007007007007007007 007007007007007007007007007007
  Line 008008008008008008008008008008 008008008008008008008008008008 008008008008008008008008008008
  Line 009009009009009009009009009009 009009009009009009009009009009 009009009009009009009009009009
)


;MAIN

MsgBox, OK to begin testing or PAUSE|BREAK to abort.

FileDelete, TestResults.txt

Loop, %runs%
{
   SetTimer, TestOver, -%TestDuration%
   TestRunning := 1
   RandallF_ReverseLines := 0
   While TestRunning
   {
      RandallF_ReverseLines(var)
      RandallF_ReverseLines++
   }

   TestRunning := 1
   DerRaphael_ReverseLines_1 := 0
   SetTimer, TestOver, -%TestDuration%
   While TestRunning
   {
      DerRaphael_ReverseLines_1(var)
      DerRaphael_ReverseLines_1++
   }

   TestRunning := 1
   DerRaphael_ReverseLines_2 := 0
   SetTimer, TestOver, -%TestDuration%
   While TestRunning
   {
      DerRaphael_ReverseLines_2(var)
      DerRaphael_ReverseLines_2++
   }

   Results = RandallF: %RandallF_ReverseLines%`nDerRaphael_1: %DerRaphael_ReverseLines_1%`nDerRaphael_2: %DerRaphael_ReverseLines_2%`n

   ;Totals
   DerRaphael_1tot := DerRaphael_ReverseLines_1+DerRaphael_1tot
   DerRaphael_2tot := DerRaphael_ReverseLines_2+DerRaphael_2tot
   RandallFtot := RandallF_ReverseLines+RandallFtot
   ;MsgBox % Results

   If A_Index = 1
   {
      FileAppend, Test Results of Reverse Line Order Iterations for Duration %Duration%`n, TestResults.txt
   }
   FileAppend, %Results%, TestResults.txt
} ;loop 100

Tot = TOTALS: RandallF: %RandallF_ReverseLines%`nDerRaphael_ReverseLines_1: %DerRaphael_1tot%`nDerRaphael_ReverseLines_2: %DerRaphael_2tot%`n

FileAppend, %tot%, TestResults.txt

Run, TestResults.txt

ExitApp ;Return


;LABL
TestOver:
TestRunning := 0
Return

;FUNC
DerRaphael_ReverseLines_1(var)
{
   Loop,Parse,var,`n
   n := a_loopfield ( strlen( n ) ? "`n" : "" ) n
      return n   
}

DerRaphael_ReverseLines_2( a,b = "",c = "init" )
{ ; adapted from sort help's last sample
   if ( c != "init" )
      return c
   else
      sort,a,% "F " A_ThisFunc
   return a
}


RandallF_ReverseLines(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed := Reversed . "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
      a0--
   }
   Return Reversed   
}

;BIND
Pause::
GuiClose:
ExitApp
Back to top
View user's profile Send private message
jballi



Joined: 01 Oct 2005
Posts: 748
Location: Texas, USA

PostPosted: Fri Jan 29, 2010 3:04 am    Post subject: Reply with quote

Can I get in on this?

Simple attempt #1

Code:
var=
(Join`n
this
 is
  a
   test
)

MsgBox % jReverse(var)
Return

jReverse(p_Data)
    {
    l_AutoTrim:=A_AutoTrim
    AutoTrim off
    VarSetCapacity(Reversed,StrLen(p_Data))
    loop parse,p_Data,`n,`r
        if StrLen(Reversed)=0
            Reversed:=A_LoopField
         else
            Reversed=%A_LoopField%`n%Reversed%
    AutoTrim %l_AutoTrim%
    return Reversed
    }


Not quite as fast as some of the other functions when reversing a small dataset but difficult to beat when reversing medium to large amounts of data.

Have fun!
Back to top
View user's profile Send private message Send e-mail
randallf



Joined: 06 Jul 2009
Posts: 678

PostPosted: Fri Jan 29, 2010 6:06 am    Post subject: Reply with quote

Cool to see all the ways of doing this, my brother showed me in python and it's not nearly as fun because they have an optimized reverse command. Razz
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Sat Jan 30, 2010 1:32 pm    Post subject: Reply with quote

Benchmarks of all here demonstrated functions (including the TF_ReverseLines TF StdLib mentioned by HugoV)



Code:
; QueryPerfomanceCounter - Benchmark Skript
; Version 1.0 (w) 2008 by DerRaphael / zLib License Style Release

DllCall("QueryPerformanceFrequency", "Int64*", BenchMark[F])
; BENCHMARK START
; Äußere Runden / Outer Rounds
@OR := 5
; Innere Läufe / Inner Runs
@IR := 10

; INIT VAR - use for simplicity this script as source
Gosub, GetVariable
Gosub, BuildRef

; Start Benchmark
Loop,4
{
   Setting := A_Index
   SetBatchLines, 20
   Critical, Off
   Process, Priority,, Normal
   if (A_Index = 1) {
      CurrentSettings := "SetBatchLines, 20 / Critical, Off / Process Priority Normal"
   } else if (A_Index = 2) {
      CurrentSettings := "SetBatchLines, -1 / Critical, Off / Process Priority Normal"
      SetBatchLines, -1
      Critical, Off
      Process, Priority,, Normal
   } else if (A_Index = 3) {
      CurrentSettings := "SetBatchLines, -1 / Critical, On / Process Priority Normal"
      SetBatchLines, -1
      Critical, On
      Process, Priority,, Normal
   } else if (A_Index = 4) {
      CurrentSettings := "SetBatchLines, -1 / Critical, On / Process Priority Realtime"
      SetBatchLines, -1
      Critical, On
      Process, Priority,, R
   }
   OfficialResult .= "`n" CurrentSettings "`n"
   
   Loop, % @OR
   {
      A_Run := A_Index
      Loop, % @IR
      {
         ; Zähler der auszuführenden Tests / Count of Tests to accomplish
         A_Test := 0
         
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "randallf's original function"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := ReverseLines(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "dR's reverse - I (loop w/ ternary)"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := reverse(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "dR's reverse - II (recursive sort callback)"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := reverse2(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "dR's reverse - III (loop w/ substring)"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := reverse3(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "jBalli's jReverse I"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := jReverse(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "TF_ReverseLines() from stdLib TF 3.1 by HugoV"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := TF_ReverseLines(var)
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
         ; SOBM***********************************************************************************
         A_Test+=1
         
         if !StrLen(Desc[%A_Test%])
            Desc[%A_Test%] := "QPC Benchmark BuildIn"
           
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[SC]) ; StartCounter
         
         ; Hier komme hinein, was getestet wird / Insert what needs to be tested
       res := QPC_BenchmarkInternal( Var )
         ; Hier ist zuende, was getestet wurde / End of whatever has been tested
         
         DllCall("QueryPerformanceCounter", "Int64 *", BenchMark[EC]) ; EndCounter
         
         Result[%A_Test%][%A_Run%][%A_Index%] := (BenchMark[EC] - BenchMark[SC]) / BenchMark[F]
      
       if ( res != ref )
         MsgBox FAIL@%A_Test%
         ; ***********************************************************************************EOBM
      }
      ; Zeiten mitteln / Average Timings
      Loop, % A_Test
      {
         A_Test := A_Index
         Result[%A_Test%][%A_Run%] := 0
         Loop, % @IR
            Result[%A_Test%][%A_Run%] += Result[%A_Test%][%A_Run%][%A_Index%]
         Result[%A_Test%][%A_Run%] /= @IR
      }
   }
   ; BENCHMARK END
   ; Resultate ermitteln / Calculate Results
   Loop, % A_Test
   {
      A_Test := A_Index
      Result[%A_Test%] := 0
      Loop, % A_Run
         Result[%A_Test%] += Result[%A_Test%][%A_Index%]
      Result[%A_Test%] /= @OR
      OfficialResult .= Result[%A_Test%] " sec`t(" Desc[%A_Test%] ")`n"
   }
}
SetBatchLines, 20
Critical, Off
Process, Priority,, Normal

MsgBox,64,Benchmark Results %@OR% Runs with %@IR% Rounds, %OfficialResult%
ExitApp

; TEST FUNCTIONS
; Randallf's Original Function
ReverseLines(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed := Reversed . "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
     
      a0--
   }
   Return Reversed
}

; dR I
reverse(var) {
   Loop,Parse,var,`n
      n := a_loopfield ( strlen( n ) ? "`n" : "" ) n
   return n
}
; dR II
reverse2( a,b = "",c = "init" ) { ; adapted from sort help's last sample
   if ( c != "init" )
      return c
   else
      sort,a,% "F " A_ThisFunc
   return a
}
; dR III
reverse3(var) {
   Loop,Parse,var,`n
      n := a_loopfield "`n" n
   return SubStr( n,1,-1 )
}
; jBally I
jReverse(p_Data)
    {
    l_AutoTrim:=A_AutoTrim
    AutoTrim off
    VarSetCapacity(Reversed,StrLen(p_Data))
    loop parse,p_Data,`n,`r
        if StrLen(Reversed)=0
            Reversed:=A_LoopField
         else
            Reversed=%A_LoopField%`n%Reversed%
    AutoTrim %l_AutoTrim%
    return Reversed
   }
; buildIn
QPC_BenchmarkInternal( Var )
{
   sort, var, F SortDummyCallBack
   return var
}

GetVariable:
var =
(Ltrim Join`n
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
   sed diam nonumy eirmod tempor invidunt ut labore et dolore
   magna aliquyam erat, sed diam voluptua. At vero eos et
   accusam et justo duo dolores et ea rebum. Stet clita kasd
   gubergren, no sea takimata sanctus est Lorem ipsum dolor
   sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing
   elitr,  sed diam nonumy eirmod tempor invidunt ut labore et
   dolore magna aliquyam erat, sed diam voluptua. At vero eos
   et accusam et justo duo dolores et ea rebum. Stet clita
   kasd gubergren, no sea takimata sanctus est Lorem ipsum
   dolor sit amet. Lorem ipsum dolor sit amet, consetetur
   sadipscing elitr,  sed diam nonumy eirmod tempor invidunt
   ut labore et dolore magna aliquyam erat, sed diam voluptua.
   At vero eos et accusam et justo duo dolores et ea rebum.
   Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet.
   Duis autem vel eum iriure dolor in hendrerit in vulputate
   velit esse molestie consequat, vel illum dolore eu feugiat
   nulla facilisis at vero eros et accumsan et iusto odio
   dignissim qui blandit praesent luptatum zzril delenit augue
   duis dolore te feugait nulla facilisi. Lorem ipsum dolor
   sit amet, consectetuer adipiscing elit, sed diam nonummy
   nibh euismod tincidunt ut laoreet dolore magna aliquam erat
   volutpat.
   Ut wisi enim ad minim veniam, quis nostrud exerci tation
   ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
   consequat. Duis autem vel eum iriure dolor in hendrerit in
   vulputate velit esse molestie consequat, vel illum dolore
   eu feugiat nulla facilisis at vero eros et accumsan et iusto
   odio dignissim qui blandit praesent luptatum zzril delenit
   augue duis dolore te feugait nulla facilisi.
   Nam liber tempor cum soluta nobis eleifend option congue
   nihil imperdiet doming id quod mazim placerat facer possim
   assum. Lorem ipsum dolor sit amet, consectetuer adipiscing
   elit, sed diam nonummy nibh euismod tincidunt ut laoreet
   dolore magna aliquam erat volutpat. Ut wisi enim ad minim
   veniam, quis nostrud exerci tation ullamcorper suscipit
   lobortis nisl ut aliquip ex ea commodo consequat.
   Duis autem vel eum iriure dolor in hendrerit in vulputate
   velit esse molestie consequat, vel illum dolore eu feugiat
   nulla facilisis.
   At vero eos et accusam et justo duo dolores et ea rebum.
   Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus
   est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  At accusam aliquyam diam diam
   dolore dolores duo eirmod eos erat, et nonumy sed tempor et
   et invidunt justo labore Stet clita ea et gubergren, kasd
   magna no rebum. sanctus sea sed takimata ut vero voluptua.
   est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat.
   Consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus est
   Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
   consetetur sadipscing elitr,  sed diam nonumy eirmod tempor
   invidunt ut labore et dolore magna aliquyam erat, sed diam
   voluptua. At vero eos et accusam et justo duo dolores et ea
   rebum. Stet clita kasd gubergren, no sea takimata sanctus
   est Lorem ipsum dolor sit amet.
)
Return

BuildRef:
   Ref := var
   Sort,Ref,F SortDummyCallBack
Return

SortDummyCallBack(a,b,c)
{
   return c
}


The Script takes each function and benchmarks it using QPC for more accuracy. the results depend on the cpu used (in my case intel atom n270) so if you're executnig the script it may look different. but the shown tendencies should stay the same.

greets
dR
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Jan 30, 2010 1:42 pm    Post subject: Reply with quote

As the TF lib isn't build for speed I think you'll find that TF_ReverseLines will become very slow for large(r) files. On the otherhand you can reverse part of a file / variable which can be useful at times (e.g. reverse only the first 10 lines or so or lines 10 to 30). But nice to see the differences...

Edit: I wonder if there would be a speed difference for randalfs version if you change this Reversed := Reversed . "`n" . a%a0%
to
Code:
Reversed .= "`n" . a%a0%

_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Sun Jan 31, 2010 6:23 pm    Post subject: Reply with quote

@HugoV:

the source is there, why dont you change the proper lines and add it as a new benchmark test and have it benched ?
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Mon Feb 01, 2010 3:22 pm    Post subject: Reply with quote

Good idea, so .= does seem faster, even an 'older method' using Reversed = %Reversed%`n%a% is somewhat better ?

Code:
; R Mod 1
ReverseLines2(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
         Reversed .= "`n" . a%a0%
      }
      Else
      {
         Reversed := a%a0%
      }
     
      a0--
   }
   Return Reversed
}

; R Mod 2
ReverseLines3(InputVar) ;reverses lines in a variable read from a file that was delimited by `n
{
   StringSplit, a, InputVar, `n
   While a0
   {
      If Reversed
      {
a:=a%a0%
Reversed = %Reversed%`n%a%
      }
      Else
      {
         Reversed := a%a0%
      }
     
      a0--
   }
   Return Reversed
}


the results are
Quote:

SetBatchLines, 20 / Critical, Off / Process Priority Normal
0.168850 sec (randallf's original function)
0.168060 sec (R Mod 1 -> .= )
0.223324 sec (R Mod 2 -> %var% )
0.003350 sec (QPC Benchmark BuildIn)

SetBatchLines, -1 / Critical, Off / Process Priority Normal
0.000402 sec (randallf's original function)
0.000294 sec (R Mod 1 -> .= )
0.000316 sec (R Mod 2 -> %var% )
0.000420 sec (QPC Benchmark BuildIn)

SetBatchLines, -1 / Critical, On / Process Priority Normal
0.000409 sec (randallf's original function)
0.000295 sec (R Mod 1 -> .= )
0.000331 sec (R Mod 2 -> %var% )
0.000418 sec (QPC Benchmark BuildIn)

SetBatchLines, -1 / Critical, On / Process Priority Realtime
0.000427 sec (randallf's original function)
0.000287 sec (R Mod 1 -> .= )
0.000320 sec (R Mod 2 -> %var% )
0.000416 sec (QPC Benchmark BuildIn)

_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
randallf



Joined: 06 Jul 2009
Posts: 678

PostPosted: Mon Feb 01, 2010 6:34 pm    Post subject: Reply with quote

Awesome stuff guys! Really cool to see the benchmarking, just what I was hoping for.

Let's switch gears for a new challenge: turn column data into tab delimited rows: (I am sure others can crush my example here)

Interesting to me at least is that if I double the column size it seems to exactly cut in half the loops.

Code:
Column1 =
(Ltrim Join`n
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
)
   
Column2 =
(Ltrim Join`n
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
)

Column3 =
(Ltrim Join`n
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
)

SetTimer, Stop, -1000


t := 1
Loops := 0

While t
{
   GoSub, ParseColumns
   Result =
   Loops++
}

msgbox %loops%
;msgbox %result%

ExitApp

Return

Stop:
t := 0
Return

ParseColumns:

   Result =

   Row := 0

   Loop, Parse, Column1, `n
   {
      Rows++ ;count the total number of rows
   }
   
   ;parse the columns and concatenate them into rows
   Loop, %rows% ;for rows
   {   
      Loop, Parse, Column%A_Index%, `n ;parse each line from the current column
      {
         Row%A_index% := Row%A_index% . A_tab . A_loopfield ;append each column entry
         StringReplace, Row%A_index%, Row%A_index%, `n ;remove the line feed
      }
      StringReplace, Row%A_index%, Row%A_index%, %A_Tab% ;remove tab from front end
   }

   Loop, %rows% ;for rows
   {
      If row%A_index% contains / ;only process and append rows that have actual data (a stamp)
      {
         Line = % Row%A_Index%
         Result := Result . Line . "`n"
      }
   }



Return


Pause::
exitapp
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Mon Feb 01, 2010 11:01 pm    Post subject: Reply with quote

probly its just me, but does this work as intended?

eg is this the result you meant it to be.

Code:
gosub, getvars

o := ""
Loop,3
   stringsplit, c%A_index%n, column%A_index%, `n
loop,% c1n0
   o .= c1n%A_Index% "`t" c2n%A_Index% "`t" c3n%A_Index% "`n"
o := SubStr( o, 1, -1 )

msgbox % o

Return

getVars:

Column1 =
(Ltrim Join`n
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
   Customer Delay
)
   
Column2 =
(Ltrim Join`n
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
   07/27/2009 04:35:11 PM
)

Column3 =
(Ltrim Join`n
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
   000 00:00
)

Return


greets
dR

@HugoV which CPU did u run the benchmark on?
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Feb 02, 2010 12:23 am    Post subject: Reply with quote

That is exactly it, rows to columns.
Back to top
emmanuel d



Joined: 29 Jan 2009
Posts: 436
Location: Belgium

PostPosted: Tue Jun 15, 2010 5:43 pm    Post subject: Reply with quote

my atempt:
Code:
var =
(Ltrim Join`n
  this
  is
  a
  test
)
Loop,Parse,var,`n
   lines := lines ? A_LoopField "`n" lines : A_LoopField
msgbox %lines%

no functions, just fast
_________________
Stopwatch
emdkplayer
http://www.autohotkey.com/forum/viewtopic.php?p=306819

the code i post falls under the:
WTFYW license
, wich meens its free to use
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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