数组值在 gawk 中被覆盖
Array values being overwritten in gawk
我正在阅读的文件示例
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
013160,32.8347,-88.1342, 38.1 AL GAINESVILLE LOCK
013511,32.7017,-87.5808, 67.1 AL GREENSBORO
013816,31.8700,-86.2542, 132.0 AL HIGHLAND HOME
015749,34.7442,-87.5997, 164.6 AL MUSCLE SHOALS AP
017157,34.1736,-86.8133, 243.8 AL SAINT BERNARD
017304,34.6736,-86.0536, 187.5 AL SCOTTSBORO
GAWK 代码
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print ,,,
station="" #Forces to be string
#Save latitude
stationInfo[station][lat]=
print "lat",stationInfo[station][lat]
#Save longitude
stationInfo[station][lon]=
print "lon",stationInfo[station][lon]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station][lat]
print "---------------"
}
示例输出
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
lat,31.0581
lon,-87.0547
lat,-87.0547
---------------
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
lat,30.5467
lon,-87.8808
lat,-87.8808
---------------
由于某种原因,stationInfo[station][lat]
中存储的值被经度覆盖。我对世界上正在发生的事情感到茫然。
我在 Fedora 22 上使用 GAWK 4.1.1
你的问题是 lon
和 lat
是变量并且评估为空字符串,所以这个赋值 stationInfo[station][lat]=
和 stationInfo[station][lon]=
正在赋值给 stationInfo[station]["]
.
您需要在那些(和其他)行中引用 lat
和 lon
以使用字符串而不是变量。
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print ,,,
station="" #Forces to be string
#Save latitude
stationInfo[station]["lat"]=
print "lat",stationInfo[station]["lat"]
#Save longitude
stationInfo[station]["lon"]=
print "lon",stationInfo[station]["lon"]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station]["lat"]
print "---------------"
}
我正在阅读的文件示例
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
013160,32.8347,-88.1342, 38.1 AL GAINESVILLE LOCK
013511,32.7017,-87.5808, 67.1 AL GREENSBORO
013816,31.8700,-86.2542, 132.0 AL HIGHLAND HOME
015749,34.7442,-87.5997, 164.6 AL MUSCLE SHOALS AP
017157,34.1736,-86.8133, 243.8 AL SAINT BERNARD
017304,34.6736,-86.0536, 187.5 AL SCOTTSBORO
GAWK 代码
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print ,,,
station="" #Forces to be string
#Save latitude
stationInfo[station][lat]=
print "lat",stationInfo[station][lat]
#Save longitude
stationInfo[station][lon]=
print "lon",stationInfo[station][lon]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station][lat]
print "---------------"
}
示例输出
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
lat,31.0581
lon,-87.0547
lat,-87.0547
---------------
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
lat,30.5467
lon,-87.8808
lat,-87.8808
---------------
由于某种原因,stationInfo[station][lat]
中存储的值被经度覆盖。我对世界上正在发生的事情感到茫然。
我在 Fedora 22 上使用 GAWK 4.1.1
你的问题是 lon
和 lat
是变量并且评估为空字符串,所以这个赋值 stationInfo[station][lat]=
和 stationInfo[station][lon]=
正在赋值给 stationInfo[station]["]
.
您需要在那些(和其他)行中引用 lat
和 lon
以使用字符串而不是变量。
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print ,,,
station="" #Forces to be string
#Save latitude
stationInfo[station]["lat"]=
print "lat",stationInfo[station]["lat"]
#Save longitude
stationInfo[station]["lon"]=
print "lon",stationInfo[station]["lon"]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station]["lat"]
print "---------------"
}