Linux utility with reverse index facility?

Discussion in 'Ruby' started by no.top.post@gmail.com, May 16, 2011.

  1. Guest

    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.
     
    , May 16, 2011
    #1
    1. Advertising

  2. pk Guest

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


    What does that mean?
     
    pk, May 16, 2011
    #2
    1. Advertising

  3. On Mon, May 16, 2011 at 12:42 PM, <> 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' ?


    $ ri Array#index

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 16, 2011
    #3
  4. On 2011-05-16, 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.

    --
    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)
     
    Chris F.A. Johnson, May 16, 2011
    #4
  5. Am 16.05.2011 12:38, schrieb :
    > 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]
     
    Janis Papanagnou, May 16, 2011
    #5
  6. On Mon, May 16, 2011 at 2:40 PM, David Brown
    <> wrote:
    > On 16/05/2011 12:38, 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? =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=
     
    Robert Klemme, May 16, 2011
    #6
  7. Stu Guest

    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

    On Mon, May 16, 2011 at 5:42 AM, <> 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.
    >
    >
    >
     
    Stu, May 16, 2011
    #7
  8. Guest

    Re (2): Linux utility with reverse index facility?

    In article <>, "Chris F.A. Johnson" <> wrote:

    > On 2011-05-16, 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.
     
    , May 16, 2011
    #8
  9. Re: Re (2): Linux utility with reverse index facility?

    On 2011-05-16, wrote:
    > In article <>, "Chris F.A. Johnson" <> wrote:
    >
    >> On 2011-05-16, 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

    --
    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)
     
    Chris F.A. Johnson, May 16, 2011
    #9
  10. Re: Re (2): Linux utility with reverse index facility?

    On Mon, May 16, 2011 at 9:40 PM, <> wrote:
    > In article <>, "Chris F.A.=

    Johnson" <> wrote:
    >
    >> On 2011-05-16, 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' ?

    >>
    >> =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/
     
    Robert Klemme, May 17, 2011
    #10
  11. On 05/16/2011 05:38 AM, 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"
     
    Robert Nichols, May 18, 2011
    #11
  12. 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/.
     
    Brian Candler, May 18, 2011
    #12
  13. Luuk Guest

    On 18-05-2011 05:40, Robert Nichols wrote:
    > On 05/16/2011 05:38 AM, 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).
    >


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

    --
    Luuk
     
    Luuk, May 18, 2011
    #13
  14. Ed Morton Guest

    On May 17, 10:40 pm, Robert Nichols
    <> wrote:
    > On 05/16/2011 05:38 AM, 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"


    not really robust though. consider what would happen if the same
    string appeared twice in a record.
     
    Ed Morton, May 20, 2011
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rashmi Panduranga

    Facility favored or not?

    Rashmi Panduranga, May 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    500
    Rashmi Panduranga
    May 9, 2005
  2. GMK

    Creating a search Facility

    GMK, Mar 1, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    497
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Mar 1, 2005
  3. dogbite
    Replies:
    4
    Views:
    701
    osmium
    Oct 10, 2003
  4. Replies:
    2
    Views:
    3,789
    Paul Uiterlinden
    May 10, 2007
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    321
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page