[Tutor] Grep equiv.

amoreira@mercury.ubi.pt amoreira@mercury.ubi.pt
Thu, 31 Aug 2000 23:23:56 +0100

Hi there!
Scott already answered about the trouble with "if result!='-1'". let me
try to answer your other questions.

Wes Bateman wrote:

> I tried to use string.splitfields(line) to break it up, but it used a
> whitespace delimeter and divided each line up.  If I did this the
> string.find choked on the list variable type.  Which raises other
> questions I have about how I can perform a particular function on all of
> the items in a list ( variable[0], variable[1], variable[2], etc.) in one
> pass?

This is the map function:
>>> l=[0,1,2,3,4]
>>> def f(x):
...    return x**2
>>> map(f,l)
[0, 1, 4, 9, 16]

> Further, what would be the preferred way to suck a file in, put each line
> in a variable, and each field in each line inside of that?  I understand
> that I can nest lists, so maybe like file[0] is first line of file and
> inside of that there could be several fields?  How would I reference them
> and how could I get them into a structure like that?

The way you read it in your script dows just that, but you have to
assign to a variable to keep it:
The hole file was read and you now have a list called lines such that
lines[0] is the fisrt line of the file, line[1] is the second line of
the file, and so on. There are other ways as well. Check the tutorial.
Wich one is best depends on what you want to do and what you want to do
it to. For instance, it's probably not a good idea to read in one deep
gulp a 2 million line file. That's what happens if you use readlines to
read it.

> Eventually I want to take say the third field from each line that matches
> my "grep-like" function and add them.

You can add the 3rd fields of each line with

import string
for line in lines:   #lines is the list read above
    s=s+ float(string.split(lines)[2])

This assumes that the fields are separated with whitespace.
string.split splits line into fields, and field #3 is is the one with
index 2
(1st position has index 0, 2nd position has index 1..... n-th position
has index n-1). float converts the string into a number, it possible.
That is:
float('3') returns 3.0; float('hello!') returns an error.

Cheers, and good luck!