Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Load An Array From A CSV File

Status
Not open for further replies.

lambic

Technical User
Nov 28, 2002
68
GB
Hi,

I have a comma separated file, whose contents look something like:

12,X
35,0
79,2
993,0
995,9
996,8
1007,1
1008,0
1010,6

I need to load an array with the contents of the second field, with the array index being the first field (e.g. so I can look up 12 to find X).
I'm not sure how to do this with a CSV file. Can anybody help?

Cheers
 
Hi,

Thanks for that - I'll give a try

Cheers
 
WARNING: The value of a subscript must be in the range 0 through 1023.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ah,

Thats a problem then, my maximum subscript value is 10452!

Hmmm.....
 
Hi

man bash said:
There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.
Code:
[blue]master #[/blue] cat datafile.csv 
10000,more
100000,then
1000000,needed
10000000,I think
[blue]master #[/blue] IFS=","
[blue]master #[/blue] while read -a v; do arr["${v[0]}"]="${v[1]}"; done < datafile.csv
[blue]master #[/blue] echo ${arr[10000000]}
I think

Feherke.
 
Thanks.

I've realised that I've posted in the wrong forum, as I'm using NAWK to develop this script!
I'll repost in the correct forum....

Sorry guys.
 
Tell us more about why you want to do this.
What do you want to use this for?
How do you wish to select values once this array is populated?
Is there user interraction required or are you matching files?
Do you mind what tools/languages are used?


Trojan.
 
Hi

PHV, you usually use/suggest/talk about [tt]ksh[/tt]. But
man ksh said:
Array indices are currently limited to the range 0 through 2047, inclusive.
Am I looking to other version's man, or your last post was not about [tt]ksh[/tt] ?

Feherke.
 
Hi,

Basically I need to write a script which will take a fixed length text file, read each record & split it at a certain point, adding a character looked up from the array (the subscript will be a field in the record), then joining the split record again and outputing to a new file.

Example records (shortened for clarity):

06123456789999+5.0011111 BLOGGS
06122334458888+6.0011111 SMITH

Lookup in array for character associated with field at positions 11 - 14

9999,X
8888,2

Rebuild records to include extra character:

06123456789999X+5.0011111 BLOGGS
061223344588882+6.0011111 SMITH

I can do what is required with NAWK, I just have a problem with the array bit!
 
Hi

Maybe this ?
Code:
#! /usr/bin/awk -f

BEGIN { FS="," }
ARGIND==1 { arr[$1]=$2 }
FNR==1 && ARGIND==2 { FIELDWIDTHS="10 4 100"; $0=$0 }
ARGIND==2 { print $1 $2 arr[$2] $3 }

Usage :
[tt]script.awk datafile.csv longnumberfile.txt > output.txt[/tt]

Feherke.
 
Hi

Sorry, the above is [tt]gawk[/tt]. This should be more [tt]nawk[/tt] friendly :
Code:
#! /usr/bin/awk -f

BEGIN { FS="," }
ARGIND==1 { arr[$1]=$2 }
ARGIND==2 { n=substr($0,11,4); sub(/\+/,arr[n] "+"); print }
Note, that I supposed that [tt]+[/tt] is present in all records.

Feherke.
 
feherke,
be careful - your '/usr/bin/awk' is gawk derivative.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
feherke,
1) my last post was about my POSIX version of ksh.
2) FIELDWIDTHS is gawk, not nawk as asked by lambic.
lambic,
Lookup in array for character associated with field at positions 11 - 14
nawk -F, '
NR==FNR{a[$1]=$2;next}
{ print substr($0,1,14) a[substr($0,11,4)] substr($0,15) }
' /path/to/lookup.csv /path/to/input > output


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
BEGIN { FS = "[,+]" ; OFS = "+" }
ARGV[1] == FILENAME { ary[$1] = $2 ; next }
{ $1 = $1 ary[ substr($1,11,4) ] }
8
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top