Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Recursively counting a matching pattern on a single line.
Given a bookmark:

<DT><A HREF="http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html" ADD_D
ATE="897592292" LAST_VISIT="982769648" LAST_MODIFIED="982769648" ID="rdf:#$r
sy5Z">PERL FAQ</A>


Wondering if it's possible to have 2 counters that would keep track of the
number of < and > encountered.

Im looking to see if there's a balance of < and >, and scream as soon as
there isnt a balance.

So given the above bookmark, I know that there's 3 <  and  3 >
and the pattern is basically <><><> and not <<>> or >><, etc.

Thanks
Birl

Report this thread to moderator Post Follow-up to this message
Old Post
S.A. Birl
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
S.A. Birl wrote:
> Given a bookmark:
>
> <DT><A
> HREF="http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html"
> ADD_DATE="897592292" LAST_VISIT="982769648" LAST_MODIFIED="982769648"
> ID="rdf:#$rsy5Z">PERL FAQ</A>
>
>
> Wondering if it's possible to have 2 counters that would keep track
> of the
> number of < and > encountered.
>
> Im looking to see if there's a balance of < and >, and scream as soon
> as
> there isnt a balance.
>
> So given the above bookmark, I know that there's 3 <  and  3 >
> and the pattern is basically <><><> and not <<>> or >><, etc.

If you're specifically looking whether the brackets follow the pattern
<><><>, could you just do something like:

my $brackets = join '', $bookmark =~ /[<>]/g;
die "Bad pattern" unless $brackets eq '<><><>';

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
Assign each line to the $_ variable and try this to get the number of
instances of < and >

The match operator works on $_ by default. If you match on < or > globally
and assign it to an array like so:

@rights = m/\</g;
@lefts = m/\>/g;

You are left with arrays that contain all the matches. Now all you need to
do is count the number of elements in each array like so:

$number_of_rights = $#rights;
$number_of_lefts = $#lefts;

Then compare them:

if ($number_of_rights != $number_of_lefts)
{
## scream here!!
}

-----Original Message-----
From: S.A. Birl [mailto:sbirl+PERL@concept.temple.edu]
Sent: Wednesday, October 27, 2004 3:23 PM
To: beginners@perl.org
Subject: Recursively counting a matching pattern on a single line.


Given a bookmark:

<DT><A HREF="http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html"
ADD_DATE="897592292" LAST_VISIT="982769648" LAST_MODIFIED="982769648"
ID="rdf:#$rsy5Z">PERL FAQ</A>


Wondering if it's possible to have 2 counters that would keep track of the
number of < and > encountered.

Im looking to see if there's a balance of < and >, and scream as soon as
there isnt a balance.

So given the above bookmark, I know that there's 3 <  and  3 >
and the pattern is basically <><><> and not <<>> or >><, etc.

Thanks
Birl

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
On Oct 27, brian.barto@spectrum-health.org (nospam-brian.barto@spectrum-hea.
.:

Brian:  Assign each line to the $_ variable and try this to get the number o
f
Brian:  instances of < and >
Brian:
Brian:  The match operator works on $_ by default. If you match on < or > gl
obally
Brian:  and assign it to an array like so:
Brian:
Brian:  @rights = m/\</g;
Brian:  @lefts = m/\>/g;
Brian:
Brian:  You are left with arrays that contain all the matches. Now all you n
eed to
Brian:  do is count the number of elements in each array like so:
Brian:
Brian:  $number_of_rights = $#rights;
Brian:  $number_of_lefts = $#lefts;
Brian:
Brian:  Then compare them:
Brian:
Brian:  if ($number_of_rights != $number_of_lefts)
Brian:  {
Brian:  	## scream here!!
Brian:  }



Great!  That works.  But I was looking to get a little trickier with it.


If I had:
<DT <A HREF="http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html" ADD_D
ATE="897592292" LAST_VISIT="982769648" LAST_MODIFIED="982769648" ID="rdf:#$r
sy5Z">PERL FAQ</A>

I wanted it to scream when it reached the 2nd <  ; stopping the pattern
matching from continuing any further.  Is that possible, or would
something more complex be needed?

If it's too complex, I'll just with what I have for now.


Thanks
Birl

Please do not CC me responses to my own posts.
I'll read the responses on the list.

Report this thread to moderator Post Follow-up to this message
Old Post
S.A. Birl
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
Great!  That works.  But I was looking to get a little trickier with it.


----------------

If you want to make sure they are alternating like <><><> etc... I would do
this:

$_ = $line;

@syms = m/[<>]/g;
$string = join("", @syms);
if ($strings !~ m/^<(>< )*>$/)
{
## Scream here!
}

The regular expression:

m/^<(>< )*>$/

will ensure that it starts with < and ends with > and anything in between
will be "><" which I think should do the trick. That logic is pretty hairy
though and I could be missing something.

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
Correction: "$strings" in the if statement should be "$string"

-----Original Message-----
From: brian.barto@spectrum-health.org
[mailto:brian.barto@spectrum-health.org]
Sent: Wednesday, October 27, 2004 4:04 PM
To: beginners@perl.org
Subject: RE: Recursively counting a matching pattern on a single line.


Great!  That works.  But I was looking to get a little trickier with it.


----------------

If you want to make sure they are alternating like <><><> etc... I would do
this:

$_ = $line;

@syms = m/[<>]/g;
$string = join("", @syms);
if ($strings !~ m/^<(>< )*>$/)
{
## Scream here!
}

The regular expression:

m/^<(>< )*>$/

will ensure that it starts with < and ends with > and anything in between
will be "><" which I think should do the trick. That logic is pretty hairy
though and I could be missing something.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
On Oct 27, brian.barto@spectrum-health.org (nospam-brian.barto@spectrum-hea.
.:

Brian:
Brian:  If you want to make sure they are alternating like <><><> etc... I w
ould do
Brian:  this:
Brian:
Brian:  $_ = $line;
Brian:
Brian:  @syms = m/[<>]/g;
Brian:  $string = join("", @syms);
Brian:  if ($strings !~ m/^<(>< )*>$/)
Brian:  {
Brian:  	## Scream here!
Brian:  }
Brian:
Brian:  The regular expression:
Brian:
Brian:  m/^<(>< )*>$/
Brian:
Brian:  will ensure that it starts with < and ends with > and anything in be
tween
Brian:  will be "><" which I think should do the trick. That logic is pretty
 hairy
Brian:  though and I could be missing something.



Wouldnt m/[<>]/g literally match <> and not <characters>?

Why wouldnt it be m/[<.+>]/g ?



Thanks
Birl

Please do not CC me responses to my own posts.
I'll read the responses on the list.

Report this thread to moderator Post Follow-up to this message
Old Post
S.A. Birl
10-28-04 01:55 AM


RE: Recursively counting a matching pattern on a single line.
Wouldnt m/[<>]/g literally match <> and not <characters>?

Why wouldnt it be m/[<.+>]/g ?



Thanks
Birl

-----------

Someone correct me if I'm wrong but putting characters inside brackets []
defines a character class. Or a group of characters you want to match on,
not necessarily in that order.

For instance m/[0-9]/g matches on any single digit number, not "0123456789"

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
10-28-04 01:55 AM


Re: Recursively counting a matching pattern on a single line.
S.A. Birl wrote:
> Wondering if it's possible to have 2 counters that would keep track
> of the number of < and > encountered.
>
> Im looking to see if there's a balance of < and >, and scream as soon
> as there isnt a balance.
>
> So given the above bookmark, I know that there's 3 <  and  3 > and
> the pattern is basically <><><> and not <<>> or >><, etc.

Assuming that you are actually not interested in *counting* them, this
is one approach:

print "Oops!\n" unless $string =~ /^[^<>]*(?:(?:<[^<>]*> )*[^<>]*)*$/;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
10-28-04 01:55 AM


Re: Recursively counting a matching pattern on a single line.
brian.barto@spectrum-health.org wrote:
> Assign each line to the $_ variable and try this to get the number of
> instances of < and >
>
> The match operator works on $_ by default. If you match on < or > globally
> and assign it to an array like so:
>
> @rights = m/\</g;
> @lefts = m/\>/g;

See the FAQ for a more efficient way to do this.

perldoc -q "How can I count the number of occurrences of a substring within 
a
string"


> You are left with arrays that contain all the matches. Now all you need to
> do is count the number of elements in each array like so:
>
> $number_of_rights = $#rights;
> $number_of_lefts = $#lefts;

An array in scalar context returns the number of elements in that array.  Yo
u
are using the index of the last element of the array which *may* (or may not
!)
be one less than the number of elements.


John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-28-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:49 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.