Computing.Net > Forums > Unix > AWK - creating an array of alphabet

AWK - creating an array of alphabet

Reply to Message Icon

Original Message
Name: xaiver
Date: July 25, 2005 at 18:47:11 Pacific
Subject: AWK - creating an array of alphabet
OS: HPUX
CPU/Ram: UNIX
Comment:

HI,

I need to assign the alphabets to an array. Due to my limited knowledge on awk, I do not know the best way to do this.

Currently I am just assigning it to an array individually but eventually it may exceed the list i created manually.
eg
list[1]="A"
list[2]="B"
list[3]="C"
so on..

any better way to do this ?



Report Offensive Message For Removal

Response Number 1
Name: Jim Boothe
Date: July 26, 2005 at 06:58:28 Pacific
Subject: AWK - creating an array of alphabet
Reply: (edit)

To get a list of values loaded into an array indexed numerical as you are doing, you can do:

split("A B C D E F G",list)

or maybe using another delimiter:

split("A:B:C:D:E:F:G",list,":")

They will be indexed from 1, so list[3] would hold the value "C".


But a different approach is to use the values themselves as the index values:

list["A"]=1
list["B"]=1
list["C"]=1
etc

With the first approach, I would have to use a for-loop to scan the array to see if a certain value is loaded.  But with this approach, I can easily check for a value with:

if ("C" in array)


Report Offensive Follow Up For Removal

Response Number 2
Name: vicchai
Date: July 26, 2005 at 17:47:19 Pacific
Subject: AWK - creating an array of alphabet
Reply: (edit)

For ksh:
set -A list a b c d e f ..... z

For bash:
list=(a b c d e f ..... z)

All the above commands are equivalent to this:
list[0]=a
list[1]=b
list[2]=c
.
.
.
list[23]=z


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: AWK - creating an array of alphabet

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software