advance a counter from a macro

  • Permalink
  • submit to reddit
  • Email
  • Follow


This is so basic I'm ashamed to ask, but I know people
here can answer in no time, so I'll shut up my ego and ask.
Consider

\def\ADV{\global\advance\lll by 1}

\lll=-1
\ADV
\ifnum\lll=0 ZZZ \else PPP \fi

\lll=-1
\showthe\lll
\ADV
\ifnum\lll=0 ZZZ \else PPP \fi

\lll=-1
\ADV
\showthe\lll
\ifnum\lll=0 ZZZ \else PPP \fi

\bye

I expected "ZZZ  ZZZ  ZZZ" but I get  "PPP PPP ZZZ"
Why??

Piero
0
Reply pierodancona (12) 9/24/2008 7:52:04 PM

See related articles to this posting


pierodancona@gmail.com a =E9crit :
> This is so basic I'm ashamed to ask, but I know people
> here can answer in no time, so I'll shut up my ego and ask.
> Consider
>
> \def\ADV{\global\advance\lll by 1}
>
> \lll=3D-1
> \ADV
> \ifnum\lll=3D0 ZZZ \else PPP \fi
>
> \lll=3D-1
> \showthe\lll
> \ADV
> \ifnum\lll=3D0 ZZZ \else PPP \fi
>
> \lll=3D-1
> \ADV
> \showthe\lll
> \ifnum\lll=3D0 ZZZ \else PPP \fi
>
> \bye
>
> I expected "ZZZ  ZZZ  ZZZ" but I get  "PPP PPP ZZZ"
> Why??
>  =20

A \relax is wiser after a number to avoid unexpected expansion of \ifnum =
:

\def\ADV{\global\advance\lll by 1\relax}

0
Reply admin_c (13) 9/24/2008 8:05:56 PM

> > Why??
>
> A \relax is wiser after a number to avoid unexpected expansion of \ifnum :
>

Yes!
Thank you. I can go to sleep now

Piero

0
Reply pierodancona (12) 9/24/2008 9:08:25 PM

<pierodancona@gmail.com> wrote:

> This is so basic I'm ashamed to ask, but I know people
> here can answer in no time, so I'll shut up my ego and ask.
> Consider
> 
> \def\ADV{\global\advance\lll by 1}
> 
> \lll=-1
> \ADV
> \ifnum\lll=0 ZZZ \else PPP \fi
> 
> \lll=-1
> \showthe\lll
> \ADV
> \ifnum\lll=0 ZZZ \else PPP \fi
> 
> \lll=-1
> \ADV
> \showthe\lll
> \ifnum\lll=0 ZZZ \else PPP \fi
> 
> \bye
> 
> I expected "ZZZ  ZZZ  ZZZ" but I get  "PPP PPP ZZZ"
> Why??

Read closely your input. It's equivalent to

\lll=-1 \ADV\ifnum\lll=0 ZZZ \else PPP \fi

and, after the initial assignment, it expands to

\global\advance\lll by 1\ifnum\lll=0 ZZZ \else PPP \fi

Now TeX expands tokens after the 1, as explained in the TeXbook;
therefore the conditional is evaluated when the value of \lll is
still -1.

A proper way to define \ADV is

\def\ADV{\global\advance\lll by 1 }

or

\def\ADV{\global\advance\lll by \@ne}

(in a context where @ has category code 11). In the first case
the space will be ignored, in the second case the number specification
ends at the token \@ne which is not expandable (Plain TeX and LaTeX
define it by "\chardef\@ne=1").

In the second case the "\showthe\lll" is irrelevant, but in the third
one it stops the search for a number specification, since \showthe does 
not expand to a <number>; therefore the assignment \lll=0 is performed
and the conditional is evaluated with the intended value.

Ciao
Enrico
0
Reply gregorio (1367) 9/24/2008 9:19:18 PM
comp.text.tex 36758 articles. 71 followers. Post

3 Replies
81 Views

Similar Articles

[PageSpeed] 34


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Re: Problem with macro logical condition : passing a counter in the macro
Paul explains well the issues, and these are things one must understand in order to use the macro facility with confidence. However, you might find yourself moving in the direction of a quick fix by simply removing the percent signs in your Condition 1 and Condition 2 blocks. On Thu, 8 Jul 2004 01:33:20 -0400, Paul M. Dorfman <sashole@BELLSOUTH.NET> wrote: >Dominic, > >You are trying to use a macro assembling Data step code based on the value >of a Data step variable before the Data step has run. Note that the entire >activity of your macro compilation ends before the S...

Problem with macro logical condition : passing a counter in the macro from the data step
Hi, I have that little piece of code as an example (see below). In the macro 'ma_create' I want to use the variable count that is created in the data step 'test'. Basically count in that exemple is set to _n_. I know that count is seen correctly in the macro because I generate the variable check is has the right value. Still logical operation that requires the value of count fail. In execution of the macro, count is always seen as greater than the macro variable days which is set in that example to 5. Why does the code only excute condition 1 (count is always se...

Advanced Macro Class
You have been complaining about this class even before you took it. Lets work on these awful examples, so the "Ivory Billed Woodpeckers" can see them and improve this course. We can start with the example you refer to. What macro technique was being demonstrated? What would be a better example? When we beat this one to death we can move on to other irritating examples. On 9/5/08, Toby Dunn <tobydunn@hotmail.com> wrote: > Cherish, > > This is one of those examples I saw and hated in the Advanced Macro class > I attend last week. No macro is needed at all. Idea...

Advanced usage of macros
I have to create a lot of similar tables that only differs by type and name of each column (ok, I guess that _is_ the difference between tables). I create these tables by creating a class for a row and then letting a std::vector hold objects of these classes. As I have to write quite a lot I would like to use macros for this. Below I have shown how most of it works. I just cannot figure out a good way of keeping the name of each column. I'm hoping somebody have a great idea on how to accomplish this :-) --- table_row.cc --- #include <iostream> #include <string> #defin...

More advanced forms of macros?
Have there ever been any experiments with something like: a. "semi-colon eating" macros #define foo(a, b;) case ((a)+(b)): foo(a, b); becomes case ((a)+(b)): b. "block taking" macros: #define foo(a, b, {c}) switch (a) \ { case (b): { c } break; default: blah; } foo(a, b) { outer1; { inner; } outer2; } becomes: switch (a) { case (b): { outer1; { inner; } outer2; } break; default: blah; } Maybe somebody thought of something like this before but they decide...

NaturallySpeaking advanced scripting macros
In this thread, I will provide some potentially useful Naturally Speaking advanced scripting macros for Windows. You must have Dragon NaturallySpeaking 8 Professional (or maybe prior Professional versions). My methods are subject to change as I learn the specific scripting capabilities of the speech recognition program. I will try to post any corrections/improvements to an already posted macro under that macro's original subthread. If a macro will not work on your system, please advise and I will be happy to reformulate/rewrite or offer assistance to get the thing working on your...

Macro Variable Counter Resolving to ** ?
Hi, I wrote a macro that creates a dataset, and need to make the number of observations in that dataset available a a variable inside the macro: %macro PValueLoop; .... /*Create dataset to be analysed. */ data CorrelationData; set Head2SHT1; where ...; run; /* Determine # of observations in dataset. */ data _null_; set CorrelationData end=eof; o+1; oo=left(put(o,2.)); if eof then call symput ('Total',oo); run; .... /* Format and relabel correlation results. */ data FormatResults; set CorrResults; NumObs=&&Total; run; %mend PValueLoop; Why do I get ** as the output? I tri...

Advanced Macros && Preprocessors
Hi all, I'm developing a small defensive programming toolkit for my own projects. So, here are my questions. 1) Is it possible to move from something like this: SAFECALL(foo();) to __safecall foo(); 2) I need to auto-add code to every method of particular class safeclass MyClass { public: void foo(); } void MyClass::foo() {} will modify the foo implementation - add some lines of code on the end of the method 3) even more advanced stuff, is it possible to modify the preprocessor (or add my own preprocessor)? I need to parse something like this. __precheck minutes >= 0 &&am...

Advanced Macros && Preprocessors
Hi all, I'm developing a small defensive programming toolkit for my own projects. So, here are my questions. 1) Is it possible to move from something like this: SAFECALL(foo();) to __safecall foo(); 2) I need to auto-add code to every method of particular class safeclass MyClass { public: void foo(); } void MyClass::foo() {} will modify the foo implementation - add some lines of code on the end of the method 3) even more advanced stuff, is it possible to modify the preprocessor (or add my own preprocessor)? I need to parse something like this. __precheck minutes >= 0 &&am...

Macro counter exceeding its upper bound
One more minute of SAS-L brain time please . . . Can anyone suggest why the "logmo" counter is exceeding its specified bound (12). I'm seeing it go to 13. Many thanks, Michael Murff Provo, UT ***************************************************************; options mlogic mprint symbolgen; %macro bigloop; %do yr=86 %to 86; %let year = %eval(&yr+4); data _null_; /*** Create log variables based on mcap ratios ***/ /*** 12 total for current year ***/ %do logmo= 1 %to 12; ...

Re: Macro Variable Counter Resolving to ** ?
Patricia: Probably the number of observations is more than 2 bytes. oo=left(put(o,2.)); ^^^^^^^^^^^^^^^ increase the length from 2 to 4, see if that fixes it. Prasad Ravi Patricia Simon <patricia.simon@QUANT To: SAS-L@LISTSERV.UGA.EDU UM.COM> cc: Sent by: "SAS(r) Subject: Macro Variable Counter Resolving to ** ? Discussion" <SAS-L@LISTSERV.UGA.E ...

Re: Advanced Macro Class #2
Kevin, I have a simple answer for your when question. Only use a Macro if there is no way in hell to use SAS Component Language (:>)))! > > > However, part of teaching macro is WHEN to use it. I might agree that this > is gratuitous use of macro, when a datastep solution exists.... > > HTH, > > Kevin > ...

Simple counter output to log from macro loop
I have the following looping macro: %macro wait; %global nowdone; %let nowdone = 0; %doagain:; data _null_; call sleep(15000); run; data _null_; infile one; length x $25.; input x $; if x='DIST.dat' then call symput('nowdone','1'); run; %if &nowdone.^=1 %then %goto doagain; %mend; Currently output is diverted to an archive log but I would like to add a counter so that the SAS log window outputs a counter each time the loop loops. Sounds simple but perhaps because my head is as foggy as this monday morning is grey, I cannot figure...

Re: Macro Variable Counter Resolving to ** ? #2
Patricia, You might tighten the code and solve your problem with /*Create dataset to be analysed, and determine # of observations in dataset. */ data CorrelationData; set Head2SHT1 end = eof; where ...; if eof then call symput ( "Total" , put ( _n_ , best. -l) ) ; run; Note that format width defaults to 12. IanWhitlock@westat.com -----Original Message----- From: Patricia Simon [mailto:patricia.simon@QUANTUM.COM] Sent: Thursday, July 17, 2003 6:28 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Macro Variable Counter Resolving to ** ? Hi, I wrote a macro that creates a dataset, and nee...

Re: Macro counter exceeding its upper bound
Hi Michael, Classical problem: after incrementing (or decrementing) the loop counter each iteration, at the end of the iterations (end condition reached) it always gets the final loop value plus (or minus) the in(de)crementing value. This is no problem, but if the value is used outside of the loop it may be a surprise. It is due to in(de)crementing beyond the final loop value, after which SAS sees it is beyond and does not start another iteration anymore. E.g. DO I = 1 TO 2; END; PUT I=; gives I=3. Thus in your case set Logmo one value back (subtract 1) after the END of the DO loop. Regard...

Re: Simple counter output to log from macro loop
Ben, Not exactly sure when you want the counter to show up in your log but here is one example of putting the counter indicator value to the log - filename one "c:\download\test.dat"; run; %macro wait; %global nowdone; %let nowdone = 0; %let counter = 0; %doagain:; %let counter = %eval(&counter + 1); %put "Counter = &counter"; data _null_; call sleep(15000); run; data _null_; infile one; length x $25.; input x $; if x='DIST.dat' then call symput('nowdone','1'); run; %if &nowdone.^=1 %then %goto doaga...

Re: Simple counter output to log from macro loop #2
Thanks Charles. It was the eval syntax I didn't know how to do, Ben. ...

SAS Programmer with advanced SAS macro skills, salary up to $150k
Our San Francisco Peninsula Client is in need of a SAS Programmer with advanced SAS macro skills. Candidate will develop and support various UNIX systems and software. Mostly SAS software for use in clinical trials data analysis and reporting by the biostatistics and programming departments. Position will not include working directly with clinical trials data or on clinical trials report teams.Ideal candidate will have advanced skills in: SAS data manipulation, SAS reporting, SAS macros, Unix, and software design and testing. A degree in a computer-related area is preferred. Experience in any ...

Better Examples for SAS Courses Re: Advanced Macro Class
I'll change the thread name in the hopes of catching the attention of the SAS education birdies :-) Yes, the examples in some of the SAS classes definately need to be improved and be more like real-life problems; given the specifics that many users give here on SAS-L, it would seem that there should be many "real world" examples that could be used by searching the archives of SAS-L in rather than examples that are hardly ever relevant to real-world users. One example that I particularly disliked, given that very few SAS programmers are engineers, was the "Ingot" examp...

[ANN] Advanced macro processor (preprocessor) Unimal 2.0a release
Hello NG, I posted the new release of Unimal (http://www.macroexpressions.com/unimal.html). Unimal is an advanced macro processor (preprocessor) independent of the target programming language(s); it helps improve software quality and development process, helps improve code maintainability and helps optimize memory usage in a highly maintainable way. It can do just about anything you'd expect from a macro processor. And the previous versions have been working hard for a major process control instrumentation company. [News release is at http://www.pr.com/press-release/14270 or http...

Re: Better Examples for SAS Courses Re: Advanced Macro Class
Hi Mary, I'm german and I HAVE problems with many of technical terms. However I never had a problem to ask, if I did not understand what is something. If you had worked some time at the helpdesk of a steel mill, that would be very near to practice for you. I think, what you say is wrong: exactly that IS a example which come from practice! The other way, without any practice would be, "you have 5 attributes, x1, x2, x3, x4 and y where y is the independant ... You see one of the disatvantages of "practical examples": they might be NOT from your (university)-"world"! ...

[rfc-dist] RFC 5930 on Using Advanced Encryption Standard Counter Mode (AES-CTR) with the Internet Key Exchange version 02 (IKEv2) Protocol
A new Request for Comments is now available in online RFC libraries. RFC 5930 Title: Using Advanced Encryption Standard Counter Mode (AES-CTR) with the Internet Key Exchange version 02 (IKEv2) Protocol Author: S. Shen, Y. Mao, NSS. Murthy Status: Informational Stream: IETF Date: July 2010 Mailbox: shenshuo@cnnic.cn, yumao9@gmail.com, ssmurthy.nittala@freescale.com Pages: 6 Charact...

Macro question: How to created macro variables from a macro
The macro below works when I don't use the CI variable and instead hard code the size of the confidence interval. When I add the CI= option in the macro it doesn't work, I'm guessing for a number of reasons, such as attempting to create a variable outside of a datastep and, perhaps, treating said variables as both a number and as text. Any help appreciated. Thanks. %MACRO UNIVAR(DATA=ONE, VAR=EXP, CI=95); %LET LBCI=(100 - &CI)/2 ; %LET UBCI= &CI + &LBCI ; PROC UNIVARIATE DATA=WORK.&DATA NOPRINT; CLASS S ; VAR &a...

macros that build macros
Back before C++ templates, I was taught a trick whereby one built C macros that built other C macros. I can't remember how the heck we did it. The problem is that there seems to be no standard way to include a '#' or '#define' in the output of a macro. The other problem is that we might have been using some sort of non-standard preprocessor trick to do it. Obliged if anyone can point me in the right direction or tell me what I'm trying to do is impossible. -thant On Sep 11, 11:08 am, Thant Tessman <a...@standarddeviance.com> wrote: > Back before C++ te...