J
John Pritchard-williams
Ok - in 'awk' you can do this: (Where 'array' is empty initially)
array[<string>]++
For instance:
array["abc"]++; => a["abc"]=1
array["abc"]++; => a["abc']=2
...
Which is really useful for tallying up a complete column of varying
values from a text file where you don't know in advance the what may
appear in a column etc...
In Ruby, I have worked out a similar the equivalent to be: (with a Hash
now...)
a={};
a["abc"]=a["abc"].to_i+1;
But I"m sure there is a shorter way ? Better trick available here?
As an aside to 'to_i' is to turn the 'nil' into zero: is this safe to
assume this?
Thanks - sorry if this is stupid question....
John
array[<string>]++
For instance:
array["abc"]++; => a["abc"]=1
array["abc"]++; => a["abc']=2
...
Which is really useful for tallying up a complete column of varying
values from a text file where you don't know in advance the what may
appear in a column etc...
In Ruby, I have worked out a similar the equivalent to be: (with a Hash
now...)
a={};
a["abc"]=a["abc"].to_i+1;
But I"m sure there is a shorter way ? Better trick available here?
As an aside to 'to_i' is to turn the 'nil' into zero: is this safe to
assume this?
Thanks - sorry if this is stupid question....
John