On Fri, 21 Nov 2008 03:41:21 -0500 "michael spellman" <[EMAIL PROTECTED]> wrote:
> On Thu, Nov 20, 2008 at 8:32 PM, Owen <[EMAIL PROTECTED]> wrote: > > > On Thu, 20 Nov 2008 06:32:51 -0800 (PST) > > marys <[EMAIL PROTECTED]> wrote: > > I am not altogether certain what you are trying to achieve. > > > > Read up on $. (See perldoc perlvar) That gives you the line number > > that you are reading. > > > > Also I think you night be better off using a regex. > > > > if ($line =~ /xxxx/}{print "$. $line\n"}; > > > > This gives you the opportunity to get matches as well as pre and > > post matches > > > > If you want to do awk type things, have a read of perldoc English > Thank you for the advice. > > I want to look in all lines in a many-line document, and if the line > contains a particular string, like maybe 'QQQ', I want to take the > next-to-last string from that line and assign a variable name $x to > it. > > A unix-like command to do the job on one line would be: > my $x = ` awk '/QQQ/{ print $(NF-1) }' ` > > Then if I had lines in the file like > > QQQ 1 2 t horseradish 65 > QQQ 24 65 18 > rr QQQ wowmom 18 > > I would get, after line#1 $x=horseradish > and after line #2 $x=65 > and after line #3 $x=wowmom > > One thing I might be able to do is to pull in one line at a time into > an array with the 'diamond operator' in the llama book and then > somehow split on whitespace at each value of that array, put the > resulting list into another array, and search this second array, > position by position, for 'QQQ'. If any of the positions match, I > could get the second-last word on that line from ($#array -1) somehow. > But there are obvious problems here: for one thing, I need to > surround the values with a quote, for example I need $x='horseradish' > or else Perl will tell me it can't do the job. Plus I am not sure > how to put the results of split into an array. But I am sure that's > do-able. It's a learning experience for sure. > > Thank you very much for the help. You need to run something like this. Adapt to your requirements ============================================================ #!/usr/bin/perl -w use strict; while (<DATA>) { my $line = $_; if ( $line =~ /QQQ/ ) { my @bits = split; print "$bits[$#bits -1]\n"; } } __DATA__ QQQ 1 2 t horseradish 65 QQQ 24 65 18 rr QQQ wowmom 18 ============================================================ horseradish 65 wowmom ============================================================ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/