Subhobroto Posted September 25, 2008 Share Posted September 25, 2008 Hi I implemented the line counting in PERL because I did not have much time to whip it up in C++ or AutoIt. The bonus of this code is I get to show people how to embed PERL in AutoIt :-) Ah - but that means to do so/try out this example, you need ActivePerl to be installed on your system (but how does not ;-) ) Altough I did not compare the test results of the other functions, I would believe MrCreatoR's matches mine so far. His results will differ with mine only in those cases where we don't want to count trailing blank. Infact I will be interested in knowing why would anyone want to dismiss count trailing blank line? Anyways, here is the code: CODEFunc LineCountPerl($sFilePath, $ignoreTrailingBlankLine = 0) Local $oSC = ObjCreate("ScriptControl") $oSC.language = "PerlScript" Local $code = 'sub countLines{' $code &= @LF & '$/ = undef;' $code &= @LF & 'open INPUTFILE, $_[0] or return 0;' $code &= @LF & 'binmode INPUTFILE;' $code &= @LF & 'my $inputLine = <INPUTFILE>;' $code &= @LF & 'return 0 if (0 == length($inputLine));' $code &= @LF & 'my $count = 1 + ($inputLine =~ tr/\x0D//);' $code &= @LF & 'while ($inputLine =~ /[^\x0D]\x0A/g) { ++$count; }' $code &= @LF & 'my $last2Characters = (unpack("x" . (length($inputLine) - 2) . " a2", $inputLine));' $code &= @LF & 'if ((unpack("x1 a1", $last2Characters) =~ tr/\x0D\x0A//) && ($last2Characters =~ tr/\x0D\x0A//))' $code &= @LF & '{' $code &= @LF & '--$count if (1 == ' & $ignoreTrailingBlankLine & ');' $code &= @LF & '}' $code &= @LF & 'close INPUTFILE;' $code &= @LF & 'return $count;' $code &= @LF & '}' $oSC.addcode($code); return $oSC.eval('countLines("' & $sFilePath & '");'); EndFunc Attached is the updated POC with other's code too. I have not attached the test files, but screenshots are attached for proof ;-) Link to comment Share on other sites More sharing options...
MrCreatoR Posted September 25, 2008 Share Posted September 25, 2008 I have not attached the test files, but screenshots are attached for proofAbout the «expected», what you mean?Please show me a file with the «--- expected 1» and my script return «2». Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
MrCreatoR Posted September 25, 2008 Share Posted September 25, 2008 Here is the version with additional parameter $iCountLastLine: $iCountLines = _FileCountLinesEx("Test.txt", 0) $sCL_Results = StringFormat("Lines = %i\n@error = %i", $iCountLines, @error) ConsoleWrite($sCL_Results) Func _FileCountLinesEx($sFilePath, $iCountLastLine=1) Local $iCountLines = 0 Local $sFRead = FileRead($sFilePath) If @error = -1 Then Return SetError(1, 0, 0) ;The file is empty If @error <> 0 Then Return SetError(2, 0, 0) ;File not exists or other "Read" error. If Not $iCountLastLine Then $sFRead = StringStripWS($sFRead, 2) $sFRead = StringReplace($sFRead, @CRLF, "") $iCountLines += @extended $sFRead = StringReplace($sFRead, @CR, "") $iCountLines += @extended StringReplace($sFRead, @LF, "") $iCountLines += @extended Return $iCountLines + 1 EndFunc Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now