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

I think it's called 'reverse indexing' ?

== TIA.

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

What does that mean?

···

no.top.post@gmail.com wrote:

$ ri Array#index

Cheers

robert

···

On Mon, May 16, 2011 at 12:42 PM, <no.top.post@gmail.com> 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 <elementValue>" ?

I think it's called 'reverse indexing' ?

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

Do you want 'grep -n'?

   If not, please be more specific.

···

On 2011-05-16, no.top.post@gmail.com 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 <elementValue>" ?

I think it's called 'reverse indexing' ?

--
   Chris F.A. Johnson, <http://cfajohnson.com>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

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.

···

On 16/05/2011 12:38, no.top.post@gmail.com 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<elementValue>" ?

I think it's called 'reverse indexing' ?

== TIA.

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]

···

Am 16.05.2011 12:38, schrieb no.top.post@gmail.com:

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

···

On Mon, May 16, 2011 at 5:42 AM, <no.top.post@gmail.com> 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 <elementValue>" ?

I think it's called 'reverse indexing' ?

== TIA.

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

···

On 05/16/2011 05:38 AM, no.top.post@gmail.com 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<elementValue>" ?

I think it's called 'reverse indexing' ?

--
Bob Nichols AT comcast.net I am "RNichols42"

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

···

--
Posted via http://www.ruby-forum.com/\.

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 Mon, May 16, 2011 at 2:40 PM, David Brown <david@westcontrol.removethisbit.com> wrote:

On 16/05/2011 12:38, no.top.post@gmail.com 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<elementValue>" ?

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.

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

In article <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com>, "Chris F.A. Johnson" <cfajohnson@gmail.com> 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 <elementValue>" ?
>
> I think it's called 'reverse indexing' ?

   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.

···

On 2011-05-16, no.top.post@gmail.com wrote:

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

···

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

On 05/16/2011 05:38 AM, no.top.post@gmail.com 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<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).

--
Luuk

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

···

On May 17, 10:40 pm, Robert Nichols <SEE_SIGNAT...@localhost.localdomain.invalid> wrote:

On 05/16/2011 05:38 AM, no.top.p...@gmail.com 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<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).

--
Bob Nichols AT comcast.net I am "RNichols42"

In article <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com>, "Chris F.A. Johnson" <cfajohnson@gmail.com> 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 <elementValue>" ?
>
> I think it's called 'reverse indexing' ?

   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

···

On 2011-05-16, no.top.post@gmail.com wrote:

On 2011-05-16, no.top.post@gmail.com wrote:

--
   Chris F.A. Johnson, <http://cfajohnson.com>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

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

···

On Mon, May 16, 2011 at 9:40 PM, <no.top.post@gmail.com> wrote:

In article <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com>, "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:

On 2011-05-16, no.top.post@gmail.com 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 <elementValue>" ?
>
> I think it's called 'reverse indexing' ?

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.

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