Linux utility with reverse index facility?

N

no.top.post

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 <elementValue>" ?

I think it's called 'reverse indexing' ?

== TIA.
 
R

Robert Klemme

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 <elementValue>" ?

I think it's called 'reverse indexing' ?

$ ri Array#index

Cheers

robert
 
C

Chris F.A. Johnson

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 <elementValue>" ?

I think it's called 'reverse indexing' ?

Do you want 'grep -n'?

If not, please be more specific.
 
J

Janis Papanagnou

Am 16.05.2011 12:38, schrieb (e-mail address removed):
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<elementValue>" ?

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]
 
R

Robert Klemme

Why not write your own loop? =A0Use your favourite scripting language. Yo= u
haven't described your problem very well, but my guess it is should be ea= sy
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=
 
S

Stu

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 "{:eek:ne=>1, :two=>2, :three=>3, :four=>4}" | cut -d"," -f 1,4
{:eek:ne=>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
 
N

no.top.post

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]
<elementValue> 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 <memory-structures>>.
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.
 
C

Chris F.A. Johnson

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).
<elementValue> 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
 
R

Robert Klemme

=A0 =A0Do you want 'grep -n'?

=A0 =A0If 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]
<elementValue> means the value of the element.
The first element has value "a".
"the =A0index of the 'element' which is "e" is
<the 5th element>; which index is =A04 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 <memory-structures>>.
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 =3D {'foo' =3D> 'dog', 'bar' =3D> 'cat'}
=3D> {"foo"=3D>"dog", "bar"=3D>"cat"}
irb(main):002:0> h.rassoc 'dog'
=3D> ["foo", "dog"]
irb(main):003:0> a =3D h.to_a
=3D> [["foo", "dog"], ["bar", "cat"]]
irb(main):004:0> a.rassoc 'dog'
=3D> ["foo", "dog"]

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

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Nichols

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<elementValue>" ?

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).
 
B

Brian Candler

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 <elementValue>" ?

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]]
 
L

Luuk

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<elementValue>" ?

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).

echo "abcde c"| awk '{ for (x=0;x<length($1);x++) {
a[substr($1,x,1)]=x;}; print a[$2]; }'
 
E

Ed Morton

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<elementValue>" ?
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).

not really robust though. consider what would happen if the same
string appeared twice in a record.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top