home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!psinntp!balltown!cabot!rodney
- From: rodney@cabot (Rodney Peck)
- Subject: Re: Feature request for perl V
- Message-ID: <1992Dec18.215039.2394@cabot.balltown.cma.COM>
- Sender: usenet@cabot.balltown.cma.COM
- Nntp-Posting-Host: cabot
- Organization: New York State Institute for Sebastian Cabot Studies
- X-Newsreader: Tin 1.1 PL4
- References: <62887@mimsy.umd.edu>
- Date: Fri, 18 Dec 1992 21:50:39 GMT
- Lines: 47
-
- #!/bin/perl
-
- @lines = (
- "folta@cs.umd.edu (Wayne Folta) writes:",
- ": Could grep have an added option/mode that would allow me to get an array",
- ": of indices instead of an array of items? I want to read a file into an",
- ": array, then do some greps in the array, some calculations, and then if",
- ": everything is right, make a couple of changes in the array, then write it",
- ": back out to the file. If I could do this:",
- ": ",
- ": @line_numbers = grepi (/IN A/, @lines) ;",
- ": ",
- ": I'd be pretty happy. How's it sound?");
-
- # I think the subroutine below does what you want. I can't see any reason
- # to make this an internal function. I imagine the internal version would
- # do roughly the same thing.
- # rodney
- #
- # here's the output of grepi looking for " array" in the above message:
- #
- # cabot> perl f
- # 1, 2, 3, 4
- # : Could grep have an added option/mode that would allow me to get an array
- # : of indices instead of an array of items? I want to read a file into an
- # : array, then do some greps in the array, some calculations, and then if
- # : everything is right, make a couple of changes in the array, then write it
- #
-
-
- @line_numbers = &grepi("/ array/",@lines);
-
- print join(", ", @line_numbers),"\n";
- grep ((print @lines[$_],"\n"), @line_numbers);
-
- sub grepi {
- local ($exp,@ar) = @_;
- local (@ans,$_);
- local ($count) = 0;
-
- foreach $_ (@ar)
- {
- push(@ans,$count) if eval($exp);
- $count++;
- }
- @ans;
- }
-