Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > check columns /tabs between two patterns

Reply
Thread Tools

check columns /tabs between two patterns

 
 
Marek
Guest
Posts: n/a
 
      09-14-2008


Hello all!


I have a tab separated text file. I want to check, whether the
contents are in the right fields. I have constructed here a little
example. Question is, how to check for right number of tabs between
"pattern" and "number" ... In my example, there is checked only one
wrong example. To be clear I take this example:

(pattern3)\t{4,}(number3)

this is checking 4 or more tabs between pattern3 and number3, which is
wrong. Only 3 tabs are right! So I have to check also for 2 tabs or 1
tab, which would be wrong too ...


Hope this was clear


best greetings marek


#! /usr/local/bin/perl

use warnings;
use strict;

while (<DATA>) {

s/\s+#.+//;
next if /^\s*$/;

if (/(pattern1)\t{2,}(number1)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$_\n\n";

}
elsif (/(pattern2)\t{3,}(number2)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$_\n\n";

}
elsif (/(pattern3)\t{4,}(number3)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$_\n\n";

}
else {

print "\nno match!\n\n";

}
}

__DATA__

pattern1 number1
pattern2 number2
pattern3 number3
pattern1 number1 # wrong number of tabs (2tabs)
pattern2 number2 # wrong number of tabs (3tabs)
pattern3 number3 # wrong number of tabs (4tabs)
pattern2 number2 # wrong number of tabs (1tab)
pattern3 number3 # wrong number of tabs (2tabs)




 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      09-14-2008
Marek schreef:

> I have a tab separated text file. I want to check, whether the
> contents are in the right fields.


Maybe you can use this approach:

Read a line, split on /\t/ and store in an array, then do tests.


while ( <> ) {
/^\s*(?:#|$)/ and next; # comment- and blank lines

my @data = split /\t/;
... # tests
}


> "Wrong number of tabs between \"$1\" and the number \"$2\" in the
> line: \n\t$_\n\n";
> [...]
> "Wrong number of tabs between \"$1\" and the number \"$2\" in the
> line: \n\t$_\n\n";
> [...]
> "Wrong number of tabs between \"$1\" and the number \"$2\" in the
> line: \n\t$_\n\n";


See also `perldoc -f sprintf`, you want to create a single format string
for that.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      09-14-2008
Marek <(E-Mail Removed)> wrote:

> Question is, how to check for right number of tabs between
> "pattern" and "number" ... In my example, there is checked only one
> wrong example. To be clear I take this example:
>
> (pattern3)\t{4,}(number3)
>
> this is checking 4 or more tabs between pattern3 and number3, which is
> wrong. Only 3 tabs are right! So I have to check also for 2 tabs or 1
> tab, which would be wrong too ...



> elsif (/(pattern3)\t{4,}(number3)\s*/i) {



elsif ( ! /(pattern3)\t{3}(number3)\s*/i ) {


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      09-14-2008
On Sun, 14 Sep 2008 02:20:45 -0700, Marek wrote:

> Hello all!
>
>
> I have a tab separated text file. I want to check, whether the contents
> are in the right fields. I have constructed here a little example.
> Question is, how to check for right number of tabs between "pattern" and
> "number" ... In my example, there is checked only one wrong example. To
> be clear I take this example:
>
> (pattern3)\t{4,}(number3)
>
> this is checking 4 or more tabs between pattern3 and number3, which is
> wrong. Only 3 tabs are right! So I have to check also for 2 tabs or 1
> tab, which would be wrong too ...


Something like (untested) /(pattern3)(?:\t{,2}|\t{4,}(number3?)/ maybe?

But I would do something like:

elsif (/(pattern2)(\t+)(number2)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$_\n\n"
if length($2) != 2;

}

And even that can be optimized further, but this should get you going.

HTH,
M4
 
Reply With Quote
 
Marek
Guest
Posts: n/a
 
      09-15-2008

Thank you all for your answers! I will stick to

elsif (/(pattern2)(\t+)(number2)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the
line:
\n\t$_\n\n"
if length($2) != 2;

}

Martijns suggestion! Instead of searching all positive possibilities
of wrong numbers of \tabs, it is easier to say if not the right number
of tabs ...

Did not know this possibility to check with "length". Fantastic


Thank you all again


marek
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      09-15-2008
On Sun, 14 Sep 2008 22:28:10 -0700, Marek wrote:

> Thank you all for your answers! I will stick to
>
> elsif (/(pattern2)(\t+)(number2)\s*/i) {
> print
> "Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
> \n\t$_\n\n"
> if length($2) != 2;
>
> }
>
> Martijns suggestion! Instead of searching all positive possibilities of
> wrong numbers of \tabs, it is easier to say if not the right number of
> tabs ...
>
> Did not know this possibility to check with "length". Fantastic


I like Tads suggestion better. I'm a bit ashamed I didn't think of that
myself.

M4
 
Reply With Quote
 
Marek
Guest
Posts: n/a
 
      09-15-2008


But Tad's suggestion I don't understand!

If something is *not* matching

elsif ( ! /(pattern3)\t{3}(number3)\s*/i ) { ... }

it will always match. Consider first line of __DATA__

pattern1 number1

this would match already, because it *does not* match, because of > !
<

But I am a simple beginner; probably I did not understand something
here!



greetings marek
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      09-15-2008
Marek <(E-Mail Removed)> wrote:
>
>
> But Tad's suggestion I don't understand!
>
> If something is *not* matching
>
> elsif ( ! /(pattern3)\t{3}(number3)\s*/i ) { ... }



You said "Only 3 tabs are right", so to test if the data is "right":

elsif ( /(pattern3)\t{3}(number3)\s*/i ) { ... } # right

but the body of the elsif deals with when it is NOT right, so you need:

elsif ( ! /(pattern3)\t{3}(number3)\s*/i ) { ... } # not right


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Marek
Guest
Posts: n/a
 
      09-16-2008
Sorry Tad,

but I understood you like follows:

#! /usr/local/bin/perl

use warnings;
use strict;

while (<DATA>) {

s/\s+#.+//;
next if /^\s*$/;

if ( ! /(pattern1)\t(number1)\s*/i) {
print
"[First if:] Wrong number of tabs between \"$1\" and the number \"$2\"
in the line:\n\t$_\n\n";

}
elsif ( ! /(pattern2)\t{2}(number2)\s*/i) {
print
"[First elsif:] Wrong number of tabs between \"$1\" and the number
\"$2\" in the line:\n\t$_\n\n";

}
elsif ( ! /(pattern3)\t{3}(number3)\s*/i) {
print
"[First elsif:] Wrong number of tabs between \"$1\" and the number
\"$2\" in the line:\n\t$_\n\n";

}
else {

print "\nno match!\n\n";

}
}

__DATA__

pattern1 number1
pattern2 number2
pattern3 number3
pattern1 number1 # wrong number of tabs (2tabs)
pattern2 number2 # wrong number of tabs (3tabs)
pattern3 number3 # wrong number of tabs (4tabs)
pattern2 number2 # wrong number of tabs (1tab)
pattern3 number3 # wrong number of tabs (2tabs)

And this is not working. Certainly a misunderstanding? And here the
version, how I understood Petr and Martjin ...

#! /usr/local/bin/perl

use warnings;
use strict;

while (<DATA>) {

s/\s+#.+//;
next if /^\s*$/;

if (/(pattern1)(\t+)(number1)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$.: $_\n\n" if length($2) !=1 ;

}
elsif (/(pattern2)(\t+)(number2)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$.: $_\n\n" if length($2) !=2 ;

}
elsif (/(pattern3)(\t+)(number3)\s*/i) {
print
"Wrong number of tabs between \"$1\" and the number \"$2\" in the line:
\n\t$.: $_\n\n" if length($2) !=3 ;

}
else {

print "\nno match!\n\n";

}
}

__DATA__

pattern1 number1
pattern2 number2
pattern3 number3
pattern1 number1 # wrong number of tabs (2tabs)
pattern2 number2 # wrong number of tabs (3tabs)
pattern3 number3 # wrong number of tabs (4tabs)
pattern2 number2 # wrong number of tabs (1tab)
pattern3 number3 # wrong number of tabs (2tabs)


 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      09-18-2008
On Mon, 15 Sep 2008 11:46:13 -0700, Marek wrote:

> But Tad's suggestion I don't understand!
>
> If something is *not* matching
>
> elsif ( ! /(pattern3)\t{3}(number3)\s*/i ) { ... }
>
> it will always match. Consider first line of __DATA__
>
> pattern1 number1
>
> this would match already, because it *does not* match, because of > ! <
>
> But I am a simple beginner; probably I did not understand something
> here!


No, you're quite right and I was quite wrong, as well as Tad. Sorry,
brain misfiring I guess.

M4
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 6.3 How can I pull out lines between two patterns that are themselves on different lines? PerlFAQ Server Perl Misc 0 04-19-2011 10:00 AM
FAQ 6.3 How can I pull out lines between two patterns that are themselves on different lines? PerlFAQ Server Perl Misc 0 01-14-2011 05:00 AM
How do I find overlaps between two Ruby Runt patterns? Amit Rawal Ruby 0 06-23-2009 07:50 AM
Two questions: datagrid with string[] and how to differentiate between columns Bob Weiner ASP .Net Datagrid Control 1 05-06-2005 01:09 PM
where to find good patterns and sources of patterns (was Re: singletons) crichmon C++ 4 07-07-2004 10:02 PM



Advertisments