Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Looping Issue? 1

Status
Not open for further replies.

not4google

Programmer
Nov 6, 2006
15
GB
Hi all,

I have some data that Im working on:

,qwerty1,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2005
,,,,,,,,,,,,,,,,,,,2006
,,,,,,,,,,,,,,,,,,,2007
,qwerty2,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2008
,,,,,,,,,,,,,,,,,,,2009
,,,,,,,,,,,,,,,,,,,2010

In my code I want to get to the point where I match the row with qwerty1 init and then keep printing all the values till I get to the qwerty2...So the output should be:

,qwerty1,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2005
,,,,,,,,,,,,,,,,,,,2006
,,,,,,,,,,,,,,,,,,,2007

Any suggestions would be apprecitaed,
 
Code:
use strict;
use warnings;

while (<DATA>) {
   print if (/querty1/ .. /querty2/);
}

__DATA__
,qwerty1,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2005
,,,,,,,,,,,,,,,,,,,2006
,,,,,,,,,,,,,,,,,,,2007
,qwerty2,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2008
,,,,,,,,,,,,,,,,,,,2009
,,,,,,,,,,,,,,,,,,,2010
Untested...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for the quick response,

I dont think Ive explained the issue properly. Once I get to the line matched qwerty1 what I want to do is force Perl to go to the next line untill I get to a line natching qwerty2,

Help is much appreciated,
 
not4google

Have you run the example? What output do you get?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
steve.....


querty1 should be qwerty1









- Kevin, perl coder unexceptional!
 
To avoid printing the end condition, use this slight modification:

Code:
use strict;
use warnings;

while (<DATA>) {
	print if (/qwerty1/ .. /qwerty2/) =~ /^\d+$/;
}

__DATA__
,qwerty0,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2002
,,,,,,,,,,,,,,,,,,,2003
,,,,,,,,,,,,,,,,,,,2004
,qwerty1,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2005
,,,,,,,,,,,,,,,,,,,2006
,,,,,,,,,,,,,,,,,,,2007
,qwerty2,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2008
,,,,,,,,,,,,,,,,,,,2009
,,,,,,,,,,,,,,,,,,,2010

The Output is:
Code:
C:\bin>perl dotdot.pl
,qwerty1,data1,data2,data3
,,,,,,,,,,,,,,,,,,,2005
,,,,,,,,,,,,,,,,,,,2006
,,,,,,,,,,,,,,,,,,,2007

C:\bin>

stevexff: Thanks for teaching me something today. I've never used the .. operator in a scalar context. Have a star.

not4google: To read about the .. operator, go here:
 
Cheers for the star, even if I can't type properly.

When used in a scalar context like this, the .. and ... operators act as a flip-flop. When the first one is met, the condition goes true, and stays that way until the second is met, when it switches off again. the only difference between .. and ... is that one (... I think) allows it to switch on and back off during the same iteration.

It's really handy for situations where you need to switch processing on and off, as you don't need a separate flag variable.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
stevexff said:
the only difference between .. and ... is that one (... I think) allows it to switch on and back off during the same iteration.

Yes, the reference that I quoted gave a much more detailed description of the operator. '...' does allow for iterater to be turned off in the same test, but it will still return true for that one iteration.

As quoted:
Code:
Here's a simple example to illustrate the difference between the two range operators:

    @lines = ("   - Foo",
              "01 - Bar",
              "1  - Baz",
              "   - Quux");

    foreach (@lines) {
        if (/0/ .. /1/) {
            print "$_\n";
        }
    }

This program will print only the line containing "Bar". If the range operator is changed to ... , it will also print the "Baz" line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top