MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1＆1

# Python, Ruby, Perl: Loop Thru a List

Xah Lee, , …,

## Python

Here is a example of going thru a list by element.

```# -*- coding: utf-8 -*-
# python

aa = ['one', 'two', 'three', 'infinity']
for xx in aa:
print xx
```

Python provides a way to loop thru a list and give both its index and the item value.

```# -*- coding: utf-8 -*-
# python

bb = ['one', 'two', 'three', 'infinity']
for ii, vv in enumerate(bb):
print ii, vv

# 0 one
# 1 two
# 2 three
# 3 infinity
```

The following construct loops thru a dictionary, each time assigning both keys and values to variables.

```# -*- coding: utf-8 -*-
# python

dd = {'john':3, 'mary':4, 'jane':5, 'vicky':7}
for kk, vv in dd.iteritems():
print kk, ' is ', vv

# jane  is  5
# john  is  3
# mary  is  4
# vicky  is  7
```

## Ruby

Loop thru a list.

```# -*- coding: utf-8 -*-
# ruby

aa = ['one', 'two', 'three', 'infinity']

# loop thru a list
aa.each {    # opening curly bracket must be on this line, else syntax error.
|xx|          # each element is set to a dummy variable xx
p xx          # prints xx
}
```

In the above, the “each” is a method for list object. This method takes a “block” argument in the form of `{ … }`.

A Ruby “block” typically has the form `{ |‹var›| … }` OR `do |‹var›| … end`, where the ‹var› is a dummy variable. Ruby's “block” is similar to a lambda (aka pure function).

When a method takes a “block”, you can think of it as taking a pure function written in the form `{ |‹var›| ‹body› }`.

Loop thru a list, get both index and value.

```# -*- coding: utf-8 -*-
# ruby

aa = ['one', 'two', 'three', 'infinity']

# loop thru a list and gets its value and index
aa.each_with_index do
|vv, ii|                      # first item is value
p vv, ii                      # print value and index
end
```

The following construct loops thru a dictionary, each time assigning both keys and values to variables.

```# -*- coding: utf-8 -*-
# ruby

hh = { :john => 3, :mary => 4, :jane => 5, :vicky => 7}

# loop thru a hash, and get key and value
hh.each do
|kk,vv|
p kk, vv                      # print key and value
end

=begin
prints

:john
3
:mary
4
:jane
5
:vicky
7

=end
```

## Perl

In Perl, looping thru a list is typically done with “for”:

```# -*- coding: utf-8 -*-
# perl

@myList = ('one', 'two', 'three', 'infinity');
for \$xx (@myList) {
print \$xx, "\n";            # print each element
}
```

To loop thru a list using indexs, do like this:

```# -*- coding: utf-8 -*-
# perl

@myList = ('one', 'two', 'three', 'infinity');
for (\$i=0; \$i < scalar(@myList); \$i++) {
print "\$i \$myList[\$i], ";
} # prints: 0 one, 1 two, 2 three, 3 infinity,
```

A typical way to loop thru a hash is:

```# -*- coding: utf-8 -*-
# perl

%myHash = ('john',3, 'mary',4, 'jane',5, 'vicky',7);
for \$key (keys %myHash) {
print "Key and Value pair is: \$key, \$myHash{\$key} \n";
}
```

perldoc perldsc