Linux utility with reverse index facility?

awk &stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can "give me the index of the ‘element’
which is " ?

I think it’s called ‘reverse indexing’ ?

== TIA.

[email protected] wrote:

awk &stuff can “give me the the Nth element”,

No it can’t.

but, without writing your own search-loop,
what can "give me the index of the ‘element’
which is " ?

What does that mean?

On Mon, May 16, 2011 at 12:42 PM, [email protected] wrote:

awk &stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can "give me the index of the ‘element’
which is " ?

I think it’s called ‘reverse indexing’ ?

$ ri Array#index

Cheers

robert

On 2011-05-16, [email protected] wrote:

awk &stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can "give me the index of the ‘element’
which is " ?

I think it’s called ‘reverse indexing’ ?

Do you want ‘grep -n’?

If not, please be more specific.

On 16/05/2011 12:38, [email protected] wrote:

awk&stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can “give me the index of the ‘element’
which is” ?

I think it’s called ‘reverse indexing’ ?

== TIA.

Why not write your own loop? Use your favourite scripting language.
You haven’t described your problem very well, but my guess it is should
be easy to do in a perl or python script (read in the file, put the data
through lists, hashes/dicts, sorts, etc., as needed).

I don’t know enough about awk or ruby to say if they can handle it
easily.

Am 16.05.2011 12:38, schrieb [email protected]:

awk&stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can “give me the index of the ‘element’
which is” ?

You are not very clear about your actual data format.

If you have your data one per line and index is the lineno

NR’th element

awk ‘NR == requestedLineNumber’ inputfile

reverse (NR of requested content, match)

awk ‘/requestedContent/ { print NR }’ inputfile

reverse (NR of requested content, equals)

awk ‘$0 == “requestedContent” { print NR }’ inputfile

If you have your data in an array you can also use awk to do
the reverse index lookup. (Waiting for your clarifications
about your actual case.)

Janis

I think it’s called ‘reverse indexing’ ?

== TIA.

[set f’up-to comp.lang.awk]

Without knowing what the format is it is very hard to answer your
question.

Creating your own index is simple as using wc and simulating an array
via carriage returns though a loop. Adding your own counter really is
trivial though.

As it’s been pointed out many commands have a numbered option to
deploy numbering if you don’t want to set up the loop yourself.
Example would be less -N or cat -n as well as grep. (don’t use cat
please =) ) The most focused command for line numbering is nl.

cut works wonders for this type of project. example simulating a
textfile with ruby hash:

% echo “{:one=>1, :two=>2, :three=>3, :four=>4}” | cut -d"," -f 1,4
{:one=>1, :four=>4}

simulating multiline textfile with hash shorthand:

% echo “one:1,two:2,three:2,four:4\nfive:5,six:6,seven:7,eight:8” |
cut -d"," -f 1,4
one:1,four:4
five:5,eight:8

Is this what your looking for?

~Stu

In article [email protected], “Chris
F.A. Johnson” removed_email_address[email protected] wrote:

If not, please be more specific.
awk & grep are not typically used on arrays of single chars,
but that’s the simplest, to ilustrate the concept.
Given the 6char array: [abcdef]
means the value of the element.
The first element has value “a”.
"the index of the ‘element’ which is “e” is
<the 5th element>; which index is 4 or 5 depending
on whether you count from 0 or 1 respectively.

So what did I mean by “writing your own search-loop”?

AFAIK ruby has <assocoation >.
That mean ruby has structures which operate like association
memories; so you can ask "what’s the index of the element
which has value “dog”, in the ‘structure’ of strings.

== Chris Glur.

On Mon, May 16, 2011 at 2:40 PM, David Brown
[email protected] wrote:

On 16/05/2011 12:38, [email protected] wrote:

awk&stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can “give me the index of the ‘element’
which is” ?

I think it’s called ‘reverse indexing’ ?

Why not write your own loop? Use your favourite scripting language. You
haven’t described your problem very well, but my guess it is should be easy
to do in a perl or python script (read in the file, put the data through
lists, hashes/dicts, sorts, etc., as needed).

I don’t know enough about awk or ruby to say if they can handle it easily.

Now that I reread the original posting it seems grep family’s option
-n does the job as well. But I do agree, the problem description is
quite foggy.

Cheers

robert

On 2011-05-16, [email protected] wrote:

Do you want ‘grep -n’?

If not, please be more specific.

awk & grep are not typically used on arrays of single chars,
but that’s the simplest, to ilustrate the concept.
Given the 6char array: [abcdef]

That's not an array; it's a string (this is shell, not C).

means the value of the element.
The first element has value “a”.

Why not use the correct term, ‘character’? Then we would have
understood what you meant.

"the index of the ‘element’ which is “e” is
<the 5th element>; which index is 4 or 5 depending
on whether you count from 0 or 1 respectively.

index() {
case $1 in
“$2”) idx=${1%%$2*}
echo “$(( ${#idx} + 1 ))” ;;
*) echo 0 ;;
esac
}

$ index abcdef c
3

On Mon, May 16, 2011 at 9:40 PM, [email protected] wrote:

Do you want ‘grep -n’?
on whether you count from 0 or 1 respectively.

So what did I mean by “writing your own search-loop”?

AFAIK ruby has <assocoation >.
That mean ruby has structures which operate like association
memories; so you can ask "what’s the index of the element
which has value “dog”, in the ‘structure’ of strings.

irb(main):001:0> h = {‘foo’ => ‘dog’, ‘bar’ => ‘cat’}
=> {“foo”=>“dog”, “bar”=>“cat”}
irb(main):002:0> h.rassoc ‘dog’
=> [“foo”, “dog”]
irb(main):003:0> a = h.to_a
=> [[“foo”, “dog”], [“bar”, “cat”]]
irb(main):004:0> a.rassoc ‘dog’
=> [“foo”, “dog”]

Please also have a look at the documentation (either “ri” or
http://ruby-doc.org/).

Cheers

robert

unknown wrote in post #998974:

awk &stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can "give me the index of the ‘element’
which is " ?

I think it’s called ‘reverse indexing’ ?

== TIA.

a = [“foo”,“bar”,“baz”] # note: indexes are 0,1,2
=> [“foo”, “bar”, “baz”]

a.index(“bar”)
=> 1

a.grep(/ba/)
=> [“bar”, “baz”]

a.each_with_index.select { |line,n| line =~ /ba/ }
=> [[“bar”, 1], [“baz”, 2]]

On 18-05-2011 05:40, Robert Nichols wrote:

I,e., instead of
myArray[n++] = “cheese”
have
myArray[“cheese”] = n++

Now you can use myArray[“something”] in an expression and get back
the number that was stored for that string (or a null string equivalent
to arithmetic 0 if that index was never stored).

echo “abcde c”| awk ‘{ for (x=0;x<length($1);x++) {
a[substr($1,x,1)]=x;}; print a[$2]; }’

On May 17, 10:40pm, Robert Nichols
[email protected] wrote:

are the indices and the values stored in the array are ordinal numbers.
Bob Nichols AT comcast.net I am “RNichols42”
not really robust though. consider what would happen if the same
string appeared twice in a record.

On 05/16/2011 05:38 AM, [email protected] wrote:

awk&stuff can “give me the the Nth element”,
but, without writing your own search-loop,
what can “give me the index of the ‘element’
which is” ?

I think it’s called ‘reverse indexing’ ?

In awk, it’s just as easy to build an array where your “elementValues”
are the indices and the values stored in the array are ordinal numbers.
I,e., instead of
myArray[n++] = “cheese”
have
myArray[“cheese”] = n++

Now you can use myArray[“something”] in an expression and get back
the number that was stored for that string (or a null string equivalent
to arithmetic 0 if that index was never stored).

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs