Comment On Global Spaces

Global variables have gotten a bad wrap. Like goto, comefrom, they are often considered harmful. Not everyone thinks action at a distance is spooky. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Global Spaces

2007-02-16 09:07 • by Anonymous (unregistered)
So what does this CAPTCHPA thing mean on all the posts anyway? Is it a reference to some previous article or something?

Actually I might have just figured it out.

CAPTCHA: muhahaha

Re: Global Spaces

2007-02-16 09:07 • by Bobby Knight (unregistered)
No.

Re: Global Spaces

2007-02-16 09:11 • by Will (unregistered)
How about using pSpaceChar as a "/t".

Like:

string Spacer(string a, string b, string c) {

int N = 8; //Set the width of the tab.

return a & pSpaceChar(N - len(a)) & b & pSpaceChar(N - len(b)) & c & pSpaceChar(N - len(c));
}

Just hope that a, b, and c are less than 8 characters long. Hell, you could even write a check for that. Hmm, how convoluted could that check be? Nested ifs? State machine?

Re: Global Spaces

2007-02-16 09:11 • by benryves
I use a function (rather than array) to return X spaces to help with aligning columns in a console app. Not a very exciting use, I'm afraid.

Re: Global Spaces

2007-02-16 09:13 • by TonyL (unregistered)
Expand(string expression) {

for (int i=0; i<8; i++) {
for (int j=0; j<expression.length(); j++) {
printf("%s%s", expression[j], pSpaceChar[i], );
}
printf("\n");
}
}

inspiration:

Re: Global Spaces

2007-02-16 09:15 • by DJ Hirko (unregistered)
How about it being a char * instead of a const char *, too? String literals are const char *'s.

Re: Global Spaces

2007-02-16 09:19 • by rien (unregistered)
the python language is famous for delimiting blocks by their indentation.

if you try to write a python interpreter in python, you might end up writing something like this:


space_chars = [" ", " ", " ", " ", " ", ""]

line = input( ">>>" )
for indent_spaces in space_chars:
if line.replace( "\t", " ").startswith( indent_spaces ):
indent_level = len( space_chars ) - space_chars.index( indent_spaces ) - 1
break


this is the most innefficient way of finding indentation ever written on earth, but a fair use of the described technique...

Re: Global Spaces

2007-02-16 09:20 • by BunnyStrider (unregistered)
120963 in reply to 120956
I've been wondering about the captcha too...how do you add do your word, or do you just add it in your comment for the fun of it..

Re: Global Spaces

2007-02-16 09:25 • by Rob (unregistered)
120964 in reply to 120961
In C string literals have type char *, not const char *, you just aren't allowed to modify them.

Re: Global Spaces

2007-02-16 09:27 • by TimmyT (unregistered)
120966 in reply to 120963
The Captcha was *waffles* dammit.

Re: Global Spaces

2007-02-16 09:29 • by TheMagic8Ball (unregistered)
It was my understanding that space was ever expanding. So pSpaceChar should not be a static array. It needs to be dynamic to account for more space.

Re: Global Spaces

2007-02-16 09:31 • by Balock
Why, I use that exact code to display my Christmas tree every year!


#include <stdio.h>
#include <stdlib.h>

char *pSpaceChar[8] = {
"", // No Space Characters
" ", // One Space Character
" ", // Two Space Characters
" ", // Three Space Characters
" ", // Four Space Characters
" ", // Five Space Characters
" ", // Six Space Characters
" " }; // Seven Space Characters

char *pStarChar[8] = {
"", // No Star Characters
"*", // One Star Character
"**", // Two Star Characters
"***", // Three Star Characters
"****", // Four Star Characters
"*****", // Five Star Characters
"******", // Six Star Characters
"*******" }; // Seven Star Characters

int main()
{
int i;
for(i=0;i<8;i++)
{
printf("%s%s%s%s\n",pSpaceChar[7-i],pStarChar[i],pStarChar[i],pSpaceChar[7-i]);
}
for(i=0;i<2;i++)
{
printf("%s%s%s%s\n",pSpaceChar[7-1],pStarChar[1],pStarChar[1],pSpaceChar[7-1]);
}
printf("Merry Christmas!\n");
}



**
****
******
********
**********
************
**************
**
**
Merry Christmas!

Re: Global Spaces

2007-02-16 09:37 • by sir_flexalot
How about using it as a multiplication teaching tool? You could also make some really gross uses of the old VB String function.

What's 5*5 kids?
hmmm: String(5, pSpaceChar[5]) == String(25, " ")

Re: Global Spaces

2007-02-16 09:37 • by Jeff Alexander (unregistered)
Perhaps it could be used as part of a code generator for the Whitespace language. Something like Add being equivalent to pSpaceChar[2].

http://compsoc.dur.ac.uk/whitespace

Re: Global Spaces

2007-02-16 09:38 • by Cthulhu

//need to print out the cube of all numbers from 0 to 99
//right-aligned
for (int i=0;i<100;i++)
{
int cubed_value= i * i * i;
std::string str_value = convertTo<string>(cubed_value);
//print the value right aligned..
cout << pSpaceChar[7-str_value.size()] << str_value << "\n";
}

Re: Global Spaces

2007-02-16 09:41 • by Spoe (unregistered)
Why not this?
sprintf(pDuration, "%-*s", pPointer, ((9 - iLength) * 2) + strlen(pPointer));


Or:
sprintf(pDuration, "%*s%s", " ", (9-iLength) * 2, pPointer);


Oh, I know, I know, not what we were looking for. We were looking for ways to use pSpaceChar.

Re: Global Spaces

2007-02-16 09:42 • by Anonymous coward (unregistered)
120976 in reply to 120956
The code is bugged. It should look that way:
  char *pSpaceChar[8] = {

"", // No Space Characters
" ", // One Space Character
" ", // Two Space Characters
" ", // Two Space Characters - fixed for customer #4424
" ", // Four Space Characters
" ", // Five Space Characters
" ", // Six Space Characters
"-1" }; // Seven Space Characters - fixed for bug #32345

Re: Global Spaces

2007-02-16 09:44 • by Martin (unregistered)
Obviously, this only makes sense when combine with other WTFs.


function SuperMan( int x )
{
int i;

for ( i == 0; i < x; i++ )
{
switch (i)
{
case 0: return strlen( pSpaceChar[i] );
case 1: return strlen( pSpaceChar[i] );
case 2: return strlen( pSpaceChar[i] );
case 3: return strlen( pSpaceChar[i] );
case 4: return strlen( pSpaceChar[i] );
case 5: return strlen( pSpaceChar[i] );
case 6: return strlen( pSpaceChar[i] );
case 7: return strlen( pSpaceChar[i] );

default:
return MAYBE;
}
}
}

Re: Global Spaces

2007-02-16 09:50 • by MrMurf
Pfft - this needs to be migrated to a J2EE Platform.

I offer forth the WhiteSpaceGenerator! It's even thread-safe!


public class WhitespaceGenerator {

public static String[] whitespaces = new String[] {
"",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};

public static String getWhitespace(int thisManySpaces) {
StringBuffer sb = new StringBuffer();
synchronized (whitespaces) {
while ( thisManySpaces >= (whitespaces.length-1) ) {
sb.append(whitespaces[whitespaces.length-1]);
thisManySpaces -= (whitespaces.length-1);
}
sb.append(whitespaces[thisManySpaces]);
}
return sb.toString();
}
}



Re: Global Spaces

2007-02-16 09:55 • by Anonymous (unregistered)
Hmm, 8 different strings... Surely this is some (even more!) esoteric dialect of brainfuck?

Re: Global Spaces

2007-02-16 09:57 • by masklinn
120980 in reply to 120962
rien:
if line.replace( "\t", " ").startswith( indent_spaces )


HAH!

YOU WISH!
(I'm pretty sure the standard CPython interpreter considers a \t to be equivalent to 8 spaces)

Re: Global Spaces

2007-02-16 09:57 • by n (unregistered)
120981 in reply to 120977
Martin:
Obviously, this only makes sense when combine with other WTFs.


function SuperMan( int x )
{
int i;

for ( i == 0; i < x; i++ )
{
switch (i)
{
case 0: return strlen( pSpaceChar
...
default:
return MAYBE;
}
}
}


this one gets my vote so far.

Re: Global Spaces

2007-02-16 09:58 • by Alchymist
Isn't it obvious! Someone is trying to create an octal calculator. Just define an array of char * pointers long enough to hold the number you want, and set each pointer to the relevant element in the spaces array. For example

char *Number1[MAXDIGITS] = {pSpaceChar[4], pSpaceChar[2], ""}; //024
char *Number2[MAXDIGITS] = {pSpaceChar[7], pSpaceChar[5], ""}; //057
char *Number3[MAXDIGITS] = {""};

Addition is performed by concatenation, followed by reducing the length by 8 & carrying pSpaceChar[1], if necessary.


const int MAXDIGITS = 3;
int Add (char **result, char **num1, char **num2);


char *pSpaceChar[8] = {
"", // No Space Characters
" ", // One Space Character
" ", // Two Space Characters
" ", // Three Space Characters
" ", // Four Space Characters
" ", // Five Space Characters
" ", // Six Space Characters
" " }; // Seven Space Characters

int main()
{

char *Number1[MAXDIGITS] = {pSpaceChar[4], pSpaceChar[2], pSpaceChar[0]};
char *Number2[MAXDIGITS] = {pSpaceChar[7], pSpaceChar[5], pSpaceChar[0]};
char *Number3[MAXDIGITS] = {pSpaceChar[0],pSpaceChar[0],pSpaceChar[0]};

Add(Number3, Number1, Number2);

return 0;

}


int Add (char **result, char **num1, char **num2)
{
char Sum[15] = "";
int carry = 0;
int val = 0;

for (int j=0; j<MAXDIGITS; j++)
{
val = sprintf(Sum, "%s%s%s", num1[j], num2[j], result[j]);

if (val > 7)
{
result[j] = pSpaceChar[val-8];
if (j==MAXDIGITS-1)
carry = 1;
else
result[j+1] = pSpaceChar[1];
}
else
result[j] = pSpaceChar[val];

memset(Sum, 0, 15);
}

return carry;
}




Subtraction is just reverse addition.
Other operators left up to the reader.

I'd go into more detail but lunch break is over.

Disclaimer: Hey - I wrote the code above quickly. Forgive any WTFs, please.

Re: Global Spaces

2007-02-16 10:00 • by AP (unregistered)
Perhaps this is used in source code generator for the Whitespace programming langauge.

Re: Global Spaces

2007-02-16 10:01 • by newt0311 (unregistered)
120984 in reply to 120964
Rob:
In C string literals have type char *, not const char *, you just aren't allowed to modify them.

no, char * are perfectly modifiable. Its done all the time. all that a char * means is an array of characters and is the closest C comes to a string. a const char * is also a valid declaration which will cause the compiler to raise an error if any attempt is made to modify it. gcc also gives a warning when a const is assigned to a non-const without a specific declaration to go along with it.

Re: Global Spaces

2007-02-16 10:02 • by ort (unregistered)
120985 in reply to 120961
Technically they are char[N]

Re: Global Spaces

2007-02-16 10:02 • by n (unregistered)
120986 in reply to 120981
i am a total tard and can't format comments. "this one gets my vote so far" should have been outside of the quote. kill me.

Re: Global Spaces

2007-02-16 10:03 • by JohnB (unregistered)
120988 in reply to 120976
Anonymous coward:
The code is bugged. It should look that way:
  char *pSpaceChar[8] = {

"", // No Space Characters
" ", // One Space Character
" ", // Two Space Characters
" ", // Two Space Characters - fixed for customer #4424
" ", // Four Space Characters
" ", // Five Space Characters
" ", // Six Space Characters
"-1" }; // Seven Space Characters - fixed for bug #32345


This is quite likely the drollest comment that has ever appeared here. Absolutely delightful!

Re: Global Spaces

2007-02-16 10:14 • by akatherder
The only combination of factors that could be attributed to this code:

1. Very specifically formatted outputs
2. Poor understanding of a language
3. Slight intellectual impairment or alcohol

Re: Global Spaces

2007-02-16 10:15 • by Trinian (unregistered)
char *pSpaceCount[8] = {
...
" ", // One Space Character, AH AH AH!
" ", // Two Space Characters, AH AH AH!
" ", // Three Scintillating Space Characters, AH AH AH!
...
};

Re: Global Spaces

2007-02-16 10:16 • by nobody (unregistered)
As I think some commenters have been trying to say, this would be useful for converting tabs to spaces.
8-printPos%8 would give the number of spaces to add to go to the next tab after printPos (assuming printPos is positive, and you really want to go to the next tab if it is already at a tab), and instead of adding that number of spaces, just index into this array.

Not that it would save much time, but it isn't that much of a WTF if it is used that way.

Re: Global Spaces

2007-02-16 10:20 • by wgh
120992 in reply to 120960
TonyL,

Ha, I got that same forward just this morning. I thought the elephant one was hilarious also.

Re: Global Spaces

2007-02-16 10:25 • by JBQ (unregistered)
I guess they don't know about the %.*s printf format...

Re: Global Spaces

2007-02-16 10:26 • by skztr
Well, perhaps I'm wtfing. I did this a couple months ago. (not global)


...
char acRec[100];
char *pDashes = "----------------------------------------"
"----------------------------------------";
char *pSpaces = " "
" ";
...
sprintf(acRec, "/*%.66s*/", pDashes);
COMM_sMsg(pJcl, 34, 1, RC00, acRec);

...

sprintf(acRec, "/*%.66s*/", pSpaces);
COMM_sMsg(pJcl, 37, 1, RC00, acRec);

sprintf(acRec, "/*%.25s%-16s%.25s*/",
pSpaces, "Activity Report", pSpaces);
COMM_sMsg(pJcl, 38, 1, RC00, acRec);

sprintf(acRec, "/*%.66s*/", pSpaces);
COMM_sMsg(pJcl, 39, 1, RC00, acRec);
...


If I'm WTFing here, let me know. I personally thought it was better than what this was written to replace (which just put the dashes and spaces inline, repeated 600 times), but if there's a still better way, I'll be glad to hear it.

Other WTF's are probably evident from this code ("why not just write a formatting function?", "Those incrementing numbers.. are they for a purpose as stupid as they seem to be?", "What the hell is an RC00...? is that a named constant for a specific literal number??", "Why are you outputting what looks like C comments in the first place?", etc), but I'd like to think those aren't mine. The use of pSpaces/pDashes itself, I don't see a problem with.

As for using an array, that's just stupid. Were I to do what was being done in the supplied code, I'd probably go with something like:

char *pSpaces = " "
" ";

#define SPACE(n) (&(pSpaces[80-(n)]))

Re: Global Spaces

2007-02-16 10:28 • by HappyPanda (unregistered)
120996 in reply to 120977
Martin:
Obviously, this only makes sense when combine with other WTFs.


function SuperMan( int x )
{
int i;

for ( i == 0; i < x; i++ )
{
switch (i)
{
case 0: return strlen( pSpaceChar[i] );
case 1: return strlen( pSpaceChar[i] );
case 2: return strlen( pSpaceChar[i] );
case 3: return strlen( pSpaceChar[i] );
case 4: return strlen( pSpaceChar[i] );
case 5: return strlen( pSpaceChar[i] );
case 6: return strlen( pSpaceChar[i] );
case 7: return strlen( pSpaceChar[i] );

default:
return MAYBE;
}
}
}


You forgot to define MAYBE. I suggest:

MAYBE = "true"

Re: Global Spaces

2007-02-16 10:36 • by StupidGuy (unregistered)
The real WTF is the unreadability...

I think
"5[pSpaceChar]" would be better readable than "pSpaceChar[5]";

Back to the example..
It's just a form of loop-unrolling... Defining often used data first... Classic optimisation :-)

Re: Global Spaces

2007-02-16 10:37 • by Jailors Suit Ya (unregistered)
pSpaceChar is the toilet.

Re: Global Spaces

2007-02-16 10:39 • by dkf (unregistered)
120999 in reply to 120984
newt0311:
Rob:
In C string literals have type char *, not const char *, you just aren't allowed to modify them.

no, char * are perfectly modifiable.
But Rob was talking about string literals, which are often in read-only memory (irrespective of what the type decorations say...)

Re: Global Spaces

2007-02-16 10:42 • by Rob (unregistered)
121001 in reply to 120984
newt0311:
Rob:
In C string literals have type char *, not const char *, you just aren't allowed to modify them.

no, char * are perfectly modifiable.


Try re-reading my post, I said *string literals* couldn't be modified.

Re: Global Spaces

2007-02-16 10:44 • by Zylon
121002 in reply to 120956
Anonymous:
So what does this CAPTCHPA thing mean on all the posts anyway? Is it a reference to some previous article or something?

It's an intelligence test. Anyone who includes their captcha in their post, fails.

Re: Global Spaces

2007-02-16 10:47 • by BitTwiddler (unregistered)
121004 in reply to 120996
No, no, no...

The default case should return FILE_NOT_FOUND. Don't you people know ANYTHING about programming???

Re: Global Spaces

2007-02-16 10:54 • by akatherder
121006 in reply to 121002
Zylon:

It's an intelligence test. Anyone who includes their captcha in their post, fails.


I'd say anyone who includes it would pass with a D+. Anyone who tries to make a clever quip about their captcha FAILS.

Captcha: Cognac. At this time of the morning?! It's is early but I guess I will drink some!! ROFL

Re: Global Spaces

2007-02-16 11:04 • by anonymous guy (unregistered)
I think the best use would be as an entry in a personnel file listing reasons for termination. You know, exceeded sick leave and doesn't show up for work, when he does show up he's drunk, and the following code. That way he doesn't get unemployment compensation.

Actually, I liked the Christmas Tree. If there's a prize, give it to him.

Re: Global Spaces

2007-02-16 11:05 • by Rob (unregistered)
121009 in reply to 120960
hah hah! Loved this one.

Re: Global Spaces

2007-02-16 11:14 • by anonymous (unregistered)
here's my captcha:

/´¯/)
,/¯ /
/ /
/´¯/' '/´¯¯·¸
/'/ / / /¨¨/¯\
('( ´ ´ ¯ ;/' ')
\ ' /
'\' \ _.·´
\ (
\ \

Re: Global Spaces

2007-02-16 11:15 • by foo (unregistered)
A construct like this:


char *pSpaceChar[8] = {
"", // No Space Characters
" ", // One Space Character
" ", // Two Space Characters
" ", // Three Space Characters
" ", // Four Space Characters
" ", // Five Space Characters
" ", // Six Space Characters
" " }; // Seven Space Characters



can be used for any base 7 arithmetic that you might imagine. What we have defined for us here is a representation of the numbers 0 through 7 which is Octal. So I have written a decimal to octal converter...

My example in Java since my C is quite rusty...

public class OctalConverter {
public static String[] spaceArray = new String[] {
"",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};



public static String convert(int number) {

ArrayList<String> sNum = new ArrayList<String>();

while ( number > 0 ) {
int digit = number % spaceArray.length;
number = Math.floor(number / 10);
sNum.add(spaceArray[digit])
}

String octal = "";
for(int ii = sNum.size() - 1; ii>=0; ii--) {
String spaceDigit = sNum.get(ii);
int digit = spaceDigit.length();
octal += Integer.toString(digit);
}
return octal;
}

}


... and no, I didn't test it. This is off the top of my head so YMMV. Need a new numerical base? Change the array definition. Use a HashMap for Hexidecimal.

--Zarf aka Foo

Re: Global Spaces

2007-02-16 11:16 • by anonymous (unregistered)

/´¯/)
,/¯ /
/ /
/´¯/' '/´¯¯·¸
/'/ / / /¨¨/¯\
('( ´ ´ ¯ ;/' ')
\ ' /
'\' \ _.·´
\ (
\ \

Re: Global Spaces

2007-02-16 11:17 • by anonymous (unregistered)

/´¯/)
,/¯ /
/ /
/´¯/' '/´¯¯·¸
/'/ / / /¨¨/¯\
('( ´ ´ ¯ ;/' ')
\ ' /
'\' \ _.·´
\ (
\ \

Re: Global Spaces

2007-02-16 11:20 • by anonymous (unregistered)

___
,'_ "`-. ,-'"`-.
('"/"-. \ / ,-,. \
`' \ ,-'- / / `-'
,-'-. /
__ ("|") f
(_)`-"---. |
l ---. j
`---' ,'
\ f
) l
__f _Y
,'",-'"_" l
(,,(,,,' ` Y
| l
| \,';,
l , Y, ;
(`._( ),'
`. `. (
,--',--' )
(,,,(,,,---'

captcha: howdy - the rabbit says 'HOWDY'!

Optimization

2007-02-16 11:20 • by David (unregistered)
static char *sevenSpaces = "       ";

char *pSpaceChar[8] = {
eightSpaces + 7,
eightSpaces + 6,
eightSpaces + 5,
eightSpaces + 4,
eightSpaces + 3,
eightSpaces + 2,
eightSpaces + 1,
eightSpaces
};
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment