Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4597 > unrolled thread
| Started by | no.top.post@gmail.com |
|---|---|
| First post | 2011-05-16 10:38 +0000 |
| Last post | 2011-05-18 03:14 -0500 |
| Articles | 15 — 11 participants |
Back to article view | Back to comp.lang.ruby
Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 10:38 +0000
Re: Linux utility with reverse index facility? pk <pk@pk.invalid> - 2011-05-16 11:41 +0100
Re: Linux utility with reverse index facility? Robert Klemme <shortcutter@googlemail.com> - 2011-05-16 05:45 -0500
Re: Linux utility with reverse index facility? "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2011-05-16 07:59 -0400
Re (2): Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 19:30 +0000
Re: Re (2): Linux utility with reverse index facility? "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2011-05-16 15:59 -0400
Re: Re (2): Linux utility with reverse index facility? Robert Klemme <shortcutter@googlemail.com> - 2011-05-17 02:14 -0500
Re: Linux utility with reverse index facility? David Brown <david@westcontrol.removethisbit.com> - 2011-05-16 14:38 +0200
Re: Linux utility with reverse index facility? Robert Klemme <shortcutter@googlemail.com> - 2011-05-16 08:10 -0500
Re: Linux utility with reverse index facility? Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-05-16 15:08 +0200
Re: Linux utility with reverse index facility? Stu <stu@rubyprogrammer.net> - 2011-05-16 12:52 -0500
Re: Linux utility with reverse index facility? Robert Nichols <SEE_SIGNATURE@localhost.localdomain.invalid> - 2011-05-17 22:40 -0500
Re: Linux utility with reverse index facility? Luuk <Luuk@invalid.lan> - 2011-05-18 19:14 +0200
Re: Linux utility with reverse index facility? Ed Morton <mortonspam@gmail.com> - 2011-05-19 16:15 -0700
Re: Linux utility with reverse index facility? Brian Candler <b.candler@pobox.com> - 2011-05-18 03:14 -0500
| From | no.top.post@gmail.com |
|---|---|
| Date | 2011-05-16 10:38 +0000 |
| Subject | Linux utility with reverse index facility? |
| Message-ID | <iqquq7$i46$1@dont-email.me> |
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.
[toc] | [next] | [standalone]
| From | pk <pk@pk.invalid> |
|---|---|
| Date | 2011-05-16 11:41 +0100 |
| Message-ID | <iqqv0v$4uf$1@speranza.aioe.org> |
| In reply to | #4597 |
no.top.post@gmail.com 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?
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-05-16 05:45 -0500 |
| Message-ID | <BANLkTinz97A74AQGEz6oFhTOg0Upk1Yw-Q@mail.gmail.com> |
| In reply to | #4597 |
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' ? $ ri Array#index Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | "Chris F.A. Johnson" <cfajohnson@gmail.com> |
|---|---|
| Date | 2011-05-16 07:59 -0400 |
| Message-ID | <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com> |
| In reply to | #4597 |
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. -- 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)
[toc] | [prev] | [next] | [standalone]
| From | no.top.post@gmail.com |
|---|---|
| Date | 2011-05-16 19:30 +0000 |
| Subject | Re (2): Linux utility with reverse index facility? |
| Message-ID | <iqru16$5cq$1@dont-email.me> |
| In reply to | #4604 |
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. == Chris Glur.
[toc] | [prev] | [next] | [standalone]
| From | "Chris F.A. Johnson" <cfajohnson@gmail.com> |
|---|---|
| Date | 2011-05-16 15:59 -0400 |
| Subject | Re: Re (2): Linux utility with reverse index facility? |
| Message-ID | <a9c8a8-3hh.ln1@206-248-139-163.dsl.teksavvy.com> |
| In reply to | #4626 |
On 2011-05-16, 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]
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)
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-05-17 02:14 -0500 |
| Subject | Re: Re (2): Linux utility with reverse index facility? |
| Message-ID | <BANLkTinHRHPQB2TFfSMSdFxGd-1ecJNW0Q@mail.gmail.com> |
| In reply to | #4626 |
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.
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
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david@westcontrol.removethisbit.com> |
|---|---|
| Date | 2011-05-16 14:38 +0200 |
| Message-ID | <T5mdnUihkr4nh0zQnZ2dnUVZ8k2dnZ2d@lyse.net> |
| In reply to | #4597 |
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. > 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.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-05-16 08:10 -0500 |
| Message-ID | <BANLkTikhvXqCQP1Kg7+51Vp1UmzNKHdSiA@mail.gmail.com> |
| In reply to | #4606 |
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
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou@hotmail.com> |
|---|---|
| Date | 2011-05-16 15:08 +0200 |
| Message-ID | <iqr7k4$r6t$1@speranza.aioe.org> |
| In reply to | #4597 |
Am 16.05.2011 12:38, schrieb no.top.post@gmail.com:
> 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]
[toc] | [prev] | [next] | [standalone]
| From | Stu <stu@rubyprogrammer.net> |
|---|---|
| Date | 2011-05-16 12:52 -0500 |
| Message-ID | <BANLkTinYKMxkJmAP0E5XtFCG+WB+gZ61ow@mail.gmail.com> |
| In reply to | #4597 |
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.
>
>
>
[toc] | [prev] | [next] | [standalone]
| From | Robert Nichols <SEE_SIGNATURE@localhost.localdomain.invalid> |
|---|---|
| Date | 2011-05-17 22:40 -0500 |
| Message-ID | <iqvf42$l3c$1@omega-3a.local> |
| In reply to | #4597 |
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).
--
Bob Nichols AT comcast.net I am "RNichols42"
[toc] | [prev] | [next] | [standalone]
| From | Luuk <Luuk@invalid.lan> |
|---|---|
| Date | 2011-05-18 19:14 +0200 |
| Message-ID | <4dd3fe8e$0$49048$e4fe514c@news.xs4all.nl> |
| In reply to | #4716 |
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).
>
echo "abcde c"| awk '{ for (x=0;x<length($1);x++) {
a[substr($1,x,1)]=x;}; print a[$2]; }'
--
Luuk
[toc] | [prev] | [next] | [standalone]
| From | Ed Morton <mortonspam@gmail.com> |
|---|---|
| Date | 2011-05-19 16:15 -0700 |
| Message-ID | <65f8c519-cf2c-4ce8-abdd-1b35c24abe7d@c41g2000yqm.googlegroups.com> |
| In reply to | #4716 |
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" not really robust though. consider what would happen if the same string appeared twice in a record.
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-05-18 03:14 -0500 |
| Message-ID | <ada9767bab841a044c9285f78e4b7070@ruby-forum.com> |
| In reply to | #4597 |
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/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web