Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #1116

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

From Tim Watts <tw@dionic.net>
Newsgroups comp.os.linux.misc
Subject Re: Re (2): Linux utility with reverse index facility?
Followup-To comp.os.linux.misc
Date 2011-05-16 22:32 +0100
Organization A noiseless patient Spider
Message-ID <6ph8a8-blt.ln1@squidward.dionic.net> (permalink)
References <iqr44b$oug$1@dont-email.me> <iqru0q$593$1@dont-email.me>

Followups directed to: comp.os.linux.misc

Show all headers | View raw


no.top.post@gmail.com wrote:

> What I want is the KNOWLEDGE, whether *nix designers included
> <reverse indexing> which one would use eg. to find:
> "which token/field is 'mat' in the record:
> "the cat sat on the mat".

Well, if I'm understanding this, then in perl:

#!/usr/bin/perl
$a="the cat sat on the mat"; # Input
@b=split /\s+/, $a;          # space(s) as delimeter, make array of tokens
%c=map {$_ => $i++} @b;     # Make an asociative array (tokenname => index)
print $c{'mat'}'             # try one

index is 0 based, ie 0 is the 1st token

Of course - there is a flaw in that program - hint: what happens if you 
search for the token "the" <- You didn't define what you wanted: a list of 
indices; the first one; the last one?

if the above is too long, you can abbreviate it:

perl -e '$a="the cat sat on the mat";$b={map {$_ => $i++} split /\s+/, $a}-
>{mat};print $b'

(That's one line)


This is why I use perl. Don't ask me about awk, sed, bash - I've found it 
more productive to learn one tool that ha enough functionality to do pretty 
much everything in one place - OK, not so clever on embedded systems where 
perl may not be present, but there you go...

One thing to note: the first part of split is a regex so you can get 
arbitrarily fancy on how you tokenise the string. an Unicode - no problem...

Cheers

Tim

-- 
Tim Watts

Back to comp.os.linux.misc | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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? Ed Morton <mortonspam@gmail.com> - 2011-05-16 07:08 -0500
      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? Tim Watts <tw@dionic.net> - 2011-05-16 22:32 +0100
  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: 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? Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-05-16 15:08 +0200
  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

csiph-web