Help - Search - Member List - Calendar
Full Version: combinations
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
Support our Sponsors!
Sumit Kaur
Hi,

I have to write my first Perl script . This scripts Searches for rare codons in nucleotide sequence . The nucleotide sequence is entered by the user in the format "ATTGCAA.." and then the program breaks this sequence in the groups of three alphabets like ATT,GCA...so no.=20

Please suggest .

Thanks
-----Original Message-----
From: Charles K. Clarkson [mailto:[Email Removed]]
Sent: Thursday, March 04, 2004 4:44 PM
To: [Email Removed]
Subject: RE: Checking filenames? [:: ?Kinda Solved? ::]


WC -Sx- Jones [mailto:[Email Removed]]
:
: Hmmm, I get 3 Indians in only the first variable anyways =/
:
:
: #! /usr/bin/perl -w
:
: use strict;
:
: # Make -w / use strict; happy...
: my ( $onelittle,
: $twolittle,
: $threelittle,
: ) = 'Indians' x 3;


Ahh! Grasshopper ... :)

Make 'Indians' an array.

my( $onelittle,
$twolittle,
$threelittle, ) = ('Indians') x 3;


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist




--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Wc -Sx- Jones
Sumit Kaur wrote:
QUOTE
Hi,

I have to write my first Perl script . This scripts Searches for rare =
codons in nucleotide sequence . The nucleotide sequence is entered by =
the user in the format "ATTGCAA.." and then the program breaks this =
sequence in the groups of three alphabets like ATT,GCA...so no.=20

Please suggest .


Please dont piggy back post - it breaks the thread of the original
conversation.

If the data will always be "three" - maybe this will give you an idea:

@array = split(//, 'ABCDEFGHIJKLMONPQRSTUVWXYZ');

my $index = -1;

while($#array > $index) {
print "nSet $index ";
print $array[++$index];
print $array[++$index];
print $array[++$index];
}


and so on...
-Bill-
__Sx__________________________________________
http://youve-reached-the.endoftheinternet.org/

Wolf Blaum
On Friday 05 March 2004 02:03, Sumit Kaur generously enriched virtual reallity
by making up this one:

QUOTE
Hi,

Hi,

QUOTE
I have to write my first Perl script . This scripts Searches for rare =
codons in nucleotide sequence . The nucleotide sequence is entered by =
the user in the format "ATTGCAA.." and then the program breaks this =
sequence in the groups of three alphabets like ATT,GCA...so no.=20

Please suggest .

I first of all suggest you use new subject line...
Then: read Learning perl by Randal Schwartz and Tom Phoenix and tell us if you
are learning perl as a first language or allready know about control
structures, filehandles and regluar expressions....

Then: what do you mean by "rare" - do you want the program to figure out, what
the least frequent codons are and list/count/whatever them or do you have a
prior definition of rare. Further (given your example above):

ATTGCAA
ATT
TTG
TGC

are all these codons or do you assume your user supplied sequence starts
somewhere you know and goes on in triplets from there on? (That is, does the
user supply only exons starting with the start tripplet?)

Once you get past the first steps: look at the bioperl modules at cpan and at
www.Bioperl.org

Enjoy -Wolf

Wolf Blaum
On Friday 05 March 2004 02:03, Sumit Kaur generously enriched virtual reallity
by making up this one:

QUOTE
Hi,

Hi,

QUOTE
I have to write my first Perl script . This scripts Searches for rare > codons in nucleotide sequence . The nucleotide sequence is entered by > the user in the format "ATTGCAA.." and then the program breaks this > sequence in the groups of three alphabets like ATT,GCA...so no.=20

Please suggest .

I first of all suggest you use new subject line...
Then: read Learning perl by Randal Schwartz and Tom Phoenix and tell us if you
are learning perl as a first language or allready know about control
structures, filehandles and regluar expressions....

Then: what do you mean by "rare" - do you want the program to figure out, what
the least frequent codons are and list/count/whatever them or do you have a
prior definition of rare. Further (given your example above):

ATTGCAA
ATT
TTG
TGC

are all these codons or do you assume your user supplied sequence starts
somewhere you know and goes on in triplets from there on? (That is, does the
user supply only exons starting with the start tripplet?)

Once you get past the first steps: look at the bioperl modules at cpan and at
www.Bioperl.org

Enjoy -Wolf

R. Joseph Newton
"Wagner, David --- Senior Programmer Analyst --- WGO" wrote:


QUOTE
I am just trying to give my user what they asked for and they do not want any attachments. I was using what Jan K suggested, but obviously missing more than just a little bit.

Wags ;)

Sound like you're stuck with it. You might point out to your clients that RTF cannot hold viri, since, AFAIK, it doesn't contain macros. OTOH, using Outlook is sorta like holding your
door wide open and inviting them in.

In that case, I would default to the suggestion about Content-type. Here is how an attachment I sent from my work to personal email appeared:

Content-Type:
application/rtf; name="Resume_rjn.rtf"
Content-Transfer-Encoding:
base64
Content-Disposition:
attachment; filename="Resume_rjn.rtf"
Change the above to:
inline
Content-Description:
Rich-Text-Format

Joseph

Rob Hanson
QUOTE
Any idea why print never gets executed?

The su is probably waiting for you to enter in a password.

....And this probably doesn't do what you think anyway. Even though you su,
it doesn't change who the current script is running under. Your program (as
written) will spawn a new process, execute su, then close that process.

Hope that helps.

Rob

-----Original Message-----
From: Silky Manwani [mailto:[Email Removed]]
Sent: Friday, March 05, 2004 1:16 PM
To: [Email Removed]
Subject: Nothing executes after system.


Hello,

@args = ("/usr/bin/su","sam");
system(@args);
print "hello";

Any idea why print never gets executed? So, the switch user takes place
but nothing gets executed after that..

Thanks.


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Rob Hanson
QUOTE
Is there another way os saying "su" using perl?

You might be able to do it with the POSIX module, and the setuid() call.
But I can't say that I have tried it, so I am only guessing.

See:
man setuid
http://search.cpan.org/~lbrocard/perl5.005...POSIX/POSIX.pod

Rob

-----Original Message-----
From: Silky Manwani [mailto:[Email Removed]]
Sent: Friday, March 05, 2004 1:30 PM
Cc: [Email Removed]
Subject: Re: Nothing executes after system.


It doesn't wait for the password. I can see that it switches the user
coz the prompt changes. But as you said , yes, since another process
gets spawned and su gets executed.

Is there another way os saying "su" using perl?

Thanks.

On Mar 5, 2004, at 10:26 AM, Hanson, Rob wrote:

QUOTE
Any idea why print never gets executed?

The su is probably waiting for you to enter in a password.

...And this probably doesn't do what you think anyway.  Even though
you su,
it doesn't change who the current script is running under.  Your
program (as
written) will spawn a new process, execute su, then close that process.

Hope that helps.

Rob

-----Original Message-----
From: Silky Manwani [mailto:[Email Removed]]
Sent: Friday, March 05, 2004 1:16 PM
To: [Email Removed]
Subject: Nothing executes after system.


Hello,

@args = ("/usr/bin/su","sam");
system(@args);
print "hello";

Any idea why print never gets executed? So, the switch user takes place
but nothing gets executed after that..

Thanks.


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Bob Showalter
Paul Kraus wrote:
QUOTE
I hate to ask for code but I am kind of in a crunch to finish a
project.

See below.

QUOTE

I have the network programming with perl but it's just too much to
read by tomorrow night :)

Buy the Perl Cookbook. It has good examples for this kind of thing.

QUOTE

I need a daemon listening on a port. I need a client side script that
just takes piped input in and then dumps it out to a file on the
other server.

I need more then that but the rest I can handle in the allocated time.

Example
Files sendtextfile (arguments) server port

cat mytextfile.txt | sendtextfile ... 12000

With this, the server won't know what to name the file, so I've just used an
incrementing number. You could extend the protocol to pass the filename.

(why not use FTP or similar?)

QUOTE

Then on the server this file would just be dumped to stdout or disk.
I will be doing a lot with it actually but this framework will get me
where I need to be to finish up this project.

The server could be getting hit with more then one file at a time.

I'm forking a process to handle each client.

QUOTE

If you do end up helping me out I appreciate, deeply!

I am guessing this is easy if it's really involved then I apologize
now for even asking :)

OK, you owe me a beer :~) Note that this has zilch security or anything to
prevent DoS type attacks, so caveat emptor...

server.pl
---------
#!/usr/bin/perl

use strict;
use IO::Socket::INET;

my $n = 0;

my $sock = IO::Socket::INET->new(
LocalPort => 12000,
Proto => 'tcp',
Listen => 5)
or die "Couldn't create listen socket: $!";

warn "Waiting for connectionsn";
while (my $client = $sock->accept) {
$n++;
defined(my $pid = fork) or die "Couldn't fork: $!";
next if $pid;

my $host = $client->peerhost;
my $file = "file.$n";
warn "Received connection from $host, writing to $filen";
open F, ">$file";
print F while <$client>;
close F;
warn "Connection from $host closedn";
exit;
}

client.pl
---------
#!/usr/bin/perl

use strict;
use IO::Socket::INET;

my $n = 0;

@ARGV or die "usage: $0 host [port]n";
my $host = shift;
my $port = shift || 12000;

my $sock = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp')
or die "Couldn't connect to $host:$port: $!";

print $sock $_ while <>;

Bob Showalter
Bob Showalter wrote:
QUOTE
...
warn "Received connection from $host, writing to $filen";
open F, ">$file";

Stick some error checking in there :~)

Silky Manwani
It doesn't wait for the password. I can see that it switches the user
coz the prompt changes. But as you said , yes, since another process
gets spawned and su gets executed.

Is there another way os saying "su" using perl?

Thanks.

On Mar 5, 2004, at 10:26 AM, Hanson, Rob wrote:

QUOTE
Any idea why print never gets executed?

The su is probably waiting for you to enter in a password.

...And this probably doesn't do what you think anyway.  Even though
you su,
it doesn't change who the current script is running under.  Your
program (as
written) will spawn a new process, execute su, then close that process.

Hope that helps.

Rob

-----Original Message-----
From: Silky Manwani [mailto:[Email Removed]]
Sent: Friday, March 05, 2004 1:16 PM
To: [Email Removed]
Subject: Nothing executes after system.


Hello,

@args = ("/usr/bin/su","sam");
system(@args);
print "hello";

Any idea why print never gets executed? So, the switch user takes place
but nothing gets executed after that..

Thanks.


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response



QUOTE
In a message dated 3/5/2004 6:11:53 PM Eastern Standard Time,
[Email Removed] writes:
i have downloaded cygwin and perl 5.0 comes along with it.
How can i access perl from cygwin. My purpose is only to be able to write a
script in perl >and run it using cygwin. Can anyone help me with this..

regards
aalok


To be specific, I think perl 5.8.2 comes with cygwin :-) You access perl
through cygwin like normal. Just run cygwin and run, 'perl /path/to/script.pl'.
Or, if you put the path (#/usr/local/bin/perl on cygwin) you can run it as
"/path/to/script.pl" (Remember though, if you're in the directory of the perl
script, it isn't "script.pl" it is "./script.pl")

If you have already been coding Perl on a Windows system (just assumed your
on a Windows system because you got cygwin) it doesn't take too much getting
use to since you've had to learn Perl for Unix and then figure out to exceptions
for Windows. cygwin is a relief after you've been fumbling with mc and
ActiveState.


-will
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Deparse
-Data::Dumper
-Clone (a Godsend)
-Perl::Tidy
-Beautifier

Paul Kraus
QUOTE
OK, you owe me a beer :~) Note that this has zilch security or anything to
prevent DoS type attacks, so caveat emptor...
Should I send the ups red label ;)


QUOTE

server.pl
---------
#!/usr/bin/perl

use strict;
use IO::Socket::INET;

my $n = 0;

my $sock = IO::Socket::INET->new(
LocalPort => 12000,
Proto => 'tcp',
Listen => 5)
or die "Couldn't create listen socket: $!";

warn "Waiting for connectionsn";
while (my $client = $sock->accept) {
$n++;
defined(my $pid = fork) or die "Couldn't fork: $!";
next if $pid;

my $host = $client->peerhost;
my $file = "file.$n";
warn "Received connection from $host, writing to $filen";
open F, ">$file";
print F while <$client>;
close F;
warn "Connection from $host closedn";
exit;
}

client.pl
---------
#!/usr/bin/perl

use strict;
use IO::Socket::INET;

my $n = 0;

@ARGV or die "usage: $0 host [port]n";
my $host = shift;
my $port = shift || 12000;

my $sock = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp')
or die "Couldn't connect to $host:$port: $!";

print $sock $_ while <>;
Perfect Thanks!! As far as security and such I can build it back in later in

the week when I finish the network book. Thanks a bunch!!

Paul

Joel
This is off topic, but is cygwin just the ports of GNU software for windows,
or is it an actual command line interface that simulates linux?

Joel

David Le Blanc
QUOTE
-----Original Message-----
From: Joel [mailto:[Email Removed]]
Sent: Saturday, 6 March 2004 1:29 PM
To: perl
Subject: Re: hi!!

I've changed your subject to be more appropriate to the question asked.

QUOTE

This is off topic, but is cygwin just the ports of GNU
software for windows,
or is it an actual command line interface that simulates linux?

Since cygwin contains a port of perl which many of us use, its probably
not that far off topic :-)

The short answer is - somewhere in between.

All the individual ported commands are windows EXE's, which can
be run stand-alone, or put into your path. However they have
been tweaked work closely together and to make common assumptions.
One of these is to assume '/' is a directory of your hard drive
(such as c:cygwin) and all drive letters are 'mounted' as
filesystems relative to /cygdrive/...


The BASH (bash?) shell has been ported, and once you launch
the shell, with appropriate variable settings and .profile
you are in an environment which feels very much like unix, and
probably a lot like linux. Except for the fact you are in a
windows COMMAND shell window, you couldn't tell the difference.

Then, to top it off, an Icon is placed onto your desktop which
launches /sbin/bash from the cygwin directory, and voila, you
are in a unix-like environment.

The port is sufficiently comprehensive, that with the GNOME modules
and networking modules, you could fool someone into thinking you
are running linux, complete with X-based login screen and
gnome desktop.

Cheers.
David

QUOTE

Joel

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




Stuart White
This was my first chance at testing this, and it did
work. How, I'm not sure though.
Specifically:

What do you call the "PRIMES:" and "__END___" syntax?
I'll look up what it's purpose is, I just don't know
it's name.

Next, I don't quite understand the for loops. I
understand that you created two scalars,
$possible_prime and $test within the scope of the for
loops, and I assume you did that because they are not
needed outside of the for loops, right? That part I
understand. However, I don't understand this part of
the line:
for my $possible_prime (@primelist)
If I were to write it, I'd declare $possible_prime at
the top where all of my variables are declared. And
then I'd write it like this:
foreach (@primelist)
Would that be a correct representation?

I'm also not sure how $possible_prime gets a value for
the range operator to work on it in the second for
loop. How does it get a value?

The other constructs that are confusing me are next
and unless. I've read about them, I've just not used
them. I remember reading that unless can be used in
place of while, where the block of code is before
using unless, instead of after like in while. That's
correct, right? I'll have to look up next. I know
when I first read about it, I didn't understand it, so
I've avoided attempting to use it.

Thanks for the help.

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

James Edward Gray II
On Mar 7, 2004, at 8:55 AM, Stuart White wrote:

QUOTE
This was my first chance at testing this, and it did
work.  How, I'm not sure though.

Then, I'm glad you asked. Let's take a closer look.

QUOTE
Specifically:

What do you call the "PRIMES:" and "__END___" syntax?
I'll look up what it's purpose is, I just don't know
it's name.

Let's start with the easy one, __END__. There's no magic going on
here, you're program will run fine without it. Consider it a note to
perl that reads, "My code stops here, don't read on." I add it to
programs I post into messages like this, so you and/or perl, can tell
where the code I posted ends.

PRIMES: ... is a label. I'm labeling the first loop, so I can make my
call to next() push that loop along, instead of the one I'm currently
in. By default, next() works on the immediately enclosing loop.

QUOTE
Next, I don't quite understand the for loops.  I
understand that you created two scalars,
$possible_prime and $test within the scope of the for
loops, and I assume you did that because they are not
needed outside of the for loops, right?

You got that right, yes.

QUOTE
That part I understand.  However, I don't understand this part of
the line:
for my $possible_prime (@primelist)
If I were to write it, I'd declare $possible_prime at
the top where all of my variables are declared.  And
then I'd write it like this:
foreach (@primelist)
Would that be a correct representation?

foreach (@primelist) goes through @primelist one at a time, sticking
each element of the array in the default variable $_. The only
difference in my loop, is that I ask the elements be stuck in
$possible_primes instead. Otherwise, the two are functionally
identical.

The reason I didn't use the default variable, is because we're talking
about nested foreach loops here. If they both stuck their values in
the same place, the inside loop would clobber the outside loops values
and I would have no way to access both.

QUOTE
I'm also not sure how $possible_prime gets a value for
the range operator to work on it in the second for
loop.  How does it get a value?

I think I just answered this above, but let me know if you're still
stuck.

QUOTE
The other constructs that are confusing me are next
and unless.  I've read about them, I've just not used
them.  I remember reading that unless can be used in
place of while, where the block of code is before
using unless, instead of after like in while.  That's
correct, right?

No, unless is just an if with the condition negated. In other words,
the following are identical:

if ( not( some_conditional_test ) )

unless ( some_conditional_test )

QUOTE
I'll have to look up next.  I know
when I first read about it, I didn't understand it, so
I've avoided attempting to use it.

next() jumps to the next iteration of the target loop, or the enclosing
loop, by default. Consider this example:

foreach (1..10) {
next if $_ % 2; # skip to the next number, if this one is odd
print "$_n"; # this prints just even numbers
}

Hope that clears up the rest of your confusions.

James

Stuart White
--- James Edward Gray II <[Email Removed]>
wrote:


QUOTE
Let's start with the easy one, __END__.  There's no
magic going on
here, you're program will run fine without it.
Consider it a note to
perl that reads, "My code stops here, don't read
on."  I add it to
programs I post into messages like this, so you
and/or perl, can tell
where the code I posted ends.

Ok, that makes sense.


QUOTE
PRIMES: ... is a label.  I'm labeling the first
loop, so I can make my
call to next() push that loop along, instead of the
one I'm currently
in.  By default, next() works on the immediately
enclosing loop.


This makes sense.


QUOTE



The reason I didn't use the default variable, is
because we're talking
about nested foreach loops here.  If they both stuck
their values in
the same place, the inside loop would clobber the
outside loops values
and I would have no way to access both.


Ah, so there is a use for the for which is like
foreach other than a shortcut. Can I do that with
foreach? I see that what you are describing with the
foreach loops above is what was going on with my
nested foreach loops before.


QUOTE
next() jumps to the next iteration of the target
loop, or the enclosing
loop, by default.  Consider this example:

foreach (1..10) {
next if $_ % 2;  # skip to the next number, if this
one is odd
print "$_n";  # this prints just even numbers
}


That is helpful. Though why would you want to use
next? Is is just another way to do something?

QUOTE
Hope that clears up the rest of your confusions.


Yup, for the most part. Thanks.
QUOTE
James



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

Stuart White
--- James Edward Gray II <[Email Removed]>
wrote:


QUOTE
Let's start with the easy one, __END__.  There's no
magic going on
here, you're program will run fine without it.
Consider it a note to
perl that reads, "My code stops here, don't read
on."  I add it to
programs I post into messages like this, so you
and/or perl, can tell
where the code I posted ends.

Ok, that makes sense.


QUOTE
PRIMES: ... is a label.  I'm labeling the first
loop, so I can make my
call to next() push that loop along, instead of the
one I'm currently
in.  By default, next() works on the immediately
enclosing loop.


This makes sense.


QUOTE



The reason I didn't use the default variable, is
because we're talking
about nested foreach loops here.  If they both stuck
their values in
the same place, the inside loop would clobber the
outside loops values
and I would have no way to access both.


Ah, so there is a use for the for which is like
foreach other than a shortcut. Can I do that with
foreach? I see that what you are describing with the
foreach loops above is what was going on with my
nested foreach loops before.


QUOTE
next() jumps to the next iteration of the target
loop, or the enclosing
loop, by default.  Consider this example:

foreach (1..10) {
next if $_ % 2;  # skip to the next number, if this
one is odd
print "$_n";  # this prints just even numbers
}


That is helpful. Though why would you want to use
next? Is is just another way to do something?

QUOTE
Hope that clears up the rest of your confusions.


Yup, for the most part. Thanks.
QUOTE
James



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

R. Joseph Newton
Stuart White wrote:

QUOTE
This was my first chance at testing this, and it did
work.  How, I'm not sure though.
Specifically:

What do you call the "PRIMES:" and "__END___" syntax?

The two are not directly related. The way PRIMES is used here makes it a
label. This allows rapid, uncomplicated iteration through the outer list if
$possible_prime is evenly divisible by, and thus has modulus zero in relation
to, $test;

The __END__ syntax, on the other hand, tells the Perl interpreter tthat the end
of profram code has been reached for the file. It can be useful if the code
section is followed by either documentation or data. With documentation, it
prevents the interpreter from taking the text as code. If you have a reference
to <DATA>, Perl will also look in the section following __END__ or __DATA__ for
the data to be processed.

Greetings! E:d_driveperlStuff>perl -w
while (<DATA>) {
print;
}

__END__
Hello
Hello
Goodbye
Goodbye
^Z

I just checked, and this is special magic with the __END__ and __DATA__ tags.
For instance:

Greetings! E:d_driveperlStuff>perl -w
while (<DATA>) {
print;
}

__JABBERWOCK__
Hello

Does not work, as we can see.

Joseph

Wc -Sx- Jones
R. Joseph Newton wrote:

QUOTE
Greetings! E:d_driveperlStuff>perl -w
while  (<DATA>) {
print;
}

__JABBERWOCK__
Hello



YOU can YOU can :) Just write an Inline for it:

use Inline JABBERWOCK;

goto_AlphaCenturi();

__END__

__JABBERWOCK__

void goto_AlphaCentur() {

printf("I've been banished *to* Jabberwock, but at
least I have Perl %sn",

SvPVX(eval_pv("use Config; $Config{version}",

0)));

}


(Obviously since Inline JABBERWOCK doesnt YET exist, this geberates some
errors - sort of ... you will need to have Inline installed to see.)

-Bill-
__Sx__________________________________________
http://youve-reached-the.endoftheinternet.org/

John W. Krahn
"R. Joseph Newton" wrote:
QUOTE

I just checked, and this is special magic with the __END__ and __DATA__ tags.
For instance:

Greetings! E:d_driveperlStuff>perl -w
while  (<DATA>) {
print;
}

__JABBERWOCK__
Hello

Does not work, as we can see.

If you want __JABBERWOCK__ to work then use the Inline::Files module.

use Inline::Files;
while (<JABBERWOCK>) {
print;
}

__JABBERWOCK__
Hello



John
--
use Perl;
program
fulfillment

Stuart White
--- James Edward Gray II <[Email Removed]>
wrote:
QUOTE
On Mar 7, 2004, at 7:07 PM, Stuart White wrote:

Ah, so there is a use for the for which is like
foreach other than a shortcut.  Can I do that with
foreach?  I see that what you are describing with
the
foreach loops above is what was going on with my
nested foreach loops before.

I believe this is the remaining source of your
confusion and I bet I
can make it go away by explaining one simple fact:
Perl allows the
words for and foreach to be used interchangeably,
they mean exactly the
same thing.  That help?


Yup. thanks.

QUOTE
next() jumps to the next iteration of the target
loop, or the enclosing
loop, by default.  Consider this example:

foreach (1..10) {
next if $_ % 2;  # skip to the next number, if
this
one is odd
print "$_n";  # this prints just even numbers
}


That is helpful.  Though why would you want to use
next?  Is is just another way to do something?

It could be sure.  There's generally more than one
way to do things,
especially in Perl.  In the above example, you could
just put the
print() call inside a conditional.  However, there
are times when doing
something without next() would be a lot more work.
My nested for loops
to find prime numbers is probably a decent example
of that.


I see.

QUOTE
Hope that helps.

James

Thanks all for your patience and assistance.


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

James Edward Gray II
On Mar 7, 2004, at 7:07 PM, Stuart White wrote:

QUOTE
Ah, so there is a use for the for which is like
foreach other than a shortcut.  Can I do that with
foreach?  I see that what you are describing with the
foreach loops above is what was going on with my
nested foreach loops before.

I believe this is the remaining source of your confusion and I bet I
can make it go away by explaining one simple fact: Perl allows the
words for and foreach to be used interchangeably, they mean exactly the
same thing. That help?

QUOTE
next() jumps to the next iteration of the target
loop, or the enclosing
loop, by default.  Consider this example:

foreach (1..10) {
next if $_ % 2;  # skip to the next number, if this
one is odd
print "$_n";  # this prints just even numbers
}


That is helpful.  Though why would you want to use
next?  Is is just another way to do something?

It could be sure. There's generally more than one way to do things,
especially in Perl. In the above example, you could just put the
print() call inside a conditional. However, there are times when doing
something without next() would be a lot more work. My nested for loops
to find prime numbers is probably a decent example of that.

Hope that helps.

James

Stuart White
Here's a line:

(10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO)
Steal: Jackson (1 ST)

I want to match PHX Stoudemire Steal: Jackson

these are my patterns:
my $steal = 'Steal:s';
my $team = 'w{3}';
my $player = 'w+';

this is my regex:
if ($_ =~ /[($team)] ($player).($steal)/)

I tried printing $3 and $4, and then $1 and $2, and
none of them printed. I'm not sure why. What am I
missing?

Eventually, I want to match any of these four lines:
(10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO)
Steal: Jackson (1 ST)

(10:51) [SAN 4-0] Jackson Jump Shot: Made (2 PTS)
Assist: Duncan (1 AST)

(9:33) [SAN] Duncan Layup Shot: Missed Block:
Stoudemire (2 BLK)

(5:35) [SAN] Bowen Substitution replaced by Ginobili

and when I tried to with this:

if ($_ =~ /[($team)] ($player).('Assist: '|'Block:
'|'Steal: '|'replaced by ')/)

and tried to print any of the backreferences, it
didn't work either. Any ideas?
HEre is my code:
-----------------

open(STATS, "stats.txt") or die "statfilen";

my $pattern = "Foul";
my $assist = 'Assists';
my $block = 'Blocks';
my $steal = 'Steal:s';
my $time = 'd+:dd';
my $team = 'w{3}';
my $player = 'w+';
my $foulType = 'Foul: (.*) ';
my $numFouls = 'w+sw+';
my @SAN;
my @PHX;
my %PHX;
my %SAN;

while (<STATS>)
{
if ($_ =~ /[($team)] ($player).($steal)/)
#|$block|$steal
{
print "this is 1: $1n";
print "this is 2: $2n";
print "this is 3: $3n";
print "this is 4: $4n";

}
}



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

Jim Halkyard
-----Original Message-----
From: Gordon Low [mailto:[Email Removed]]
Sent: 09 March 2004 08:29
To: [Email Removed]
Subject: Logical problem with small script??


Wondered if anyone can throw some light on why this script won't act
properly.

Wrote it to convert a string using a ceaser cipher for a course I am
doing. Basically get the string and replace each character with the
character offset by 3 or 4 up to 26. Have to try up to 26 times so it
was crying out for a script, trouble is it doesn't work the way I
intended and I cannot see how.

No real attempts at error catching have been done as it was only
supposed to be used by myself.
Supposed to do this
Enter
QUOTE
cipher.pl phhw dw plgqljkw
....and get all 26 different combinations to crack the message.

Trouble is I get this.....
qiix ex qmhrmklx -------> offset = 1 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
skkz gz sojtomnz -------> offset = 3 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
skkz gz sojtomnz -------> offset = 3 <-------
tlla ha tpkupnoa -------> offset = 4 <-------

Instead of just going through once it is repeating ie loops to 1 then
loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at
offset 23 but I don't want it looping all the time. Code follows, I am
on Linux 8.0 with Perl 5.8, there is probably better ways to do this but
it is driving me mad trying to see the logical problem, hoping someone
can help.

*******************************************************************
#!/usr/bin/perl -w

use strict;

my @alpha =
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"
,"t","u","v","w","x","y","z");
my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){ # set up a lookup table for the array
offset
$alphat{$alpha[$count]} = $count;
$count++;
}

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){ # try all ceaser cipher offsets
foreach (@ARGV){ # take in each command line arg
for ($count=0; $count < length($_); $count++){ # go through
each arg and convert with cipher
$op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)]; # conversion
}
$op .= " "; # space between args
}
$op .= " -------> offset = $ceascount <-------n"; # final result
} # use another ceaser cipher offset



*******************************************************************



Hi Gordon,

The problem here not really the logic of the script. Each time you loop
through on the outer for loop $op is being appended to, not over written.
Each time though the loop you are processing a different offset but printing
that and all previous ones.

To fix this simply move the line my $op = ""; to be the first line inside
the outer for loop and move the print to the end of that loop.

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++)
{
my $op = ""; # This means $op is empty each time through
foreach (@ARGV)
{
for ($count=0; $count < length($_); $count++)
{
$op .= $alpha[($alphat{substr($_,$count,1)} +
$ceascount) %($#alpha+1)];
}
$op .= " ";
}
$op .= " -------> offset = $ceascount <-------n"; # final result
print $op,"n";
}

Alternatively move the print to outside the loops all together. ie only
print once.

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++)
{
foreach (@ARGV)
{
for ($count=0; $count < length($_); $count++)
{
$op .= $alpha[($alphat{substr($_,$count,1)} +
$ceascount) %($#alpha+1)];
}
$op .= " ";
}
$op .= " -------> offset = $ceascount <-------n";
}
print $op,"n";

Hope this helps,

Jim

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Gordon Low
Wondered if anyone can throw some light on why this script won't act
properly.

Wrote it to convert a string using a ceaser cipher for a course I am
doing. Basically get the string and replace each character with the
character offset by 3 or 4 up to 26. Have to try up to 26 times so it
was crying out for a script, trouble is it doesn't work the way I
intended and I cannot see how.

No real attempts at error catching have been done as it was only
supposed to be used by myself.
Supposed to do this
Enter
QUOTE
cipher.pl phhw dw plgqljkw
....and get all 26 different combinations to crack the message.

Trouble is I get this.....
qiix ex qmhrmklx -------> offset = 1 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
skkz gz sojtomnz -------> offset = 3 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
skkz gz sojtomnz -------> offset = 3 <-------
tlla ha tpkupnoa -------> offset = 4 <-------

Instead of just going through once it is repeating ie loops to 1 then
loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at
offset 23 but I don't want it looping all the time. Code follows, I am
on Linux 8.0 with Perl 5.8, there is probably better ways to do this but
it is driving me mad trying to see the logical problem, hoping someone
can help.

*******************************************************************
#!/usr/bin/perl -w

use strict;

my @alpha =
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){ # set up a lookup table for the array
offset
$alphat{$alpha[$count]} = $count;
$count++;
}

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){ # try all ceaser cipher offsets
foreach (@ARGV){ # take in each command line arg
for ($count=0; $count < length($_); $count++){ # go through
each arg and convert with cipher
$op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)]; # conversion
}
$op .= " "; # space between args
}
$op .= " -------> offset = $ceascount <-------n"; # final result
} # use another ceaser cipher offset



*******************************************************************

Eurospace Szarindar
Hi Gordon,

why don't you use the power of pack and unpack

QUOTE
print unpack ("C",a)    # returns 97
print pack ("i", unpack ("C",A)+3) # returns D
print pack ("i*", map ( $_+=3 , (unpack ("C*",ABCD))) ) # returns DEFG

hope it helps

Michel

-----Message d'origine-----
De: Gordon Low [mailto:[Email Removed]]
Date: mardi 9 mars 2004 09:29
: [Email Removed]
Objet: Logical problem with small script??


Wondered if anyone can throw some light on why this script won't act
properly.

Wrote it to convert a string using a ceaser cipher for a course I am
doing. Basically get the string and replace each character with the
character offset by 3 or 4 up to 26. Have to try up to 26 times so it
was crying out for a script, trouble is it doesn't work the way I
intended and I cannot see how.

No real attempts at error catching have been done as it was only
supposed to be used by myself.
Supposed to do this
Enter
QUOTE
cipher.pl phhw dw plgqljkw
...and get all 26 different combinations to crack the message.

Trouble is I get this.....
qiix ex qmhrmklx -------> offset = 1 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
skkz gz sojtomnz -------> offset = 3 <-------
qiix ex qmhrmklx -------> offset = 1 <-------
rjjy fy rnisnlmy -------> offset = 2 <-------
skkz gz sojtomnz -------> offset = 3 <-------
tlla ha tpkupnoa -------> offset = 4 <-------

Instead of just going through once it is repeating ie loops to 1 then
loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at
offset 23 but I don't want it looping all the time. Code follows, I am
on Linux 8.0 with Perl 5.8, there is probably better ways to do this but
it is driving me mad trying to see the logical problem, hoping someone
can help.

*******************************************************************
#!/usr/bin/perl -w

use strict;

my @alpha ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"
,"t","u","v","w","x","y","z");
my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){ # set up a lookup table for the array
offset
$alphat{$alpha[$count]} = $count;
$count++;
}

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){ # try all ceaser cipher offsets
foreach (@ARGV){ # take in each command line arg
for ($count=0; $count < length($_); $count++){ # go through
each arg and convert with cipher
$op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)]; # conversion
}
$op .= " "; # space between args
}
$op .= " -------> offset = $ceascount <-------n"; # final result
} # use another ceaser cipher offset



*******************************************************************


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Stuart White
Okey doke, I didn't think to think that just changing
the subject line would still put the message in the
previous thread. My mistake.


--- James Edward Gray II <[Email Removed]>
wrote:
QUOTE
Stuart, please start new messages for new topics,
instead of replying
to existing threads.

On Mar 8, 2004, at 8:17 PM, Stuart White wrote:

Here's a line:

(10:18) [PHX] Stoudemire Turnover: Lost Ball (1
TO)
Steal: Jackson (1 ST)

I want to match PHX Stoudemire Steal: Jackson

See if this example gets you going.

James

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
if (m/[([A-Z0-9 -]+)]
(w+).+(?:Steal|Assist|Block|replaced by):
(w+)/) {
  print "$1 $2 $3n";
}
}

__DATA__
(10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO)
Steal: Jackson (1
ST)
(10:51) [SAN 4-0] Jackson Jump Shot: Made (2 PTS)
Assist: Duncan (1 AST)
(9:33) [SAN] Duncan Layup Shot: Missed Block:
Stoudemire (2 BLK)
(5:35) [SAN] Bowen Substitution replaced by Ginobili



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

James Edward Gray II
On Mar 8, 2004, at 8:17 PM, Stuart White wrote:

> (5:35) [SAN] Bowen Substitution replaced by Ginobili

James Edward Gray II
Stuart, please start new messages for new topics, instead of replying
to existing threads.

On Mar 8, 2004, at 8:17 PM, Stuart White wrote:

QUOTE
Here's a line:

(10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO)
Steal: Jackson (1 ST)

I want to match PHX Stoudemire Steal: Jackson

See if this example gets you going.

James

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
if (m/[([A-Z0-9 -]+)] (w+).+(?:Steal|Assist|Block|replaced by):
(w+)/) {
print "$1 $2 $3n";
}
}

__DATA__
(10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO) Steal: Jackson (1
ST)
(10:51) [SAN 4-0] Jackson Jump Shot: Made (2 PTS) Assist: Duncan (1 AST)
(9:33) [SAN] Duncan Layup Shot: Missed Block: Stoudemire (2 BLK)
(5:35) [SAN] Bowen Substitution replaced by Ginobili

In a message dated 3/9/2004 2:43:16 PM Eastern Standard Time,
[Email Removed] writes:
QUOTE
Hi all, I'm trying to figure out how can I check if a variable matches the
first 5 digits of the line below without removing anything from the line.

13384 R 20020920 N Gatekeeper, The

Silver Fox

use strict;
use warnings;
my $line = "13384 R 20020920 N Gatekeeper, The";
my $var = 13384;
my ($match) = ($line =~ /(d{5})/);
print "Match! ", $var,"==", $match,"n" if $var == $match;
#OR
my $substr = substr $line, 0, 5;
print "Match! ",$var,"==",$substr,"n" if $var == $substr;

#Is the line the same?
print $line,"n";



-will
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Deparse
-Data::Dumper
-Clone (a Godsend)
-Perl::Tidy
-Beautifier

James Edward Gray II
On Mar 9, 2004, at 2:28 AM, Gordon Low wrote:

QUOTE
Wondered if anyone can throw some light on why this script won't act
properly.

Looks like you've already been helped with the print problem, but I
thought I would add two things.

[snip]

QUOTE
#!/usr/bin/perl -w

use strict;

my @alpha =
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r
","s","t","u","v","w","x","y","z");
my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){                  # set up a lookup table for the
array
offset
$alphat{$alpha[$count]} = $count;
$count++;
}

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){    # try all ceaser cipher offsets
foreach (@ARGV){              # take in each command line arg
for ($count=0; $count < length($_); $count++){      # go through
each arg and convert with cipher

First, I believe there is a logic bug in the line above. I believe you
meant (length($_) - 1). The warnings the program was issuing helped me
find it.

QUOTE
$op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)];  # conversion
}
$op .= " ";  # space between args
}
$op .= " -------> offset = $ceascount <-------n";  # final result
}    # use another ceaser cipher offset

And two, here's a more Perlish example of your script:

#!/usr/bin/perl

use strict;
use warnings;

my @alpha = ('a'..'z');
my %alphat;
foreach (0..$#alpha){
$alphat{$alpha[$_]} = $_;
}

my $op = '';
for my $ceascount (1..scalar @alpha){
foreach (@ARGV) {
for my $count (0..(length($_) - 1)){
$op .= $alpha[($alphat{substr $_, $count, 1} + $ceascount) %
@alpha];
}
$op .= ' '; # space between args
}
$op .= " -------> offset = $ceascount <-------n";
}
print $op, "n";

__END__

I'm not trying to say you need to be writing like this. I just thought
you might find a shortcut or two in there you like.

Good luck.

James

In a message dated 3/9/2004 3:08:10 PM Eastern Standard Time,
[Email Removed] writes:
QUOTE
Careful, I believe we want the above capture anchored to the start of
the line, though I prefer the direct match approach...

You're right about the anchor, but direct match wouldn't be the preferable
method here. Say, using your code, my $variable = 1338. The regex is true, even
though the number will be different.You'd have to do /^$variables+/ (even
then, $variable could be "13384 R", be true, and mess things up). Regex in
conditionals can be effy and ambiguous, and if they can be avoided, why not avoid
them.

-will
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Deparse
-Data::Dumper
-Clone (a Godsend)
-Perl::Tidy
-Beautifier

Wiggins D Anconia
QUOTE
At 11:08 PM 3/8/04 -0500, Wiggins d'Anconia wrote:
This is where I do my usual little dance, though untimely as it may be
in this case, across the stage and say "POE" then exit again as if I was
still on vacation....

OK, I'll bite.  What's so great about POE, and why, oh, why, do you
love it
so?


Well, that is a tough one. That is the problem with POE, steep learning
curve. The reason why it is so great is that it allows you to take some
of those things that "Perl makes possible" and turn them back into
things that "Perl makes easy".

In this case the desire to fork multiple processes and maintain
bi-directional communication with them becomes a nightmare of dealing
with forking code, pipes, and all of the other IPC nasties.
POE::Wheel::Run encapsulates all of that providing a very simple
interface for dealing with forking those processes and handling their
input/output through events. So all of the pipe/select stuff gets
hidden. Obviously there are some limitations of what you can do (aka
real-time kind of loses some of its meaning) etc. but for the most part
hiding the gory details is usually not a problem. The same can be said
for TCP servers, and other types of daemons.

I love it because it made an application that was a major pain in the
rear-end incredibly elegant and simple in a very short amount of time
(aka after I got over the learning curve).

http://danconia.org

Michael C. Davis
At 08:54 AM 3/9/04 -0700, Wiggins d Anconia wrote:
QUOTE
In this case the desire to fork multiple processes and maintain
bi-directional communication with them becomes a nightmare of dealing
with forking code, pipes, and all of the other IPC nasties.
POE::Wheel::Run encapsulates all of that providing a very simple
interface for dealing with forking those processes and handling their
input/output through events.  So all of the pipe/select stuff gets
hidden.  Obviously there are some limitations of what you can do (aka
real-time kind of loses some of its meaning) etc. but for the most part
hiding the gory details is usually not a problem.  The same can be said
for TCP servers, and other types of daemons.

So, it's a tool for managing a family of forked processes? It's primary
focus is IPC-related functionality?

Wiggins D Anconia
QUOTE
At 08:54 AM 3/9/04 -0700, Wiggins d Anconia wrote:
In this case the desire to fork multiple processes and maintain
bi-directional communication with them becomes a nightmare of dealing
with forking code, pipes, and all of the other IPC nasties.
POE::Wheel::Run encapsulates all of that providing a very simple
interface for dealing with forking those processes and handling their
input/output through events.  So all of the pipe/select stuff gets
hidden.  Obviously there are some limitations of what you can do (aka
real-time kind of loses some of its meaning) etc. but for the most part
hiding the gory details is usually not a problem.  The same can be said
for TCP servers, and other types of daemons.

So, it's a tool for managing a family of forked processes?  It's primary
focus is IPC-related functionality?



Not exactly, that would be pigeon holing it. While it can handle those
things very well, it is certainly not limited to them. It is easiest to
think of it as a framework for handling time slicing, or event
programming tasks, or multi-tasking processes. A good way to see what
it is capable of is to check out the cookbook found here:

http://poe.perl.org/?POE_Cookbook

But realize that those are provided as documentation rather than as an
exhaustive list of what it can do. There really isn't an exhaustive list
of what it can do since it is a framework rather than any specific
functionality. I was trying to come up with a good analogy but couldn't
seem to find an appropriate one....

http://danconia.org

Michael C. Davis
At 09:52 AM 3/9/04 -0700, Wiggins d Anconia wrote:
QUOTE
...
a framework for handling time slicing, or event
programming tasks, or multi-tasking processes

If that's what it is, these terms localize it pretty well for me. "An
event-based framework for handling time-slicing in multitasking processes."

Wiggins D Anconia
QUOTE
At 09:52 AM 3/9/04 -0700, Wiggins d Anconia wrote:
...
a framework for handling time slicing, or event
programming tasks, or multi-tasking processes

If that's what it is, these terms localize it pretty well for me. "An
event-based framework for handling time-slicing in multitasking
processes."



That represents a good generalization I think. The introduction on
poe.perl.org is better, but longer.

Of course using "event based framework for handling time-slicing in
multitasking processes" often won't work any better than a "famboozled
watchamathingie with xanatically whooziewhatsit" on a beginners list ;-)...

http://danconia.org

Michael C. Davis
At 11:32 AM 3/9/04 -0700, Wiggins d Anconia wrote:
QUOTE
... "famboozled watchamathingie with xanatically whooziewhatsit" on a
beginners list ;-)...



Oh yeah, since you bring it up .... about the whooziewhatsit ...

Stuart White
QUOTE
See if this example gets you going.

James

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
if (m/[([A-Z0-9 -]+)]
(w+).+(?:Steal|Assist|Block|replaced by):
(w+)/) {
  print "$1 $2 $3n";
}
}


That got me started. I do have a question though
about your regex.

This backreference is $1, and matches the team
abbreviation.
([A-Z0-9 -]+)

This one is $2, and matches the first name:
(w+)

And this one I'd think should be $3, and match Steal
or Assist etc., but the results don't say that.

(?:Steal|Assist|Block|replaced by)

Instead, $3 is this, and matches the second name.
(w+)

If backreferences are supposed to be in parentheses,
why isn't this
(?:Steal|Assist|Block|replaced by)
a backreference?

Thanks.

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

John W. Krahn
Gordon Low wrote:
QUOTE

Wondered if anyone can throw some light on why this script won't act
properly.

Certainly.

QUOTE
Wrote it to convert a string using a ceaser cipher for a course I am
doing. Basically get the string and replace each character with the
character offset by 3 or 4 up to 26. Have to try up to 26 times so it
was crying out for a script, trouble is it doesn't work the way I
intended and I cannot see how.

No real attempts at error catching have been done as it was only
supposed to be used by myself.
Supposed to do this
Enter
cipher.pl phhw dw plgqljkw
...and get all 26 different combinations to crack the message.
Trouble is I get this.....
qiix ex qmhrmklx  -------> offset = 1 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
skkz gz sojtomnz  -------> offset = 3 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
skkz gz sojtomnz  -------> offset = 3 <-------
tlla ha tpkupnoa  -------> offset = 4 <-------

Instead of just going through once it is repeating ie loops to 1 then
loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at
offset 23 but I don't want it looping all the time. Code follows, I am
on Linux 8.0 with Perl 5.8, there is probably better ways to do this but
it is driving me mad trying to see the logical problem, hoping someone
can help.

*******************************************************************
#!/usr/bin/perl -w
use strict;

my @alpha =
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

Ouch. Writing out each letter is error prone. It is better to use the
range operator to create the list.

my @alpha = 'a' .. 'z';

QUOTE
my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){                  # set up a lookup table for the array
offset
$alphat{$alpha[$count]} = $count;
$count++;
}

When this loop runs I get the warning:

Use of uninitialized value in array element at ./cipher.pl line 92.

Where line 92 is "$alphat{$alpha[$count]} = $count;" because the initial
value in $count is undef. You can explicitly set $count:

my $count = 0;
foreach ( @alpha ){ # set up a lookup table for the array offset
$alphat{ $alpha[ $count ] } = $count;
$count++;
}

However, I was wondering why you are looping through the values in @alpha
but not using them?

foreach ( @alpha ){ # set up a lookup table for the array offset
$alphat{ $_ } = $count++;
}

That eliminates the warnings message and makes the code shorter and easier
to read but you can do the same thing without the foreach loop and the $count
variable by using a hash slice:

# set up a lookup table for the array offset
@alphat{ @alpha } = 0 .. $#alpha;


QUOTE
for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){    # try all ceaser cipher offsets

That looks more like C then Perl. :-) A more perl-ish way to write that is:

for my $ceascount ( 1 .. $#alpha ) { # try all ceaser cipher offsets
...
}
continue {
print $op;
}

Your problem is that after printing $op it retains the contents from the
previous iteration of the loop. You need to clear out the contents of $op
after you print it:

for my $ceascount ( 1 .. $#alpha ) { # try all ceaser cipher offsets
...
}
continue {
print $op;
$op = '';
}


QUOTE
foreach (@ARGV){              # take in each command line arg
for ($count=0; $count < length($_); $count++){      # go through
each arg and convert with cipher

A more perl-ish for loop would be:

for my $count ( 0 .. length() - 1 ){ # go through each arg and convert with cipher


QUOTE
$op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)];  # conversion
}
$op .= " ";  # space between args
}
$op .= " -------> offset = $ceascount <-------n";  # final result
}    # use another ceaser cipher offset

A simpler way to write that:

#!/usr/bin/perl
use warnings;
use strict;

my @alpha = 'a' .. 'z';
my @ceaser = @alpha[ 1 .. $#alpha, 0 ];

my %alphat;
@alphat{ @alpha } = @ceaser;

my $code = "@ARGV";

for my $ceascount ( 1 .. $#alpha ) {
( my $message = $code ) =~ s/([@alpha])/ exists $alphat{ $1 } ? $alphat{ $1 } : $1 /eg;
print "$message -------> offset = $ceascount <-------n";
@alphat{ @alpha } = @ceaser = @ceaser[ 1 .. $#ceaser, 0 ];
}

__END__



John
--
use Perl;
program
fulfillment

Randal L. Schwartz
QUOTE
"Michael" == Michael C Davis <[Email Removed]> writes:

Michael> At 09:52 AM 3/9/04 -0700, Wiggins d Anconia wrote:
QUOTE
...
a framework for handling time slicing, or event
programming tasks, or multi-tasking processes

Michael> If that's what it is, these terms localize it pretty well for me. "An
Michael> event-based framework for handling time-slicing in multitasking processes."

"An event loop on steroids"

Most of us have written programs to do many things, but usually in an
orderly fashion. If you've ever found yourself writing a program that
was trying to do many things *at once* in an orderly fashion, you'll
want an event loop. An event loop associates reactions with each of a
set of actions of interest.

A simple version of this might be like trying to watch two growing
files at once, and interleaving the results into one larger file.
With "simple" Perl, this is pretty hard, because you don't have
asynchronous I/O. With POE, you can grab the lines from "both" files
at the "same" time in an orderly way, performing identical or distinct
actions as each line is seen.

For example, in
<http://www.stonehenge.com/merlyn/PerlJournal/col01.html>, I used POE
to write a process that tails a file, noting the time at which each
line appears, and in parallel also responding to one (or many) web
browsers as a web server, delivering a color-coded version of the
trailing lines of the file. In
<http://www.stonehenge.com/merlyn/LinuxMag/col41.html>, I used POE in
a parallel link checker, overlapping DNS lookups with page fetch
response times to fetch things three to ten times faster in a single
process than I might have done without POE. In
<http://www.stonehenge.com/merlyn/PerlJournal/col09.html>, I tail a
logfile (I do that a lot :) and deliver it to an IRC channel,
throttled appropriately so that I don't get booted off for channel
flooding.

POE is like forking, without the complete separation of data
and troublesome IPC issues.

POE is like threads, but with built-in data locking.

POE requires some discipline, but can be very powerful when used
correctly.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +
<[Email Removed]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

David Le Blanc
QUOTE
-----Original Message-----
From: WC -Sx- Jones [mailto:[Email Removed]]
Sent: Tuesday, 9 March 2004 5:54 PM
To: [Email Removed]
Subject: Re: HERE DOCUMENTS and teachable moments

R. Joseph Newton wrote:

Better just to respond to the situations as they arise.
Sometimes this will mean pointing a person back to a recent
thread that they *really
should have* read themselves before posting.  Sometimes a
thread may also lead to other questions about the approach a
person is taking to
learning Perl, or to structuring problems.  Message threads
can take some interesting courses, and it is not only more
spontaneous, but  also
more information-efficient, to make the knowledge
meaningful in its context, to find the teachable moment.


I agree - so I am totally talked out of the 'how to' thread.

I would rather not alienate anyone who is helping if people
feel its not
contributing.


Well that's just too darn bad..

*I* for one learnt something while reading the aforementioned
'how to' and was looking forward to another helping.

I agree the problem was the format not the content, since most
newbie's, if they are familiar with 'google' et al, are going to
search for solutions by stating their problem.

Can we create a 'how do' keyed on 'problems answered' rather
than 'pure perl'. After all, who reads a 'how to' for something
they are not trying to do at the time :-)

What about maintaining YAPF in the format of a web page/site which
presents a number of 'keywords' to search engines for which the page
provides a practical Perl answer. There is always room for more.

Robin Sheat
On Wed, Mar 10, 2004 at 11:30:35AM +1100, David le Blanc wrote:
QUOTE
Can we create a 'how do' keyed on 'problems answered' rather
than 'pure perl'.  After all, who reads a 'how to' for something
they are not trying to do at the time :-)
I started my own for my own problems at:

http://www.kallisti.net.nz/PerlTips
It only has one thing there, but that is because I wrote it last
night...my main programming computer has been down for a few days, so I
haven't had any other things come to mind just yet.

People are welcome to chuck howtos or whatever up there (two points: one
is that I'll create categories (and sub-categories) as indexes become
full, the other is that if you post something, please be pedantic about
how it appears...use the one I wrote up as an example if you like...I'm
a formatting/style pedant and think everyone else should be too:)

--
Robin <[Email Removed]> JabberID: <[Email Removed]>

Hostes alienigeni me abduxerunt. Qui annus est?

PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663

Gordon Low
Thanks to all for replying, biggest mistake I made was not clearing the
string but have been comparing how others would have done the same
script and finding it very useful. Am not a great scripter so all hints
welcome, will be checking scripts for techniques.




On Wed, 2004-03-10 at 09:09, John W. Krahn wrote:
QUOTE
Gordon Low wrote:

Wondered if anyone can throw some light on why this script won't act
properly.

Certainly.

Wrote it to convert a string using a ceaser cipher for a course I am
doing. Basically get the string and replace each character with the
character offset by 3 or 4 up to 26. Have to try up to 26 times so it
was crying out for a script, trouble is it doesn't work the way I
intended and I cannot see how.

No real attempts at error catching have been done as it was only
supposed to be used by myself.
Supposed to do this
Enter
cipher.pl phhw dw plgqljkw
...and get all 26 different combinations to crack the message.
Trouble is I get this.....
qiix ex qmhrmklx  -------> offset = 1 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
skkz gz sojtomnz  -------> offset = 3 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
skkz gz sojtomnz  -------> offset = 3 <-------
tlla ha tpkupnoa  -------> offset = 4 <-------

Instead of just going through once it is repeating ie loops to 1 then
loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at
offset 23 but I don't want it looping all the time. Code follows, I am
on Linux 8.0 with Perl 5.8, there is probably better ways to do this but
it is driving me mad trying to see the logical problem, hoping someone
can help.

*******************************************************************
#!/usr/bin/perl -w
use strict;

my @alpha =
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

Ouch.  Writing out each letter is error prone.  It is better to use the
range operator to create the list.

my @alpha = 'a' .. 'z';

my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){                  # set up a lookup table for the array
offset
$alphat{$alpha[$count]} = $count;
$count++;
}

When this loop runs I get the warning:

Use of uninitialized value in array element at ./cipher.pl line 92.

Where line 92 is "$alphat{$alpha[$count]} = $count;" because the initial
value in $count is undef.  You can explicitly set $count:

my $count = 0;
foreach ( @alpha ){    # set up a lookup table for the array offset
$alphat{ $alpha[ $count ] } = $count;
$count++;
}

However, I was wondering why you are looping through the values in @alpha
but not using them?

foreach ( @alpha ){    # set up a lookup table for the array offset
$alphat{ $_ } = $count++;
}

That eliminates the warnings message and makes the code shorter and easier
to read but you can do the same thing without the foreach loop and the $count
variable by using a hash slice:

# set up a lookup table for the array offset
@alphat{ @alpha } = 0 .. $#alpha;


for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){    # try all ceaser cipher offsets

That looks more like C then Perl.  :-)  A more perl-ish way to write that is:

for my $ceascount ( 1 .. $#alpha ) {    # try all ceaser cipher offsets
...
}
continue {
print $op;
}

Your problem is that after printing $op it retains the contents from the
previous iteration of the loop.  You need to clear out the contents of $op
after you print it:

for my $ceascount ( 1 .. $#alpha ) {    # try all ceaser cipher offsets
...
}
continue {
print $op;
$op = '';
}


foreach (@ARGV){              # take in each command line arg
for ($count=0; $count < length($_); $count++){      # go through
each arg and convert with cipher

A more perl-ish for loop would be:

for my $count ( 0 .. length() - 1 ){      # go through each arg and convert with cipher


$op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)];  # conversion
}
$op .= " ";  # space between args
}
$op .= " -------> offset = $ceascount <-------n";  # final result
}    # use another ceaser cipher offset

A simpler way to write that:

#!/usr/bin/perl
use warnings;
use strict;

my @alpha = 'a' .. 'z';
my @ceaser = @alpha[ 1 .. $#alpha, 0 ];

my %alphat;
@alphat{ @alpha } = @ceaser;

my $code = "@ARGV";

for my $ceascount ( 1 .. $#alpha ) {
( my $message = $code ) =~ s/([@alpha])/ exists $alphat{ $1 } ? $alphat{ $1 } : $1 /eg;
print "$message  -------> offset = $ceascount <-------n";
@alphat{ @alpha } = @ceaser = @ceaser[ 1 .. $#ceaser, 0 ];
}

__END__



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response



Stuart White
--- James Edward Gray II <[Email Removed]>
<snip>

QUOTE
And this one I'd think should be $3, and match
Steal
or Assist etc., but the results don't say that.

(?:Steal|Assist|Block|replaced by)

Stuart, I love your questions.  ;)  You're always
missing one tiny
piece of knowledge and you always ask them in such a
way that I know
exactly what it is.  Here's the missing piece this
time:

You see that the above is surrounded by ( ) and you
think that means it
should capture.  The truth is that the above is
surrounded by (?: ),
which happens to be ( )'s cousin.  It groups things
together, like ( ),
but it does not capture.  I had to cluster them so
the |s would work,
but I didn't need to hang onto the results, so I
chose (?: ) over ( ).

You could do it just fine with normal parenthesis,
if you prefer.  If
you do, you just have to remember that your third
answer is in $4,
because $3 is holding some junk.  Take your pick.


Geez, I can't recall them covering (?: ) in my
books...D'oh! The part about it grouping and
capturing things makes sense, as it's the "cousin" of
( ). The part about being able to include the |'s
doesn't. I found out, without knowing at the time,
that the parentheses breakdown with |'s. I didn't
know it at the time, but when I put the ORs in the
parentheses and ran the program, I just got the
command prompt, no output. Your explanation tells me
that (?: ) could capture the ORs, and implies that the
( ) could not. <--This part makes sense, as i'll just
regard it as a rule.
But then you go on to say that I could still use it
with ( ), but then $3 would contain junk and $4 would
contain the name after the
(?:Steal|Assist|Block|replaced by). I'm assuming that
junk in $3 would be either Assist or Block or Steal or
replaced by, is that correct? I ask this because
later, perhaps two days from now, perhaps two weeks
from now, I'm going to want that information, assuming
it is Assist or Block or Steal or replaced by.
Do I just put (:? ) within ( )? That sortof makes
sense, but it also seems, I'm not sure what the right
word is, but it doesn't seem right.

Lastly, I'm curious about this (:? ) operator. I'm
going to look it up, but assuming that perldoc is not
going to explain it sufficiently for me, as is often
the case, do you mind telling me why it is needed to
get the |'s, if that also applies to &, and numerical
and word comparison operators?
Thanks.

QUOTE
Instead, $3 is this, and matches the second name.
(w+)

If backreferences are supposed to be in
parentheses,
why isn't this
(?:Steal|Assist|Block|replaced by)
a backreference?

I'm hoping this makes sense now.  There are only
three captures in my
regex.  (w+) is the third.  Can you see that now?

It does, and I can.


Oh, and I appreciate when you not only answer a
question with an explanation, but you use an example
as well. That's extremely helpful. -stu

QUOTE
James



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

Stuart White
--- James Edward Gray II <[Email Removed]>
wrote:
QUOTE
On Mar 10, 2004, at 10:20 AM, Stuart White wrote:

Geez, I can't recall them covering (?: ) in my
books...D'oh!

It may not have.  It's not super common to see it
thrown about.  Most
people just use (...), I would guess.


Ahh, ok. So, like you said, (?: ) is just for
grouping things. I can see how that might be useful.
Thanks.


QUOTE
The part about it grouping and
capturing things makes sense, as it's the "cousin"
of
( ).  The part about being able to include the |'s
doesn't.  I found out, without knowing at the
time,
that the parentheses breakdown with |'s.  I didn't
know it at the time, but when I put the ORs in the
parentheses and ran the program, I just got the
command prompt, no output.

Hmm. this still sounds a little confused.

When I look at your regex, I think now that perhaps it
wasn't the ( ) that were written incorrectly by me,
but rather, my mistake in not accounting for the
digits in the brackets where the Team is, or the .+
instead of just the . in between $2 and $3. this
makes sense, because in Beginning Perl, it has quite a
few examples of | within ( ), which is why I didn't
think it'd be a problem in the first place.

Let's us
QUOTE
another example:


The second regex:

if (m/[([A-Z0-9 -]+)](w+).+
(Steal|Assist|Block|replaced by):? (w+)/)

is what I want. This example makes sense too.


QUOTE
Notice that they are nearly identical matches, I
just changed the
(?:...) to (...) in the second one.  They function
the same, the
variables set by the expression is the only
difference.  (?:...)
doesn't set a variable.


Got it.

QUOTE
Your other confusion seems to be the | character.
You seem to think
it's a Perl or symbol.  Not true.  We're inside a
regex here, gotta
switch thinking.  Regex knowledge in, Perl out.  |
is a regex
alternation character, which pretty much means find
this or this, as
expected.  That's probably why the symbol was
chosen, looks like the or
operators of many languages.  However, note that &
isn't significant in
a regex.


Yup, simple mistake. It's been awhile since I read
about | in regex, but I remember now that it is an
alternation character. I certainly did get confused
in my last post though. Thanks for the clarification.

QUOTE
Now, let's get to why | needs the (?:...) or (...)
around it.  If they
weren't there, my regex would read like this:


This part I understood. I was confused before because
I thought that (...) broke down when | was used, and
that to circumvent that, one would use (?:...)
instead.

QUOTE
You might take a trip back to the regex section of
your books, if | is
new to you.  It's regex 101 and I would be super
surprised if it isn't
covered.


It's covered. I'll be looking at that at lunchtime.
Thanks.

QUOTE
James



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com

James Edward Gray II
On Mar 9, 2004, at 9:00 PM, Stuart White wrote:

QUOTE
That got me started.  I do have a question though
about your regex.

Good, better to ask and know, I think. Let's see if we can clear it
up...

QUOTE
This backreference is $1, and matches the team
abbreviation.
([A-Z0-9 -]+)

This one is $2, and matches the first name:
(w+)

Full marks to here. You're right and right.

QUOTE
And this one I'd think should be $3, and match Steal
or Assist etc., but the results don't say that.

(?:Steal|Assist|Block|replaced by)

Stuart, I love your questions. ;) You're always missing one tiny
piece of knowledge and you always ask them in such a way that I know
exactly what it is. Here's the missing piece this time:

You see that the above is surrounded by ( ) and you think that means it
should capture. The truth is that the above is surrounded by (?: ),
which happens to be ( )'s cousin. It groups things together, like ( ),
but it does not capture. I had to cluster them so the |s would work,
but I didn't need to hang onto the results, so I chose (?: ) over ( ).

You could do it just fine with normal parenthesis, if you prefer. If
you do, you just have to remember that your third answer is in $4,
because $3 is holding some junk. Take your pick.

QUOTE
Instead, $3 is this, and matches the second name.
(w+)

If backreferences are supposed to be in parentheses,
why isn't this
(?:Steal|Assist|Block|replaced by)
a backreference?

I'm hoping this makes sense now. There are only three captures in my
regex. (w+) is the third. Can you see that now?

James

ccsh
During my various MTA and smtp research I have repeatedly seen two
questions:

How can I execute a script with Postfix and how can I copy/capture the
e-mail source: headers and body?

This script may prove helpful -
http://insecurity.org/blackhole/blackhole.sx

.... which is making the rounds into CPAN:
http://search.cpan.org/~sneex/


( NOTE: This wasnt posted to the
perl beginners e-mail list - but to the
Newsgroup: perl.beginners Usenet group itself. )


Enjoy!
-Bill-
__Sx__________________________________________
http://youve-reached-the.endoftheinternet.org/


PS - To all of you who offered me web space
in which to post my Perl mumblings - I
appreciate it -- but I have had a CPAN ID
now for years -- just never used it much...

Thx/Bill :)

James Edward Gray II
On Mar 10, 2004, at 10:20 AM, Stuart White wrote:

QUOTE
Geez, I can't recall them covering (?: ) in my
books...D'oh!

It may not have. It's not super common to see it thrown about. Most
people just use (...), I would guess.

QUOTE
The part about it grouping and
capturing things makes sense, as it's the "cousin" of
( ).  The part about being able to include the |'s
doesn't.  I found out, without knowing at the time,
that the parentheses breakdown with |'s.  I didn't
know it at the time, but when I put the ORs in the
parentheses and ran the program, I just got the
command prompt, no output.

Hmm. this still sounds a little confused. Let's us another example:

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
print "nLine: $_";
if (m/[([A-Z0-9 -]+)] (w+).+(?:Steal|Assist|Block|replaced by):?
(w+)/) {
print "tMatched: \[([A-Z0-9 -]+)\]
(\w+).+(?:Steal|Assist|Block|replaced by):? (\w+)n";
print "tt$1 is $1ntt$2 is $2ntt$3 is $3n";
}
if (m/[([A-Z0-9 -]+)] (w+).+(Steal|Assist|Block|replaced by):?
(w+)/) {
print "tMatched: \[([A-Z0-9 -]+)\]
(\w+).+(Steal|Assist|Block|replaced by):? (\w+)n";
print "tt$1 is $1ntt$2 is $2ntt$3 is $3ntt$4 is $4n";
}
}

__DATA__
(10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO) Steal: Jackson (1
ST)
(10:51) [SAN 4-0] Jackson Jump Shot: Made (2 PTS) Assist: Duncan (1 AST)
(9:33) [SAN] Duncan Layup Shot: Missed Block: Stoudemire (2 BLK)
(5:35) [SAN] Bowen Substitution replaced by Ginobili

When I run the above, I get:

Line: (10:18) [PHX] Stoudemire Turnover: Lost Ball (1 TO) Steal:
Jackson (1 ST)
Matched: [([A-Z0-9 -]+)] (w+).+(?:Steal|Assist|Block|replaced
by):? (w+)
$1 is PHX
$2 is Stoudemire
$3 is Jackson
Matched: [([A-Z0-9 -]+)] (w+).+(Steal|Assist|Block|replaced by):?
(w+)
$1 is PHX
$2 is Stoudemire
$3 is Steal
$4 is Jackson

Line: (10:51) [SAN 4-0] Jackson Jump Shot: Made (2 PTS) Assist: Duncan
(1 AST)
Matched: [([A-Z0-9 -]+)] (w+).+(?:Steal|Assist|Block|replaced
by):? (w+)
$1 is SAN 4-0
$2 is Jackson
$3 is Duncan
Matched: [([A-Z0-9 -]+)] (w+).+(Steal|Assist|Block|replaced by):?
(w+)
$1 is SAN 4-0
$2 is Jackson
$3 is Assist
$4 is Duncan

Line: (9:33) [SAN] Duncan Layup Shot: Missed Block: Stoudemire (2 BLK)
Matched: [([A-Z0-9 -]+)] (w+).+(?:Steal|Assist|Block|replaced
by):? (w+)
$1 is SAN
$2 is Duncan
$3 is Stoudemire
Matched: [([A-Z0-9 -]+)] (w+).+(Steal|Assist|Block|replaced by):?
(w+)
$1 is SAN
$2 is Duncan
$3 is Block
$4 is Stoudemire

Line: (5:35) [SAN] Bowen Substitution replaced by Ginobili
Matched: [([A-Z0-9 -]+)] (w+).+(?:Steal|Assist|Block|replaced
by):? (w+)
$1 is SAN
$2 is Bowen
$3 is Ginobili
Matched: [([A-Z0-9 -]+)] (w+).+(Steal|Assist|Block|replaced by):?
(w+)
$1 is SAN
$2 is Bowen
$3 is replaced by
$4 is Ginobili

Notice that they are nearly identical matches, I just changed the
(?:...) to (...) in the second one. They function the same, the
variables set by the expression is the only difference. (?:...)
doesn't set a variable.

Your other confusion seems to be the | character. You seem to think
it's a Perl or symbol. Not true. We're inside a regex here, gotta
switch thinking. Regex knowledge in, Perl out. | is a regex
alternation character, which pretty much means find this or this, as
expected. That's probably why the symbol was chosen, looks like the or
operators of many languages. However, note that & isn't significant in
a regex.

Now, let's get to why | needs the (?:...) or (...) around it. If they
weren't there, my regex would read like this:

Find
[([A-Z0-9 -]+)] (w+).+Steal
OR
Assist
OR
Block
OR
replaced by:? (w+)

Instead, it reads like this:

Find
[([A-Z0-9 -]+)] (w+).+
Followed By
Steal
OR
Assist
OR
Block
OR
replaced by
Followed By
:? (w+)

As you can see, I need the parenthesis to keep the oring behavior of |
from going to far.

Hopefully that makes sense.

You might take a trip back to the regex section of your books, if | is
new to you. It's regex 101 and I would be super surprised if it isn't
covered.

James


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.