in reply to Re: Twirling baton progress indicator in Perl
in thread Twirling baton progress indicator in Perl

I know how much we all love when people who haven't been using perl for any length of time bemoan that perl code looks like line noise, or that it's a write-only language. So, I think we should at least not do the same in reverse.

At no point when I was writing C would I have imagined using either switches or "pointer arithmetic" for this. I would have done basically the same thing you just did, but in C.

void twirl_baton() { static int position = 0; static char* baton = "-\\|/"; printf("%c\b", baton[position++ % 4]); }
Which is exactly the same, except it allows work to get accomplished while the baton is twirling, as you just keep calling twirl_baton every so often in the code. Which I'm sure you would have done in perl if it weren't that the OP asked specifically about sleep in their misdiagnosis of their problem. Yes, under the covers, baton[position++ % 4] is pointer arithmetic, but abstractly it is no more pointer arithmetic than your perl usage. For example, array usage is taught way before pointers in most C courses and books.

That's not to say that some people didn't learn switch statements with the rotating baton - but my experience doesn't show it to be indicative or cause/effect. I would think this to be more indicative of other languages where arrays aren't so easy to do - e.g., REXX or shell. Or languages where many beginners show up, but never advance to the point of seeing/using arrays (I can think of many QBASIC "programmers" falling into this category - I'm sure there are many VB users as well, although with common usage of VB being GUI, Windows Registry, and database interactions, I would think VB would have a lower percentage of these users than previous iterations of BASIC).

Replies are listed 'Best First'.
Re^3: Twirling baton progress indicator in Perl
by Aristotle (Chancellor) on Oct 11, 2005 at 11:49 UTC

    Make no mistake, I’m not complaining about C. I like the language quite a bit, and so long as I don’t have to monkey around on the heap too much it’s a lot of fun. (I also enjoy writing shell scripts and XSLT… :-))

    Yes, under the covers, baton[position++ % 4] is pointer arithmetic, but abstractly it is no more pointer arithmetic than your perl usage.

    Apologies for the bad wording – what I meant to express was that you are addressing particular characters in something that’s not overtly an array. Although now that I think about it, even an honest-to-goodness character array is quite simple so use, you just do something like

    char[] baton = { '-', '\\', '|', '/' }; int num_batons = sizeof( baton ) / sizeof( baton[0] );

    So yeah, I retract that comment. Still, I’ve seen this class of problem very frequently handled with switch in C – and as I now realize, there’s even less reason to do it that way than I thought, even in C.

    Hrm.

    Makeshifts last the longest.