Jump to content
Sign in to follow this  
myspacee

Read last n row

Recommended Posts

myspacee

Hello,

in a cicle i store in a Var some text, eg:

juventus-inter 3-0
atalanta-napoli 2-9
fiorentina-rho 4-4
....

this var grow every cicle, and i need to return in a tooltip only last 5 row.

Any hint to solve this ?

thank you !

m.

Share this post


Link to post
Share on other sites
myspacee

thank you for reply,

_FileCountLines() doesn't work only for files,

i store my values in a var...

indirectly suggest to maintain a file to store these info ?

m.

Share this post


Link to post
Share on other sites
jchd

An array seems to be a good way. Try StringSplit


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Andreik

An implementation of what jchd said:

Dim $VAR

Do
    $INPUT = InputBox("Score","Type here the score" & @CRLF & "Leave input blank to stop","")
    $VAR &= $INPUT & @CRLF
    TrayTip("Last five",DisplayLast(StringTrimRight($VAR,2),5),1)
Until $INPUT = ""

Func DisplayLast($VAR,$LAST=5)
    Local $RESULT = ""
    Local $SPLIT = StringSplit($VAR,@CRLF,1)
    If IsArray($SPLIT) Then
        For $INDEX = $SPLIT[0]-$LAST+1 To $SPLIT[0]
            If $INDEX <= 0 Then ContinueLoop
            $RESULT &= $SPLIT[$INDEX] & @CRLF
        Next
    EndIf
    Return $RESULT
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites
taietel

#include <Array.au3>

$sVar="juventus-inter 3-0"@LF
$sVar &= "atalanta-napoli 2-9"@LF
$sVar &= "fiorentina-rho 4-4"@LF
$sVar &= "uytuyt-mnb 0-1"@LF
$sVar &= "ytrytr-uio 1-2"@LF
$sVar &= "gfdgfd-uyt 3-4"

$aVar = StringSplit($sVar, @LF)
_ArrayDisplay($aVar,"The whole array")

$sLastFive = ""
For $i=$aVar[0]-4 To $aVar[0]
 $sLastFive &= $aVar[$i]&@CRLF
Next

MsgBox(32,"Last five rows are:",$sLastFive)

Edited by taietel

Share this post


Link to post
Share on other sites
jchd

Well done guys.

The best idea for the OP would probably to store successive lines in successive array entries in the first place. Since he is only interested with the 5 last lines, a fixed-sized array with 5 entries would be just good.

Use something like this:

Local $VAR5[5]
Local $count = 0

Do
    $INPUT = InputBox("Score", "Type here the score" & @CRLF & "Leave input blank to stop","")
    If $INPUT Then
        $VAR5[Mod($count, 5)] = $INPUT
        $count += 1
    EndIf
Until $INPUT = ""

If $count Then
    $count -= 1
    Local $start
    If $count < 5 Then
        $start = 0
    Else
        $start = $count - 4
    EndIf
    For $i = $start To $count
        ConsoleWrite("Last line - " & $count - $i & " = " & $VAR5[Mod($i, 5)] & @LF)
    Next
EndIf

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Andreik

^you're a genius and gave me another idea of example using _ArrayPush()

#include <Array.au3>

Dim $VAR5[5]

While True
    $INPUT = InputBox("Score", "Type here the score" & @CRLF & "Leave input blank to stop","")
    If $INPUT <> "" Then
        _ArrayPush($VAR5,$INPUT)
        TrayTip("Content",$VAR5[0] & @CRLF & $VAR5[1] & @CRLF & $VAR5[2] & @CRLF & $VAR5[3] & @CRLF & $VAR5[4],1)
    Else
        ExitLoop
    EndIf
WEnd

When the words fail... music speaks

Share this post


Link to post
Share on other sites
Malkey

The StringInStr/StringMid method in the example also works fine when there are 300,000 lines in the variable, $sVar.

The StringRegExpReplace method causes the tool-tip to lag a little behind the moving cursor.

HotKeySet("{ESC}", "Terminate")
HotKeySet("!i", "Input")

Local $sLastFive, $iLoc
Global $sVar = "juventus-inter 3-0" & @LF
$sVar &= "atalanta-napoli 2-9" & @CRLF
$sVar &= "fiorentina-rho 4-4" & @LF

;local $sVar = FileRead("linesOut.txt")

While 1
    ; StringInStr/StringMid method.
    $iLoc = StringInStr($sVar, @LF, 0, -5 - (StringMid($sVar, StringInStr($sVar, @LF, 0, -1) + 1) = ""))
    $sLastFive = StringMid($sVar, $iLoc + 1)

    ; or

    ; StringRegExpReplace method.
    ;$sLastFive = StringRegExpReplace($sVar, "^((.*\v*)*?)((.*\v*){0,5})$", "\3")

    ToolTip("Press Alt+I to add line." & @CRLF & "Press Esc to exit." & @CRLF & "Last five rows are:" & @CRLF & @CRLF & StringStripWS($sLastFive, 2))
    Sleep(10)

WEnd


Func Input()
    Local $INPUT
    While 1
        $INPUT = InputBox("Score", "Type here the score" & @CRLF & "Leave input blank to stop", "")
        If $INPUT <> "" Then
            $sVar &= $INPUT & @CRLF
            ToolTip("Last five rows are:" & @CRLF & @CRLF & StringStripWS(StringMid($sVar, StringInStr($sVar, @LF, 0, -5 - (StringMid($sVar, StringInStr($sVar, @LF, 0, -1) + 1) = "")) + 1), 2))
        Else
            Return
        EndIf
        Sleep(10)
    WEnd
EndFunc   ;==>Input

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Share this post


Link to post
Share on other sites
jchd

@Andreik,

Indeed, one can use _ArrayPush the way you show.

But this is exactly what I wanted to avoid. look at what _ArrayPush does and imagine we are in the situation where we want to keep only the last 300 000 lines of a continuous log.

_ArrayPush will move 299 999 entries around everytime it's invoked.

Even worse would be ReDim to N+1 entries and _ArrayDelete the first one.

Using the modulo function and working only with the indices of the array may seem a bit more convoluted at first look but reveals a much more efficient solution to the general problem, since it scales gracefully in constant time.

I fully agree that with only 5 entries the question doesn't matter. I wanted to offer a more efficient way to this thread which someday could be of real value to someone else having a need for a much bigger "leftover".


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
dexto

How about RegExp:

ConsoleWrite(GetLastLine(FileRead(@ScriptFullPath)) & @CRLF)
Func GetLastLine($txt, $lines = 1)
    local $got = StringRegExp($txt, '((?:.*?(?:\r\n|\n\r|\r|\n|\Z)){'&$lines&'}\Z)', 1)
    If Not @error Then
        Return $got[0]
    Else
        Return SetError(@error, 1, '')
    EndIf
EndFunc   ;==>GetLastLine
Edited by dexto

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×