Changeset 8589 in project


Ignore:
Timestamp:
02/20/08 12:25:07 (12 years ago)
Author:
azul
Message:

Changes applied for azul (193.142.125.1) through svnwiki:

Tweaks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/language-comparison

    r8585 r8589  
    166166== Hash tables
    167167
    168 <table>
    169 <tr>
    170 <td></td>
    171 <td>Chicken</td>
    172 <td>C++ (STL)</td>
    173 <td>Python</td>
    174 <td>Perl</td>
    175 </tr>
    176 
    177 <tr>
    178 <td>Creating a hash table</td>
    179 <td><enscript highlight="scheme">(make-hash-table)</enscript>
     168=== Creating a hash table
     169
     170==== Chicken
     171
     172<enscript highlight="scheme">(make-hash-table)</enscript>
    180173
    181174If you need to populate the hash table, you'll probably use {{alist->hash-table}}:
    182175<enscript highlight=scheme>(alist->hash-table '((1 . "uno") (2 . "dos")))</enscript>
    183176
    184 Note that the list can be created at runtime.</td>
    185 <td><enscript highlight="cpp">map<int, string> my_hash;
     177Note that the list can be created at runtime.
     178
     179==== C++ STL
     180
     181<enscript highlight="cpp">map<int, string> my_hash;
    186182my_hash[1] = "uno";
    187 my_hash[2] = "dos";</enscript></td>
    188 <td></td>
    189 <td>
     183my_hash[2] = "dos";</enscript>
     184
     185==== Perl
     186
    190187<enscript highlight="perl">my %hash = (
    191188  1 => 'uno',
    192189  2 => 'dos'
    193 );</enscript></td>
    194 </tr>
    195 
    196 <tr>
    197 <td>Obtaining an element from a hash</td>
    198 <td><enscript highlight=scheme>(hash-table-ref/default my_hash 2 #f)</enscript>
    199 
    200 This will return #f (false) if the element is not defined.  If you prefer to have an error thrown, you should use:
    201 
    202 <enscript highlight=scheme>(hash-table-ref my_hash 2)</enscript></td>
    203 <td>my_hash[2]</td>
    204 <td></td>
    205 <td><enscript highlight="perl">$my_hash{2}</enscript></td>
    206 </tr>
    207 
    208 <tr>
    209 <td>Printing the keys in the hash</td>
    210 <td>If you can afford to have a list with the keys created, you should do this:
     190);</enscript>
     191
     192=== Obtaining an element from a hash
     193
     194==== Chicken
     195
     196<enscript highlight=scheme>(hash-table-ref/default my_hash 2 #f)</enscript>
     197
     198This will return {{#f}} (false) if the element is not defined.  If you prefer to have an error thrown, you should use:
     199
     200<enscript highlight=scheme>(hash-table-ref my_hash 2)</enscript>
     201
     202==== C++ STL
     203
     204<enscript highlight=cpp>my_hash[2]</enscript>
     205
     206==== Python
     207
     208...
     209
     210==== Perl
     211
     212<enscript highlight="perl">$my_hash{2}</enscript>
     213
     214=== Printing the keys in the hash
     215
     216==== Chicken
     217
     218If you can afford to have a list with the keys created, you should do this:
     219
    211220<enscript highlight=scheme>(format #t "~{~A~}~%" (hash-table-keys my_hash))</enscript>
    212221
     
    216225  my-hash
    217226  (lambda (key value)
    218     (format #f "~A~%" key)))</enscript></td>
    219 <td><enscript highlight="cpp">for(map<int, string>::iterator iter = my_hash.begin();
     227    (format #f "~A~%" key)))</enscript>
     228
     229==== C++ STL
     230
     231<enscript highlight="cpp">for(map<int, string>::iterator iter = my_hash.begin();
    220232    it != my_hash.end(); it++ )
    221233  cout << it->first << endl;</enscript>
    222 </td>
    223 <td></td>
    224 <td><enscript highlight=perl>for my $key ( keys %my_hash ) {
     234
     235==== Python
     236
     237==== Perl
     238
     239<enscript highlight=perl>for my $key ( keys %my_hash ) {
    225240  print "$key\n";
    226 }</enscript></td></tr>
    227 </table>
     241}</enscript>
Note: See TracChangeset for help on using the changeset viewer.