Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 12, 2004 foreach | ||||
---|---|---|---|---|
| ||||
Im trying to iterate through a char[][] with foreach. It prints the correct amount of index's of "ch", but there is no value there. ie: the following code prints c = c = c = names is a char[] --------------------------------- char[][] ch = split(names,"\n"); foreach(char[] c; ch) { printf("c = \n", c); } Any help is appreciated. Phill. |
March 12, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | Thats strange....... ie: this prints fine printf(c); but this doesnt: printf("c = \n", c); Maybe I dont understand this printf :o(( Phill. "Phill" <phill@pacific.net.au> wrote in message news:c2tgjg$2pqj$1@digitaldaemon.com... > Im trying to iterate through a char[][] > with foreach. > > It prints the correct amount of index's of "ch", but there is no value there. > > ie: the following code prints > > c = > c = > c = > > names is a char[] > --------------------------------- > char[][] ch = split(names,"\n"); > foreach(char[] c; ch) > { > printf("c = \n", c); > > } > > Any help is appreciated. > > Phill. > > > > |
March 12, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | On Sat, 13 Mar 2004 10:29:45 +1100, Phill wrote:
> Thats strange.......
>
> ie: this prints fine
> printf(c);
>
> but this doesnt:
> printf("c = \n", c);
>
> Maybe I dont understand this printf :o((
>
> Phill.
you forgot %c:
printf("c = %c\n", c);
Ant
|
March 12, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | I tried that but it doesnt print correctly ie: It prints this: c = ? c = c = # c = $ c = % c = ! c = ) c = c = c = c = % c = c = ( c = ( c = $ c = , c = , c = ! c = ? c = ? c = " c = ! c = $ c = & c = ? c = T ------------- instead of this: ---------------------- 200 digitalmars.com InterNetNews NNRP server INN 2.3.2 ready (posting ok). Sending List request..... 215 Newsgroups in form "group high low flags". c++ 0000003792 0000000001 y c++.stl 0000000083 0000000001 y c++.stl.hp 0000000010 0000000001 y c++.stl.sgi 0000000039 0000000001 y c++.stl.port 0000000176 0000000001 y c++.idde 0000000402 0000000001 y c++.command-line 0000000402 0000000001 y c++.rtl 0000000059 0000000001 y c++.mfc 0000000347 0000000001 y c++.atl 0000000041 0000000001 y c++.announce 0000000594 0000000001 y c++.dos 0000000217 0000000001 y c++.dos.16-bits 0000000156 0000000001 y c++.dos.32-bits 0000000416 0000000001 y c++.windows 0000000006 0000000001 y c++.windows.16-bits 0000000071 0000000001 y c++.windows.32-bits 0000000757 0000000001 y c++.chat 0000000348 0000000001 y D 0000025479 0000000001 y D.gnu 0000000488 0000000001 y DMDScript 0000000040 0000000001 y c++.beta 0000000357 0000000001 y c++.stlsoft 0000000188 0000000001 y c++.wxwindows 0000000172 0000000001 y It ok because printf(c) is fine for me I just wanted to see what I was getting back from the server, to see what I was dealing with. I would be interested to find out the reason it is printing that though. I dont know much C/C++ so I only know that basics of printf. Phill. ."Ant" <duitoolkit@yahoo.ca> wrote in message news:pan.2004.03.12.23.11.53.688428@yahoo.ca... > On Sat, 13 Mar 2004 10:29:45 +1100, Phill wrote: > > > Thats strange....... > > > > ie: this prints fine > > printf(c); > > > > but this doesnt: > > printf("c = \n", c); > > > > Maybe I dont understand this printf :o(( > > > > Phill. > > you forgot %c: > printf("c = %c\n", c); > > Ant > |
March 13, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | Phill wrote: > Im trying to iterate through a char[][] > with foreach. > > It prints the correct amount of index's of "ch", but there is no value > there. > > ie: the following code prints > > c = > c = > c = > > names is a char[] > --------------------------------- > char[][] ch = split(names,"\n"); > foreach(char[] c; ch) > { > printf("c = \n", c); Try: printf("%.*s = \n", c); It's a char[] so "%.*s" it the proper format string. (See also http://www.wikiservice.at/d/wiki.cgi?HowTo/printf) > > } > > Any help is appreciated. > > Phill. Here's a full example, so that everyone can try it out: import std.string; void main() { char[] names = "200 digitalmars.com InterNetNews NNRP server INN 2.3.2 ready (posting ok). Sending List request..... 215 Newsgroups in form \"group high low flags\". c++ 0000003792 0000000001 y c++.stl 0000000083 0000000001 y c++.stl.hp 0000000010 0000000001 y c++.stl.sgi 0000000039 0000000001 y c++.stl.port 0000000176 0000000001 y c++.idde 0000000402 0000000001 y c++.command-line 0000000402 0000000001 y c++.rtl 0000000059 0000000001 y c++.mfc 0000000347 0000000001 y c++.atl 0000000041 0000000001 y c++.announce 0000000594 0000000001 y c++.dos 0000000217 0000000001 y c++.dos.16-bits 0000000156 0000000001 y c++.dos.32-bits 0000000416 0000000001 y c++.windows 0000000006 0000000001 y c++.windows.16-bits 0000000071 0000000001 y c++.windows.32-bits 0000000757 0000000001 y c++.chat 0000000348 0000000001 y D 0000025479 0000000001 y D.gnu 0000000488 0000000001 y DMDScript 0000000040 0000000001 y c++.beta 0000000357 0000000001 y c++.stlsoft 0000000188 0000000001 y c++.wxwindows 0000000172 0000000001 y"; char[][] ch = split(names,"\n"); foreach(char[] c; ch) { printf("%.*s = \n", c); } } -- Justin http://jcc_7.tripod.com/d/ |
March 13, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | Phill wrote:
[...]
> foreach(char[] c; ch)
> {
> printf("c = \n", c);
> }
[...]
printf("c = %.*s\n", c);
There are some conventions most programmers trust in:
`i' is a name used for a variable of integer type containing running
content
`n' is a name used for a variable of integer type containing an upper bound
`c' is a name used for a variable of character type
...
So long.
|
March 13, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | On Sat, 13 Mar 2004 10:49:45 +1100, Phill <phill@pacific.net.au> wrote: > I tried that but it doesnt print correctly ie: > It prints this: > [...] > ------------- > instead of this: > ---------------------- [...] Thats because c is of type char[]. instead of: printf("c = \n",c); use this: printf("c = %*.s\n",c); cya, Andrew |
March 13, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Edwards | On Fri, 12 Mar 2004 23:06:47 -0500, Andrew Edwards <remove_ridimz@remove_yahoo.com> wrote: > On Sat, 13 Mar 2004 10:49:45 +1100, Phill <phill@pacific.net.au> wrote: > >> I tried that but it doesnt print correctly ie: >> It prints this: >> > [...] >> ------------- >> instead of this: >> ---------------------- > [...] > > Thats because c is of type char[]. > > instead of: printf("c = \n",c); > > use this: printf("c = %*.s\n",c); > > cya, > Andrew sorry, that should be %.*s -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
March 13, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | "Manfred Nowak" <svv1999@hotmail.com> wrote in message news:c2tml8$383$1@digitaldaemon.com... > Phill wrote: > > [...] > > foreach(char[] c; ch) > > { > > printf("c = \n", c); > > } > [...] > > printf("c = %.*s\n", c); > > There are some conventions most programmers trust in: > > `i' is a name used for a variable of integer type containing running > content > `n' is a name used for a variable of integer type containing an upper bound > `c' is a name used for a variable of character type > ... > > So long. > Yes, and??? Phill |
March 13, 2004 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | On Sat, 13 Mar 2004 15:31:51 +1100, Phill wrote:
>
> "Manfred Nowak" <svv1999@hotmail.com> wrote in message news:c2tml8$383$1@digitaldaemon.com...
>> Phill wrote:
>>
>> [...]
>> > foreach(char[] c; ch)
>> > {
>> > printf("c = \n", c);
>> > }
>> [...]
>>
>> printf("c = %.*s\n", c);
>>
>> There are some conventions most programmers trust in:
>>
>> `i' is a name used for a variable of integer type containing running
>> content
>> `n' is a name used for a variable of integer type containing an upper
> bound
>> `c' is a name used for a variable of character type
>> ...
>>
>> So long.
>>
>
> Yes, and???
>
> Phill
that's why my suggestions was wrong
I saw 'char' and 'c' so my brain ignored the '[]'
(actually I just looked at the printf and saw 'c')
You wouldn't do:
char[] myCharacter;
char myString;
would you?
Ant
|
Copyright © 1999-2021 by the D Language Foundation