counter ?

Help with writing and playing macros

counter ?

Postby jb_gfx » Fri Feb 24, 2006 10:09 am

Hi,

Sorry if my question sounds dumb because it should be very easy to do but I can't find any explanation anywhere.

I have some files who are formatted this way :

Code: Select all
000:000
datas goes here

000:000
datas goes here

000:000
datas goes here

...


All I want to do is insert a counter that goes from 1 to the number of datas blocks in the file on a new line before each block.

Something like that :

Code: Select all
$counter=1
Loop
Find RegExp "^([0-9]+:[0-9]+^)"
IfFound
Replace "$counter^p^1"
$counter++;
Else
ExitLoop
EndIf
EndLoop


Except that I can't find how to set up a basic variable and increment it ($counter in my example... of course!).

Any idea please ?

PS: At the end I would like to have something like that :


Code: Select all
1
000:000
datas goes here

2
000:000
datas goes here

3
000:000
datas goes here

...

3565
000:000
datas goes here
User avatar
jb_gfx
Newbie
 
Posts: 2
Joined: Sun Feb 13, 2005 12:00 am

Re: counter ?

Postby Bego » Fri Feb 24, 2006 10:44 am

Howdy,

unfortunately UE does not support variables.

But Mofi found a workaround for that, look here:

How to insert an increasing number at each "find"?

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: counter ?

Postby jb_gfx » Mon Feb 27, 2006 8:22 am

Gosh ! I tought such basic feature would be integrated... :/

Thank you for the tips but this won't work for me since the counter is limited to 99 and in my case I need thousand of values.
User avatar
jb_gfx
Newbie
 
Posts: 2
Joined: Sun Feb 13, 2005 12:00 am

Re: counter ?

Postby Bego » Mon Feb 27, 2006 9:23 am

You're running in open doors here, dude....

I think a better macro-support is the no.1 on my personal improvement hitlist. Variables, nested loops and indents would enhance the macro-possibilities a lot. :?
Anyway, many things can be done with the status quo. :idea:

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: counter ?

Postby Mofi » Mon Feb 27, 2006 12:26 pm

Nevertheless you need thousands, this could be done with my macro. Just adjust the red loop number as explained for example to 500000 and you will be able to count up to 499999.

There is also a second method to count up a number by 1 explained at Is this possible? Find X And Replace With X+1. With the following improved version it has no range limit. But you need 2 macros for this solution because 2 loops are needed and currently nested loops are not supported. The red number in the main macro is the initial value of the counter which is increased by 1 before first use.

Your main macro:

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
Clipboard 7
"0"
StartSelect
Key LEFT ARROW
Cut
EndSelect
Loop
Find RegExp "[0-9]+:[0-9]+"
IfNotFound
ExitLoop
EndIf
Key HOME
IfColNumGt 1
Key HOME
EndIf
"
"
Key UP ARROW
PlayMacro 1 "CountUp"
Key DOWN ARROW
Key END
EndLoop
ClearClipboard
Clipboard 0


The universal macro CountUp has following code:

Paste
EndSelect
InsertMode
"|"
Key LEFT ARROW
Key LEFT ARROW
OverStrikeMode
Loop
IfCharIs "0"
"1"
ExitLoop
EndIf
IfCharIs "1"
"2"
ExitLoop
EndIf
IfCharIs "2"
"3"
ExitLoop
EndIf
IfCharIs "3"
"4"
ExitLoop
EndIf
IfCharIs "4"
"5"
ExitLoop
EndIf
IfCharIs "5"
"6"
ExitLoop
EndIf
IfCharIs "6"
"7"
ExitLoop
EndIf
IfCharIs "7"
"8"
ExitLoop
EndIf
IfCharIs "8"
"9"
ExitLoop
EndIf
IfCharIs "9"
"0"
Key LEFT ARROW
IfColNum 1
InsertMode
"1"
ExitLoop
EndIf
Key LEFT ARROW
IfCharIs "0123456789"
Else
Key RIGHT ARROW
InsertMode
"1"
ExitLoop
EndIf
EndIf
EndLoop
InsertMode
Loop
IfColNum 1
ExitLoop
EndIf
Key LEFT ARROW
IfCharIs "0123456789"
Else
Key RIGHT ARROW
ExitLoop
EndIf
EndLoop
StartSelect
Find Select "|"
Key LEFT ARROW
Copy
EndSelect
Key RIGHT ARROW
Key LEFT ARROW
Key DEL



This macro is really universal. It uses the clipboard of the main macro and assumes that the initial or last number is stored in the clipboard. So the number can be manipulated by the main macro at every time. It is able to count up the number even inside a term like var[005] and does already recognize existing parts of a number if the cursor and the edit mode is set correct before calling macro CountUp. Leading zeros is supported if the initial string number has the right amount of leading zeros.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: counter ?

Postby Cleitus » Thu Sep 28, 2006 11:49 pm

Hey Mofi, hope you are browsing this as I am going out of my mind. I tried using this counter but I keep getting a "Search string not found" error when I run the following code. Basically it is taking a big file and splitting it up into smaller ones. Any help would be appreciated.

FIRST MACRO:
-----------------
InsertMode
ColumnModeOff
HexOff
Top
"000
"
Top
StartSelect
Key END
Clipboard 8
Cut
EndSelect
Key BACKSPACE
Clipboard 9
Loop
IfEof
ExitLoop
EndIf
GotoLine 15000
Key END
SelectToTop
Cut
EndSelect
DeleteLine
NewFile
"C:\tempDir\tempFile-000.csv
"
Paste
Top
Find "000"
Clipboard 8
PlayMacro 1 "CountUp"
Clipboard 9
Key HOME
StartSelect
Key END
Cut
EndSelect
Key BACKSPACE
SaveAs "^c"
CloseFile
EndLoop
Clipboard 9
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 0


YOUR MACRO: see CountUp above
User avatar
Cleitus
Newbie
 
Posts: 1
Joined: Wed Sep 27, 2006 11:00 pm

Re: counter ?

Postby Bego » Fri Sep 29, 2006 5:52 am

I'm not Mofi ;-) but: Did you set the macro property "Continue if a Find with Replace not found" ?

Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: counter ?

Postby Mofi » Fri Sep 29, 2006 6:53 am

Hi Cleitus and Bego!

Checking Continue if a Find with Replace not found is normally the correct advice, but not in this case. The 2 finds in the 2 macros should always find the string because the search string was inserted before by the macro itself.

By the way: a better english name for this property would be "Continue if a Find not found".

But here I think the problem is what I have posted in my first post at Perl RegExs misbehaving in v12.10b. In v12.10b of UltraEdit and v6.00a of UEStudio a normal non-regex find executed from within a macro never finds the string when the Perl compatible regular expression engine is active. In your first macro there is no regex engine selecting macro command because you don't use any regex search. But if you have the Perl engine selected in the configuration dialog, the non regex finds in the macro are not working nevertheless because the Perl engine is active.

Solution: Switch temporarily to an other regex engine.

I'm also wondering about the Key BACKSPACE in your macro. I think you want to delete the line inserted at top of the file, but Key BACKSPACE executed at top of the file will not do it. Key DEL or macro command DeleteLine would be better.

Here is my solution for your macro. The macro property Continue if a Find with Replace not found should have no influence on macro execution.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
"000"
StartSelect
Key HOME
Clipboard 8
Cut
EndSelect
Clipboard 9
Loop
IfEof
ExitLoop
EndIf
StartSelect
GotoLineSelect 15000
Key END
Cut
EndSelect
DeleteLine
NewFile
"C:\tempDir\tempFile-000.csv
"
Paste
"
"

Top
Find "000"
Clipboard 8
PlayMacro 1 "CountUp"
Clipboard 9
Key HOME
StartSelect
Key END
Cut
EndSelect
DeleteLine
SaveAs "^c"
CloseFile
EndLoop
Clipboard 9
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 0
PerlReOn

You can remove the green " lines if you don't want it. But I personally don't like text files where the last line of the file is not terminated with CRLF or with LF only or with CR only depending on the file type DOS/UNIX/MAC.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros

cron