Skip to main content
added 382 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

awk has an in operator. It may be used to access the indexes in an array (arrays are associative arrays/hashes in awk).

If the names of the fruits are keys in the array market then you may use

if (fruit_name in market) { ... }

to check whether the string in fruit_name is a key in market.

For example

BEGIN { FS = "\t" }

NR == FNR { market[$1] = $2; next }

!($1 in market) { printf("No %s in the market\n", $1 ); next }

{ sum += market[$1] }

END { printf("Total sum is %.2f\n", sum ) }

Running this on two files:

$ awk -f script.awk market_prices mylist

where market_prices is a two-column tab delimited file with items and prices, and mylist is a list of items. The script would read the items and their prices from the first file and populate market with these, and then calculate the total cost of the items in the second file, if they existed in the market, reporting the items that can't be found.

The in operator may also be used to loop over the indexes of an array:

for (i in array) {
    print i, array[i]
}

The ordering of the indexes may not be sorted.

awk has an in operator. It may be used to access the indexes in an array (arrays are associative arrays/hashes in awk).

If the names of the fruits are keys in the array market then you may use

if (fruit_name in market) { ... }

to check whether the string in fruit_name is a key in market.

The in operator may also be used to loop over the indexes of an array:

for (i in array) {
    print i, array[i]
}

The ordering of the indexes may not be sorted.

awk has an in operator. It may be used to access the indexes in an array (arrays are associative arrays/hashes in awk).

If the names of the fruits are keys in the array market then you may use

if (fruit_name in market) { ... }

to check whether the string in fruit_name is a key in market.

For example

BEGIN { FS = "\t" }

NR == FNR { market[$1] = $2; next }

!($1 in market) { printf("No %s in the market\n", $1 ); next }

{ sum += market[$1] }

END { printf("Total sum is %.2f\n", sum ) }

Running this on two files:

$ awk -f script.awk market_prices mylist

where market_prices is a two-column tab delimited file with items and prices, and mylist is a list of items. The script would read the items and their prices from the first file and populate market with these, and then calculate the total cost of the items in the second file, if they existed in the market, reporting the items that can't be found.

The in operator may also be used to loop over the indexes of an array:

for (i in array) {
    print i, array[i]
}

The ordering of the indexes may not be sorted.

Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

awk has an in operator. It may be used to access the indexes in an array (arrays are associative arrays/hashes in awk).

If the names of the fruits are keys in the array market then you may use

if (fruit_name in market) { ... }

to check whether the string in fruit_name is a key in market.

The in operator may also be used to loop over the indexes of an array:

for (i in array) {
    print i, array[i]
}

The ordering of the indexes may not be sorted.