www.digitalmars.com         C & C++   DMDScript  

D - foreach

reply "Phill" <phill pacific.net.au> writes:
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.
Mar 12 2004
next sibling parent reply "Phill" <phill pacific.net.au> writes:
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.
Mar 12 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
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
Mar 12 2004
parent reply "Phill" <phill pacific.net.au> writes:
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 = 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
Mar 12 2004
parent reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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
Mar 12 2004
parent reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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/
Mar 12 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Andrew Edwards wrote:

 sorry, that should be %.*s
I always get these mixed up to. I came up with a poem (or whatever you call it): star before s but not before dot -- -Anderson: http://badmama.com.au/~anderson/
Mar 13 2004
parent John Reimer <jjreimer telus.net> writes:
J Anderson wrote:

 Andrew Edwards wrote:
 
 sorry, that should be %.*s
I always get these mixed up to. I came up with a poem (or whatever you call it): star before s but not before dot
I know. I've had the same trouble remembering it. Then I recalled that if one thinks of s as representing a pointer type then its easy to remember that the asterisk should be right next to "variable" (for those familiar with C/C++ at least). That has helped me. Later, John
Mar 14 2004
prev sibling next sibling parent reply J C Calvarese <jcc7 cox.net> writes:
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/
Mar 12 2004
parent "Phill" <phill pacific.net.au> writes:
Thanks for the URL Justin, its great!

Phill.

"J C Calvarese" <jcc7 cox.net> wrote in message
news:c2tli3$12e$1 digitaldaemon.com...
 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/
Mar 13 2004
prev sibling next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
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.
Mar 12 2004
parent reply "Phill" <phill pacific.net.au> writes:
"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
Mar 12 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
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
Mar 12 2004
parent reply "Phill" <phill pacific.net.au> writes:
Expanation:

At first I actually tried to read each character
from the char[] which is returned , but then I decided that I would use
split(which as you know returns a char[][]) so that I could read the whole
line(group name) in a char[] instead of a char at a time, unfortunately when
I changed from char to char[] I left the char[] name as c because I was in a
hurry to test and partly because I am lazy :o))

Phill.


"Ant" <duitoolkit yahoo.ca> wrote in message
news:pan.2004.03.13.04.22.24.989634 yahoo.ca...
 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
Mar 12 2004
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Phill wrote:

[...]
 because I was in a hurry to test and partly because I am lazy
Do you agree then, that your conduct may be a suitable object of negative criticism? Instead of reasoning over your code yourself, you was so lazy to post to the world a fact about `foreach' whereas you had no evidence, that your code worked without the `foreach', thereby allocating the time of at least three helpful and unlazy people, not accounting those who already spent or will spend time to read your posting and the answers for only recognizing, a person in a hurry, that was drinking coffee while the world started circling around its generated non issue. How do you want to pay that back? So long.
Mar 13 2004
parent reply "Phill" <phill pacific.net.au> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c2uhre$1fdg$1 digitaldaemon.com...
 Phill wrote:

 [...]
 because I was in a hurry to test and partly because I am lazy
Do you agree then, that your conduct may be a suitable object of negative criticism?
only to a pedant.
 Instead of reasoning over your code yourself, you was so lazy to post to
 the world a fact about `foreach' whereas you had no evidence, that your
 code worked without the `foreach', thereby allocating the time of at least
 three helpful and unlazy people, not accounting those who already
 spent or will spend time to read your posting and the answers for only
 recognizing, a person in a hurry, that was drinking coffee while the world
 started circling around its generated non issue.
If you READ my post and READ the code you will see that the only REAL problem with my code was I had this: printf("c = \n",c); instead of this: printf("c = %*.s\n",c); The reason that I used this: printf("c = \n",c); Is because I did not know about this: printf("c = %*.s\n",c); My lack of knowledge of the formatting of strings is as I stated in an earlier post because I have hardly any programming experience in C or C++. I only know Java, but I am trying to learn this D language. As this is about the only place one can come to get any questions answered about the use of it, this is where I come. You bitching about peoples lack of knowledge is not going to do anything for the popularity of D, so why dont you get off you high horse......... So long!
 How do you want to pay that back?

 So long.
Mar 13 2004
parent reply John Reimer <jjreimer telus.net> writes:
 You bitching about peoples lack of knowledge is not going to do anything
 for the popularity of D, so why dont you get off you high horse.........
 
 So long!
 
 
Phil, don't worry yourself too much about responses such as Manfred's. Your question was innocent enough. No one is forced to read or even answer posts, so whoever complains the way he did has to take responsibility for themselves. Just give him the benefit of the doubt and assume that he had a stressful day and was tired. It happens to everyone now and again. Actually, cranky retorts are quite rare in this D group, thank goodness :-). In truth, the way Manfred wrote his response, I almost thought he was joking around. It was rather a mouthful to read, consisting of one looooong sentence linked together with several clauses. Try reading it aloud without taking a breath ;-). Peacefully, John
Mar 13 2004
parent "Phill" <phill pacific.net.au> writes:
"John Reimer" <jjreimer telus.net> wrote in message
news:pan.2004.03.13.09.51.20.547117 telus.net...
 You bitching about peoples lack of knowledge is not going to do anything
 for the popularity of D, so why dont you get off you high horse.........

 So long!
Phil, don't worry yourself too much about responses such as Manfred's. Your question was innocent enough. No one is forced to read or even answer posts, so whoever complains the way he did has to take responsibility for themselves. Just give him the benefit of the doubt and assume that he had a stressful day and was tired. It happens to everyone now and again. Actually, cranky retorts are quite rare in this D group, thank goodness
:-).
 In truth, the way Manfred wrote his response, I almost thought he was
 joking around.  It was rather a mouthful to read, consisting of one
 looooong sentence linked together with several clauses.  Try reading it
 aloud without taking a breath ;-).
Yes, its very similar to taking the semi colons out of your code isnt it? :o)) Phill.
 Peacefully,

 John
Mar 13 2004
prev sibling parent "Phill" <phill pacific.net.au> writes:
Thanks to everyone!

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.
Mar 12 2004