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 

TF: Text file & Variables/String Library v3.3 [lib]
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
majkinetor



Joined: 24 May 2006
Posts: 4250
Location: Belgrade

PostPosted: Tue Jul 28, 2009 5:40 pm    Post subject: Reply with quote

Quote:
* Note: yes they are a bit silly, but I find them to be useful
as they serve as a shorthand for TF_ReplaceInLines. They are
by no means "intelligent". Thanks to infogulch for the idea:

Its not silly. I use that feature pretty much tbh and every serious editor has it.

One other cool function to have would be to extract portion of file from one key sentence to another and single Trim function, perhaps with better signature to accept trim type (left, right, both).
_________________
Back to top
View user's profile Send private message
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Tue Jul 28, 2009 9:18 pm    Post subject: Reply with quote

majkinetor wrote:
Its not silly. I use that feature pretty much tbh and every serious editor has it.
The function itself is useful, but you could already do that with a little bit of extra effort:

Code:
TF_ReplaceInLines(F, "%A_Tab%", "%A_Space%%A_Space%%A_Space%%A_Space%") ; you could do this
TF_Tab2Spaces(F) ; but this is shorter :-)


majkinetor wrote:
One other cool function to have would be to extract portion of file from one key sentence to another and single Trim function, perhaps with better signature to accept trim type (left, right, both).


So not TF_RangeReplace but a TF_RangeExtract/Grep/Echo like function, yes that can be very useful, I'll look into it. Another idea I've almost completed is converting a delimited file (such as a CSV) to a fixed column, so

Quote:
1,222222,3
44,5,6


will become

Quote:
1.,222222,3
44,5........,6
(. is space)

which will enable further processing by the TF_Col* functions

UltraEdit has this built in and I find it extremely useful combined with
the other column editing functions of UE.
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
Murp|e



Joined: 12 Jan 2007
Posts: 515
Location: Norway

PostPosted: Tue Jul 28, 2009 11:33 pm    Post subject: Reply with quote

HugoV: I was using the TF_RemoveBlankLines function with the "!" symbol for OverWrite (Because I want to remove the blank lines of a file, as opposed to creating a copy of it minus the blank lines). This works fine except in cases where the the file doesn't exist. If the file doesn't exist, this function used with overwrite will create a blank file. I don't think a function which is supposed to remove something from a file, should ever be expected to create an entirely new filequestionmark
Back to top
View user's profile Send private message Visit poster's website
ribbet.one
Guest





PostPosted: Wed Jul 29, 2009 4:35 am    Post subject: Small modifications to tf_linenumber Reply with quote

HugoV you are awesome. Heresy also. And the rest of you too.

I have always wanted a way to emulate line enumeration found in my old word processor, WordPerfect. Call it a legacy from the days of hard copy Smile but it can start new line numbers on every page. So with this small mod you can emulate that and make the line numbers restart every time it reaches a designated number. This way I can set up my printed pages to contain N number of lines and make line number restart at N.

Also a minor mod here is you can replace the leading zero with any character, with the possible exception of ahk operators or code (haven't checked that). And the default, instead of zero, is simply A_Space. This way effectively Right-justifies the line number.

I still want a way to left-justify the line number without having all the text move to the right one char when you add another digit, which is what happens now.

So two new params in this version:

MaxNumber = maximum line number. If = 0 (default) MaxNumber = true line number

Padliner = character or number to take place of "leading zero." Default is A_Space

Code:


TF_LineNumber(TextFile, LeadingZeros = 0, MaxNumber = 0, Padliner = "A_Space")
   
   {
    TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
    FileRead, Str, %TextFile%
    If (LeadingZeros > 0)
      {
             StringReplace, Str, Str, `n, `n, UseErrorLevel
            Lines:=ErrorLevel+1
            Padding:=StrLen(Lines)
            Loop, %Padding%
          PadLines .= Padliner
      }
    Loop, Parse, Str, `n
      {
          If MaxNumber = 0
             MaxNo = %A_Index%
          Else
             {
                MaxNo++
                If MaxNo > %MaxNumber%
                Maxno = 1
             }
            LineNumber:= Maxno
            If (LeadingZeros > 0)
         {
          LineNumber := Padlines LineNumber  ; add padding
          StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
         }
       OutPut .= LineNumber A_Space A_LoopField "`n"
      }
    Return OW ? _OverWrite_(TextFile, Output) : _MakeCopy_(TextFile, Output)
   }



Pls. feel free to offer any improvements or suggestions. I'm not a fluent AHK coder, but with your help I'm improving slowly. I've only tested these a few times.
Back to top
ribbet.one
Guest





PostPosted: Wed Jul 29, 2009 5:10 am    Post subject: Reply with quote

OK, I think this may fix the left justify issue with the line number when there are no leading characters. Feel free to tell me if I'm doing it wrong or to help me out here. I may have it right. Smile

Code:

TF_LineNumber(TextFile, LeadingZeros = 0, MaxNumber = 0, Padliner = "A_Space")
   
   {
    TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
    FileRead, Str, %TextFile%
    ;If (LeadingZeros > 0)
      {
             StringReplace, Str, Str, `n, `n, UseErrorLevel
            Lines:=ErrorLevel+1
            Padding:=StrLen(Lines)
            Loop, %Padding%
          PadLines .= Padliner
      }
    Loop, Parse, Str, `n
      {
          If MaxNumber = 0
             MaxNo = %A_Index%
          Else
             {
                MaxNo++
                If MaxNo > %MaxNumber%
                Maxno = 1
             }
            LineNumber:= Maxno
            If (LeadingZeros > 0)
         {
          LineNumber := Padlines LineNumber  ; add padding
          StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
         }
          If (LeadingZeros = 0)
         {
          LineNumber := LineNumber Padlines ; add padding
          StringLeft, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
         }         
       OutPut .= LineNumber A_Space A_LoopField "`n"
      }
    Return OW ? _OverWrite_(TextFile, Output) : _MakeCopy_(TextFile, Output)
   }
Back to top
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Wed Jul 29, 2009 7:41 am    Post subject: Reply with quote

Murp|e wrote:
HugoV: I was using the TF_RemoveBlankLines ... This works fine except in cases where the the file doesn't exist. If the file doesn't exist, this function used with overwrite will create a blank file. I don't think a function which is supposed to remove something from a file, should ever be expected to create an entirely new file
All functions assume the file you pass on to the function exist, if it doesn't it means there is no input and it will simple generate an empty file (0 byte).

But I'll put it on my TODO list.
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Wed Jul 29, 2009 8:05 am    Post subject: Re: Small modifications to tf_linenumber Reply with quote

ribbet.one wrote:
WordPerfect .... Call it a legacy from the days of hard copy Smile but it can start new line numbers on every page....

Also a minor mod here is you can replace the leading zero with any character, with the possible exception of ahk operators or code (haven't checked that). And the default, instead of zero, is simply A_Space. This way effectively Right-justifies the line number.

I still want a way to left-justify the line number without having all the text move to the right one char when you add another digit, which is what happens now.
Thanks for the suggestion, I will add this to the library. I've been testing it (changing your mod slightly)

Output generated:

padding with "0" (leading = 1) ; 0 is default value
...
08 line
09 line
10 line
... --> OK (same as before)

padding with " " (leading = 1)
...
8 line
9 line
10 line
... --> OK (same as before but with space passed on as padding char)

no padding
...
8 line
9 line
10 line
...

Now the question is should the "no padding" line up
the original lines so look like this:

no padding, where _ indicates a space because of the forum doesn't show multiple spaces
...
8 _line
9 _line
10 line
...

Or should it be left as users choice, e.g. lineup yes / no

Re: wordperfect yes those were the days Smile
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
ribbet.1



Joined: 20 Feb 2007
Posts: 191
Location: D.C.

PostPosted: Wed Jul 29, 2009 10:22 am    Post subject: Re: Small modifications to tf_linenumber Reply with quote

The BB editor and the forum's font don't display this issue properly. So I'm using the "code" tags to make it more clear.
Quote:
no padding
...
8 line
9 line
10 line
...

I don't see the padding there. Maybe your results above are just a BB editor glitch, but if you set the leadingzero to 0 and still have a char for the "padliner" param ( here " " ) you get this:
Code:
7  This is line seven
8  This is line eight
9  This is line nine
10 This is line ten

or just for the purpose of clarifying, using these settings:
Code:
TF_LineNumber(F,0,0," ")

I get this:
Code:
7  This is line seven
8  This is line eight
9  This is line nine
10 This is line ten
11 This is line eleven
12 This is line etc.

Instead of this:
Code:
7 This is line seven
8 This is line eight
9 This is line nine
10 This is line ten
11 This is line eleven
12 This is line etc.

Quote:
Now the question is should the "no padding" line up
the original lines so look like this:

no padding, where _ indicates a space because of the forum doesn't show multiple spaces
...
8 _line
9 _line
10 line
...

That's my personal preference, partly for aesthetic reasons. Moreover, that way column operations will still work the same after you apply line numbers. And I can still use my beloved "block editing" in Textpad too. So if we're voting, I'd say make it that way.
Quote:

Or should it be left as users choice, e.g. lineup yes / no

Easy enough to create another param for that and let user decide. But I think I have it the way I want it.

Re: WP, before I had my AHK I dabbled in PerfectScript. I always wondered if PerfectScript and AutoIt had some of the same origins.
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 191
Location: D.C.

PostPosted: Wed Jul 29, 2009 11:02 am    Post subject: Reply with quote

And if you use this modification, you can also choose to pad the line number or not:
Code:
TF_LineNumber(TextFile, LeadingZeros = 0, MaxNumber = 0, Padliner = "A_Space")
   
   {
    TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
    FileRead, Str, %TextFile%
      {
             StringReplace, Str, Str, `n, `n, UseErrorLevel
            Lines:=ErrorLevel+1
            Padding:=StrLen(Lines)
            Loop, %Padding%
          PadLines .= Padliner
      }
    Loop, Parse, Str, `n
      {
          If MaxNumber = 0
             MaxNo = %A_Index%
          Else
             {
                MaxNo++
                If MaxNo > %MaxNumber%
                Maxno = 1
             }
            LineNumber:= Maxno
            If (LeadingZeros = 1)
         {
          LineNumber := Padlines LineNumber  ; add padding
          StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
         }
          If (LeadingZeros = 0)
         {
          LineNumber := LineNumber Padlines ; add padding
          StringLeft, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
         }         
       OutPut .= LineNumber A_Space A_LoopField "`n"
      }
    Return OW ? _OverWrite_(TextFile, Output) : _MakeCopy_(TextFile, Output)
   }

If you make the LeadingZero any number besides 0 or 1, like this:
Code:
TF_LineNumber(F,2,0," ")

you get this:
Code:
8 This is line eight
9 This is line nine
10 This is line ten
11 This is line eleven

But if you make LeadingZero a 0 you get
Code:
8  This is line eight
9  This is line nine
10 This is line ten
11 This is line eleven

And if you make it a 1 you get:
Code:
 8 This is line eight
 9 This is line nine
10 This is line ten
11 This is line eleven

So freedom of choice is restored and it didn't even take any more code lines.
Back to top
View user's profile Send private message
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Wed Jul 29, 2009 11:21 am    Post subject: Reply with quote

@ribbet.1 yes I noticed that too while experimenting and preparing the examples. Thanks. I was bugfixing something else, see "history" in next update Wink
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum


Last edited by hugov on Wed Jul 29, 2009 5:24 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Wed Jul 29, 2009 5:23 pm    Post subject: Reply with quote

v2.3a update/bugfix, 29 July 2009:

Fixed/Changed:

- New features in TF_LineNumber:
Restart + Choice of leading/padding character
Thanks for the idea ribbet.1, http://www.autohotkey.com/forum/viewtopic.php?p=284687#284687

- Built in Check if TextFile actually exists, to prevent creation of empty file(s).
Thanks for the idea Murp|e, http://www.autohotkey.com/forum/viewtopic.php?p=284649#284649

- TF_Replace: if SearchText wasn't present in TextFile the function never returned (stuck in endless loop)

- TF_ReplaceInLines: if SearchText wasn't present in TextFile simple return an do nothing (faster, does not create file_copy)

- TF_RegExReplaceInLines: if NeedleRegEx wasn't present in TextFile simple return an do nothing (faster, does not create file_copy)

@ribbet.1: I believe your version of TF_LineNumber above has a bug, try it with TF_LineNumber(F) and see what happens Wink

I've made some changes to your version so it uses 0 by default so the new function behaves the same as the old one when using the F and F,1 examples.
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
ribbet.1



Joined: 20 Feb 2007
Posts: 191
Location: D.C.

PostPosted: Wed Jul 29, 2009 7:25 pm    Post subject: Reply with quote

HugoV wrote:
v2.3a update/bugfix, 29 July 2009:


@ribbet.1: I believe your version of TF_LineNumber above has a bug, try it with TF_LineNumber(F) and see what happens Wink

I guess you don't want to use "A_Space" after all. Embarassed I replace it with " " and it seems to work. I hope that's all you're referring to.

Thanks for considering my suggestions. I've never seen a text editor that has this feature common in word processors, and I've wanted it for a while.
Back to top
View user's profile Send private message
Tastingo
Guest





PostPosted: Wed Jul 29, 2009 8:24 pm    Post subject: Reply with quote

Thanks!!!!!
Back to top
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Mon Aug 03, 2009 2:22 pm    Post subject: Reply with quote

v2.3b minor update:

- Fixed: no output problem for TF_SplitFileBy* functions, "bug" introduced by 2.3a "Built in Check if TextFile actually exists"
- TF_RemoveBlankLines check if file has empty empty lines to start with, if not, return and do nothing (does not create file_copy identical to file)
- TF_RangeReplace same fix as 2.3a

Edit: will work on it some more and build in check to speed up various functions, no use in looping all lines if the end result will be the same as the input.
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
hugov



Joined: 27 May 2007
Posts: 3273

PostPosted: Thu Aug 06, 2009 11:45 am    Post subject: Reply with quote

v2.4 update, 06 August 2009:

- Added: TF_Sort (HugoV)

Simply pass on the Sort options as described in http://www.autohotkey.com/docs/commands/Sort.htm to sort (a section of) TextFile
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Page 2 of 10

 
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