Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 1 and Version 2 of ForeachStringArrExample

Show
Ignore:
Author:
csauls (IP: 69.166.134.113)
Timestamp:
04/09/06 07:58:21 (15 years ago)
Comment:

Updated to use foreach type inference, removed Source link, and updated to use writefln() rather than printf("...\n").

Legend:

Unmodified
Added
Removed
Modified
  • ForeachStringArrExample

    v1 v2  
    1111{{{ 
    1212#!d 
    13 /*  
    14     This alias makes it a little easier to see what's going on with the  
    15     dynamic strings (a/k/a "char[]").  
    16 */ 
    17 alias char[] string;  
    18  
     13import std.stdio; 
    1914 
    2015void main() { 
    2116 
    2217    /* create an associative array called "map" */ 
     18    char[][char[]] map;  
    2319 
    24     string[string] map;  
    25  
    26  
    27     /*  
    28         The associate array is populated like this: 
    29         map[key] = value  
    30     */ 
    31      
    3220    map["abc"] = "apple"; 
    3321    map["def"] = "bee"; 
    3422    map["xyz"] = "cat"; 
    3523 
    36  
    37     /*  
    38         The foreach is used to determine the contents of  
    39         the associative array. 
     24    /*  The foreach is used to determine the contents of the associative array. 
     25        Note that the types of 'key' and 'value' are inferred. 
    4026    */ 
    4127 
    42     printf("Iterating over map...\n"); 
    43     foreach (string key, string value; map)  
    44     { 
    45         printf("\t%.*s => %.*s\n", key, value); 
    46     } 
     28    writefln("Iterating over map..."); 
     29    foreach (key, value; map)  
     30        writefln("\t%s => %s", key, value); 
    4731} 
    4832}}} 
    5135 
    5236This example was inspired on an example posted by csauls and from comments by larsivi. 
    53  
    54 || Link || http://www.dsource.org/tutorials/index.php?show_example=71 || 
    55 || Posted by || jcc7 || 
    56 || Date/Time || Mon May 3, 2004 12:20 am ||