reading a file and using arrays 
Author Message
 reading a file and using arrays

I have a file of the form

FILE1 345
FILE2 234
FILE3 333
     .
     .

with > 5000 ROWS

and I want to be able to read it in using awk
and assign the 1st column as 1 array and the
second column as another array

i.e. col1 [i] and col2 [j]

I figure that you use getline but I can't assign
the array elements to make it work.

Any suggestions would be great

Mike

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 28 Jul 2002 03:00:00 GMT  
 reading a file and using arrays

Quote:

>I have a file of the form

>FILE1 345
>FILE2 234
>FILE3 333
>     .
>     .

>with > 5000 ROWS

>and I want to be able to read it in using awk
>and assign the 1st column as 1 array and the
>second column as another array

>i.e. col1 [i] and col2 [j]

>I figure that you use getline but I can't assign
>the array elements to make it work.

I don't understand what you want, but how about something like this:

awk '{col1[$1]++; col2[$2]++}
     END {for (i in col1){
              print "col1[" i "] = " col1[i]}
          for (j in col2){
              print "col1[" j "] = " col1[j]}} ' infile

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 28 Jul 2002 03:00:00 GMT  
 reading a file and using arrays
I wasn't to sure what the original post was
asking either. I guessed it'd be something
like:

awk '{col1[NR]=$1;col2[NR]=$2}'

Tristan

Quote:

> I don't understand what you want, but how about something like this:

> awk '{col1[$1]++; col2[$2]++}
>      END {for (i in col1){
>               print "col1[" i "] = " col1[i]}
>           for (j in col2){
>               print "col1[" j "] = " col1[j]}} ' infile



Mon, 29 Jul 2002 03:00:00 GMT  
 reading a file and using arrays
Ok let me explain what I was trying to say in the original post.

I have a file which is made up 2 columns of strings
with each column being approx. 5000 rows.

i.e.

NAME0    200
NAME1    345
     ...
NAME4999 10456

I want to use arrays to label each string in the file.
That is an array which would be of the form
(for column 1)
col1[0] = NAME0, col1[1] = NAME1, ..., col1[4999] = NAME4999
(for column 2)
col2[0] = 200, col2[1] = 345, ..., col2[499] = 10456

The reason I'd like to do this is that I have 2 files
in this format and I would like to compare the elements
of column 1 in file 1 with the elements of column2 in file 2,
and similarly for columns 2.

I hope this explanation is a bit clearer,

Thanks,

Mikesta



Quote:
> I wasn't to sure what the original post was
> asking either. I guessed it'd be something
> like:

> awk '{col1[NR]=$1;col2[NR]=$2}'

> Tristan


> > I don't understand what you want, but how about something like this:

> > awk '{col1[$1]++; col2[$2]++}
> >      END {for (i in col1){
> >               print "col1[" i "] = " col1[i]}
> >           for (j in col2){
> >               print "col1[" j "] = " col1[j]}} ' infile

Sent via Deja.com http://www.deja.com/
Before you buy.


Mon, 29 Jul 2002 03:00:00 GMT  
 reading a file and using arrays
Mikesta,

Thanks that's much clearer.
Use the following to set up the arrays:

awk '{col1[NR-1]=$1;col2[NR-1]=$2}'

The only difference from the last
suggestion I offered is subtracting
one from NR to start the array indexing
at zero.
NR is a variable containing the number
of the current record, and so (in the
above form) the array index used to
store $1 and $2 will be the number of
the current line minus one.

Hope this helps,

Tristan

Quote:

> I want to use arrays to label each string in the file.
> That is an array which would be of the form
> (for column 1)
> col1[0] = NAME0, col1[1] = NAME1, ..., col1[4999] = NAME4999
> (for column 2)
> col2[0] = 200, col2[1] = 345, ..., col2[499] = 10456

> Mikesta



Tue, 30 Jul 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Reading an array from file and outputting another file

2. end-of-file during read using unformatted reading

3. Using VFS to read zip files inside of tar files

4. Memory read problem when calling dynamic arrays in DLL built using VC++ (LabView 5)

5. convert 2d array to 1d array without using shift registers and build array

6. How to read a binary-file into a bit-array

7. read two sets of data from two spread sheet files to two arrays problem

8. How do I read lines of Text from Text File and add them to Array

9. Write/read arrays of clusters to/from file

10. Read/write an array from file

11. reading file and count and decimate array

12. reading a 1d array of waveform from file?

 

 
Powered by phpBB® Forum Software