convert string to <> ( multipul matches)

D

dangelsocket

This is a question i'm sure has been asked before but i can't see to dig a
a answer.

say your recieving a text stream:

contained in $stream. ( only available as a string )

say you need to find numerous occurances of a string in $stream.

like:

for ( $stream ) {

(/match_this/) ? print 'match this'; : my $nada;
}

# because $stream is being recieved as a 'plain string', this script
will only match the first occurance of /match_this/ (even if it occurs many
times,
of course the 'g' and 'm' modifiers are irrevelent in this situtation.

What i ended up doing was something like @list = split(/\w+/, $stream);
which works
but it seems that there is another way to do this (of course)
( almost like a backwards chomp like - add newline, or define $/ somewhere
)
 
D

danglesocket

came up with this as well, other answers are always cool but this works well
for what i needed:

while ( $stream =~ /(input.*)/ig) {
if ( defined $1 ) { print "input: $1\n";}
}




__danglesocket__

This is a question i'm sure has been asked before but i can't see to dig a
a answer.

say your recieving a text stream:

contained in $stream. ( only available as a string )

say you need to find numerous occurances of a string in $stream.

like:

for ( $stream ) {

(/match_this/) ? print 'match this'; : my $nada;
}

# because $stream is being recieved as a 'plain string', this script
will only match the first occurance of /match_this/ (even if it occurs many
times,
of course the 'g' and 'm' modifiers are irrevelent in this situtation.

What i ended up doing was something like @list = split(/\w+/, $stream);
which works
but it seems that there is another way to do this (of course)
( almost like a backwards chomp like - add newline, or define $/ somewhere
)
 
B

bd

This is a question i'm sure has been asked before but i can't see to dig a
a answer.

say your recieving a text stream:

contained in $stream. ( only available as a string )

say you need to find numerous occurances of a string in $stream.

What do you mean, 'find'? Do you want to count them? Or store the line
they're on?
like:

for ( $stream ) {

(/match_this/) ? print 'match this'; : my $nada;
}

# because $stream is being recieved as a 'plain string', this script
will only match the first occurance of /match_this/ (even if it occurs many
times,
of course the 'g' and 'm' modifiers are irrevelent in this situtation.

I don't understand. Why not:
my @matches;

for ($stream) {
@matches = /match_this/;
}
 
S

Steve Grazzini

dangelsocket said:
say you need to find numerous occurances of a string in $stream.

like:

for ( $stream ) {

(/match_this/) ? print 'match this'; : my $nada;
}

# because $stream is being recieved as a 'plain string', this
script will only match the first occurance of /match_this/ (even
if it occurs many times, of course the 'g' and 'm' modifiers are
irrevelent in this situtation.

The /g modifier might be more relevant than you think:

while ($stream =~ /pattern/g) {
# don't abuse the conditional operator
}

Maybe the /m and /s modifiers aren't needed, but it's really not
clear from your description.

If you do need to access $stream via a filehandle, you can do this
in 5.8.0:

open my $str, '<', \$stream;
while (<$str>) {
...
}

And there was a CPAN module (IO::Stringy, I think) that let you do
the same thing with earlier versions of Perl.
 
D

danglesocket

Steve Grazzini said:
say you need to find numerous occurances of a string in $stream.


If you do need to access $stream via a filehandle, you can do this
in 5.8.0:

open my $str, '<', \$stream;
while (<$str>) {
...
}
-Steve

- thanks, that's what i was looking for.




__danglesocket__

# because $stream is being recieved as a 'plain string', this script
will only match the first occurance of /match_this/ (even if it occurs many
times,
of course the 'g' and 'm' modifiers are irrevelent in this situtation.

I don't understand. Why not:
my @matches;

for ($stream) {
@matches = /match_this/;
}

-thanks, i didn't think of this.

danglesocket said:
came up with this as well, other answers are always cool but this works well
for what i needed:

while ( $stream =~ /(input.*)/ig) {
if ( defined $1 ) { print "input: $1\n";}
^^^^^^^^^^^^^^^^^

That test is worthless.

$1 must be defined (ie. the match must succeed) for control
to reach this point in the first place.


-actually, in the context i was originally using it, it made sense.
this is a 'bad' re-creation. (yes i normally use HTML::parser, or the
TokeParser to parse html)

#!/usr/bin/perl -w

use strict;
use warnings;
my $page;

# re-create string, yes unecessary
for ( <DATA> ) {
$page .= $_;
}

while ( $page =~ /(form.*)(table)?/ig) {
#if ( defined $1 ) { print "input: $1\n";}
print "input: $1\n";
#will produce an error unless wrapped in 'if defined' clause
print "table: $2\n";
}

__DATA__
<HTML>
<HEAD><TITLE>Ubasldfksdf</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<CENTER>
<H2>
<FONT SIZE=+3>P</FONT>ERSONAL
<FONT SIZE=+3>I</FONT>NFORMATION
</H2>
</CENTER>
<P>
<P><I>Fields marked with an asterisk (*) must be entered.</I>
<!-- This is a demo, so no ACTION or METHOD for the form. -->
<FORM NAME="PersonalInfo">
<TABLE>
<TR>
<TD>* Last Name:</TD>
<TD><INPUT TYPE="text" NAME="LastName"
onFocus="promptEntry(sUSLastName)"
onChange="checkString(this,sUSLastName)"></TD>
</TR>
<TR>
<TD>* First Name:</TD>
<TD><INPUT TYPE="text" NAME="FirstName"
onFocus="promptEntry(sUSFirstName)"
onChange="checkString(this,sUSFirstName)"></TD>
</TR>

<TR>
<TD>Title:</TD>
<TD><INPUT TYPE="text" NAME="Title" onFocus="promptEntry(sTitle)"></TD>
</TR>

<TR>
<TD>Company Name:</TD>
<TD><INPUT T

-thanks for the responses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,122
Messages
2,570,716
Members
47,282
Latest member
hopkins1988

Latest Threads

Top