[22901] in Perl-Users-Digest
Perl-Users Digest, Issue: 5121 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 14 14:05:42 2003
Date: Sat, 14 Jun 2003 11:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 14 Jun 2003 Volume: 10 Number: 5121
Today's topics:
ANNOUNCE: Time::Format 0.04 <sdn@comcast.net>
Re: die and exception over-stringification (Jay Tilton)
Re: get data on socket problem <dont@want.spam>
list commands query <notspam@spamfree.dud>
Re: list commands query <jurgenex@hotmail.com>
Re: list commands query <krahnj@acm.org>
Re: list commands query <ndronen@io.frii.com>
Re: Mail::POP3Client - Login Issue <soneill@ciktech.com>
Re: Mail::POP3Client - Login Issue <noreply@gunnar.cc>
Re: Mail::POP3Client - Login Issue <soneill@ciktech.com>
Re: Mail::POP3Client - Login Issue <noreply@gunnar.cc>
Re: Mail::POP3Client - Login Issue <soneill@ciktech.com>
Re: Newline and here-doc <m.theiss@web.de>
Re: Newline and here-doc (Tad McClellan)
Perl to PHP translator <darnold@presicient.com>
Re: regexp challenge <dthomson@users.sf.net>
Re: regexp challenge (Tad McClellan)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 14 Jun 2003 11:05:19 -0500
From: "Eric J. Roode" <sdn@comcast.net>
Subject: ANNOUNCE: Time::Format 0.04
Message-Id: <1o1sr-m7h.ln1@red.stonehenge.com>
*** post for FREE via your newsreader at post.newsfeed.com ***
Announcing an update to perl module Time::Format. This version includes
internationalization features -- if I18N::Langinfo is installed and the
locale is properly set, month names and weekday names will be displayed
by %time in a language appropriate for the current locale.
Now available on CPAN.
>From the README:
=====================
Time::Format provides a very easy way to format dates and times. The
formatting functions are tied to hash variables, so they can be used
inside strings as well as in ordinary expressions. The formatting
codes used are meant to be easy to remember, use, and read.
Also provided is a tied-hash interface to POSIX::strftime and
Date::Manip::UnixDate.
If the I18N::Langinfo module is available, Time::Format provides
weekday and month names in a language appropriate for your locale.
EXAMPLES
$time{'Weekday Month d, yyyy'} Thursday June 5, 2003
$time{'Day Mon d, yyyy'} Thu Jun 5, 2003
$time{'DAY MON d, yyyy'} THU JUN 5, 2003
$time{'dd/mm/yyyy'} 05/06/2003
$time{yymd} 030605
$time{'H:mm:ss am'} 1:02:14 pm
$time{'hh:mm:ss.uuuuuu'} 13:02:14.171447
$time{'yyyy/mm/dd hh:mm:ss.mmm'} 2003/06/05 13:02:14.171
$strftime{'%A %B %e, %Y'} Thursday June 5, 2003
$manip{'%m/%d/%Y'} 06/05/2003
$manip{'%m/%d/%Y','yesterday'} 06/04/2003
$manip{'%m/%d/%Y','first monday in November 2000'} 11/06/2000
There are also corresponding functions for each of these hashes,
which you can use if you prefer a function-based interface.
DEVELOPMENT STATE
Time::Format is a brand new module. It has a decent test suite, but
it hasn't been used much in the Real World yet. Thus it should be
considered "beta" software. When six months pass without any bugs being
reported, or any features being added, I'll bump the version to 1.0.
--
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
------------------------------
Date: Sat, 14 Jun 2003 15:36:38 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: die and exception over-stringification
Message-Id: <3eeb380b.236006476@news.erols.com>
Joey Hess <joey@kitenet.net> wrote:
: I have an exception class that overloads stringify and when stringified
: has to do something expensive. I had hoped that moving the complex
: claculation to happen when it's stringified would avoid it entirely in
: cases where the exception is caught and thrown away. Instead it seems
: that die always stringifies it, and sometimes it will even get
: stringified twice.
:
: The code below illistrates the problem. The first exception is caught
: by the eval, but has already been stringified once at that point. The
: second exception actually gets stringified twice, once apparently when
: die first throws it, and once when perl stringifies it to print it as it
: dies of it.
:
: print "catch exception and throw it away\n";
: eval {
: die My::Exception->new();
: };
:
: print "die of exception\n";
: die My::Exception->new;
:
: package My::Exception;
: my $counter=0;
: sub new {
: my $c=++$counter;
: return bless(\$c, shift);
: }
: use overload '""' => sub {
: my $this=shift;
: print "expensive operation here (for exception #".$$this.")\n";
: sleep 2;
: return "stringification of exception #".$$this;
: }
:
: This takes 6+ seconds to run, not just 2, and prints:
:
: catch exception and throw it away
: expensive operation here (for exception #1)
: die of exception
: expensive operation here (for exception #2)
: expensive operation here (for exception #2)
: stringification of exception #2 at test line 7.
Freaky.
Dunno what version you're running, but I don't see that second
"expensive operation here (for exception #2)"
line on Perl 5.8. No matter.
For debugging, you might use Carp::cluck() to trace the call stack and
determine with more certainty where the extra stringifying operation
is happening.
use Carp qw(cluck);
use overload '""' => sub {
cluck("I'm getting strung out!");
# rest of method
}
If die() really is double-stringifying its argument, you could head it
off and stringify the argument before die() ever gets a chance to
touch it.
die( ''. My::Exception->new );
Lastly, and I don't know how well this may apply to your program,
consider rolling the expensive operation off into a subroutine that
memoizes itself, so it's only expensive the first time it happens.
Maybe even make the stringing method memoize the object's stringy form
within the object itself.
use overload '""' => sub {
my $this=shift;
unless( $this->been_strung ) {
# expensive operation goes here
$this->been_strung(
"stringification of exception #".$$this
);
}
return $this->been_strung;
}
------------------------------
Date: Sat, 14 Jun 2003 17:29:35 +0100
From: Chris Lowth <dont@want.spam>
Subject: Re: get data on socket problem
Message-Id: <P3IGa.3345$LP.1261@newsfep4-winn.server.ntli.net>
rdack wrote:
> i send a binary file on a socket and the client sends all bytes fine,
> no error, but the server doesn't get the whole file. what could be
> going wrong?
>
> this is the server side code for getting the data:
>
> $res = 0;
> $chnk=32768;
> # $len comes from client and is correct (530664 bytes)
>
> while($res < $len)
> {
> print "$res ";
> $chnk = $len - $res if ($res + $chnk) > $len;
> print $chnk." ";
> $rr = sysread $sock, $data, $chnk, $res;
> print $rr."\n";
> die "failed to get data (got $res bytes): $!\n" if !defined
> $rr;
> last if $rr == 0;
> $res += $rr;
> }
>
> i can get between 480k+ and 516k+ bytes. and i see not every read gets
> a full chunk asked for.
> then hangs on last read when client has sent all bytes, but server
> seems to have lost some bytes (12k - 40k) and thinks there should be
> more coming.
>
> does sysread not buffer? should client send in smaller/larger chunks
> than server gets?
> is there some canned code for a simple data xfer between sockets?
>
> rdack
Can we see the sender code too?
--
Real address: chris at lowth dot sea oh em.
World's first wrist-watch PDA with Palm OS, available June 30
from Amazon.com. Order now to beat the rush!
http://www.lowth.com/shop/wrist_pda
------------------------------
Date: Sat, 14 Jun 2003 13:53:06 GMT
From: Sean O'Dwyer <notspam@spamfree.dud>
Subject: list commands query
Message-Id: <notspam-A3A043.09530614062003@syrcnyrdrs-03-ge0.nyroc.rr.com>
In " Perl for Dummies" Paul E. Hoffman writes...
* shift adds to the left side of a list
* unshift removes from the left side of the list
* push adds to the right side of a list
* pop removes from the right side of the list
...but he also states...
* push adds one or more elements to the end of the list,
* while unshift adds elements to the beginning of the list.
I just want confirmation that the latter statement is false, and that
pop/push affect the end/right of the list and shift/unshift affect the
beginning/left.
Thanks,
Sean
------------------------------
Date: Sat, 14 Jun 2003 14:06:58 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: list commands query
Message-Id: <6_FGa.13499$Bw2.7836@nwrddc03.gnilink.net>
Sean O'Dwyer wrote:
> In " Perl for Dummies" Paul E. Hoffman writes...
>
> * shift adds to the left side of a list
> * unshift removes from the left side of the list
> * push adds to the right side of a list
> * pop removes from the right side of the list
>
> ...but he also states...
>
> * push adds one or more elements to the end of the list,
> * while unshift adds elements to the beginning of the list.
>
> I just want confirmation that the latter statement is false, and that
> pop/push affect the end/right of the list and shift/unshift affect the
> beginning/left.
Why don't you simply check The Fine Manual:
perldoc -f push:
push ARRAY,LIST
Treats ARRAY as a stack, and pushes the values of LIST onto the
end of ARRAY. [...]
perldoc -f unshift:
unshift ARRAY,LIST
Does the opposite of a "shift". Or the opposite of a "push",
depending on how you look at it. Prepends list to the front of
the array, and [...]
For questions like those you should _always_ check the manual. It is the
ultimate source of information (beside the source code of perl, of course).
jue
------------------------------
Date: Sat, 14 Jun 2003 14:07:50 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: list commands query
Message-Id: <3EEB2C30.323240FF@acm.org>
Sean O'Dwyer wrote:
>
> In " Perl for Dummies" Paul E. Hoffman writes...
>
> * shift adds to the left side of a list
^^^^^^^ ^^^^
removes from array
> * unshift removes from the left side of the list
^^^^^^^^^^^^ ^^^^
adds to array
> * push adds to the right side of a list
^^^^
array
> * pop removes from the right side of the list
^^^^
array
> ...but he also states...
>
> * push adds one or more elements to the end of the list,
^^^^
array
> * while unshift adds elements to the beginning of the list.
^^^^
array
> I just want confirmation that the latter statement is false, and that
> pop/push affect the end/right of the list and shift/unshift affect the
> beginning/left.
Yes, that is correct, however the use of "list" is incorrect as push,
pop, shift, unshift and splice will only work on arrays not lists.
perldoc -q "What is the difference between a list and an array"
John
--
use Perl;
program
fulfillment
------------------------------
Date: 14 Jun 2003 14:37:29 GMT
From: Nicholas Dronen <ndronen@io.frii.com>
Subject: Re: list commands query
Message-Id: <3eeb3329$0$202$75868355@news.frii.net>
Sean O'Dwyer <notspam@spamfree.dud> wrote:
SOD> In " Perl for Dummies" Paul E. Hoffman writes...
SOD> * shift adds to the left side of a list
"shift" removes from the head of an array. See perldoc -f shift
$ perl -wle '@foo = qw(a b); shift @foo; print "@foo"'
b
SOD> * unshift removes from the left side of the list
"unshift" prepends. perldoc -f unshift
$ perl -wle '@foo = qw(a b); unshift @foo, "left or right?"; print "@foo"'
left or right? a b
SOD> * push adds to the right side of a list
SOD> * pop removes from the right side of the list
These definitions are mostly right, but since the prototypes of
push and pop are:
push ARRAY,LIST
pop ARRAY
he should substitute "array" for "list."
SOD> ...but he also states...
SOD> * push adds one or more elements to the end of the list,
SOD> * while unshift adds elements to the beginning of the list.
Again, substitute "array" for "list."
SOD> I just want confirmation that the latter statement is false, and that
SOD> pop/push affect the end/right of the list and shift/unshift affect the
SOD> beginning/left.
There's a little bit of falsity in all of these definitions. Start
with the perldoc. It's more authoritative than the guy who wrote
_Perl for Dummies_.
Regards,
Nicholas
--
"Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
"Meanings are another story." http://www.ifas.org/wa/glossolalia.html
------------------------------
Date: Sat, 14 Jun 2003 10:17:56 -0400
From: "SeanCIK" <soneill@ciktech.com>
Subject: Re: Mail::POP3Client - Login Issue
Message-Id: <3eeb2fd7_6@corp.newsgroups.com>
Thanks, Gunnar. That solved the problem of the "password, please try again"
error (can you explain why that worked?), but now I'm getting this:
you have -1 new messages.
I've had this "-1" problem before. I know there are two new messages on the
server. Any ideas as to what's going on?
Thanks, again.
Sean
"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:bcet7q$i9jn2$1@ID-184292.news.dfncis.de...
> SeanCIK wrote:
> >
> > #!/usr/bin/perl
> > use Mail::POP3Client;
> > $pop2 = new Mail::POP3Client( USER => "myusername", PASSWORD =>
> > "mypassword", HOST =>
> > "my.hosting.server", PORT => 110, DEBUG => 0, AUTH_MODE => "PASS",
TIMEOUT
> > => 60 );
> > $pop2->Connect() || die $pop2->Message();
> > print "you have ", $pop2->Count, " new messages.\n";
> > $pop2->Close();
> >
> > Here is the error code I get in return:
> > at pop3test.pl line 4, <GEN1> line 3.password, please try again
> >
> > I have checked the password and it is entered correctly. Any ideas
what's
> > causing this?
>
> I'd try it without the line:
>
> $pop2->Connect() || die $pop2->Message();
>
> / Gunnar
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: Sat, 14 Jun 2003 17:09:43 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Mail::POP3Client - Login Issue
Message-Id: <bcfdu1$ip3mj$1@ID-184292.news.dfncis.de>
Please don't top post! See the reasons for it at
http://web.presby.edu/~nnqadmin/nnq/nquote.html
SeanCIK wrote:
> Thanks, Gunnar. That solved the problem of the "password, please
> try again" error (can you explain why that worked?),
The first "Synopsis" example is simpler than your code:
http://search.cpan.org/author/SDOWD/Mail-POP3Client-2.14/POP3Client.pm
I have no other explanation.
> but now I'm getting this: you have -1 new messages.
>
> I've had this "-1" problem before. I know there are two new
> messages on the server. Any ideas as to what's going on?
No. I'm not using Mail::POP3Client myself; I'm using Net::POP3. This
is a Net::POP3 equivalent to your code:
#!/usr/bin/perl
use strict;
use warnings;
use Net::POP3;
my $pop = Net::POP3->new('my.hosting.server');
my $cnt = $pop->login('myusername', 'mypassword');
print "you have $cnt new messages.\n";
$pop->quit();
You can always try that. Note, though, that if you want to do
something else but counting the messages, you need to understand
references to make use of Net::POP3. I get the impression that
Mail::POP3Client doesn't require knowledge of Perl references.
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 14 Jun 2003 11:27:46 -0400
From: "SeanCIK" <soneill@ciktech.com>
Subject: Re: Mail::POP3Client - Login Issue
Message-Id: <3eeb4036_1@corp.newsgroups.com>
>
> The first "Synopsis" example is simpler than your code:
> http://search.cpan.org/author/SDOWD/Mail-POP3Client-2.14/POP3Client.pm
>
> I have no other explanation.
>
> > but now I'm getting this: you have -1 new messages.
> >
> > I've had this "-1" problem before. I know there are two new
> > messages on the server. Any ideas as to what's going on?
>
> No. I'm not using Mail::POP3Client myself; I'm using Net::POP3. This
> is a Net::POP3 equivalent to your code:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Net::POP3;
> my $pop = Net::POP3->new('my.hosting.server');
> my $cnt = $pop->login('myusername', 'mypassword');
> print "you have $cnt new messages.\n";
> $pop->quit();
>
> You can always try that. Note, though, that if you want to do
> something else but counting the messages, you need to understand
> references to make use of Net::POP3. I get the impression that
> Mail::POP3Client doesn't require knowledge of Perl references.
>
> / Gunnar
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
Gunnar,
Thanks for Net::POP3 sample (and sorry for the top post). I ran that and
now I seem to be getting no count of new messages (and I've double-checked,
I still have two new msgs on the server). Here's the results:
Use of uninitialized value in concatenation (.) or string at pop3test2.pl
line 7.
you have new messages.
I've had this same problem with other Net::POP3 scripts I've tried.
Sean
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: Sat, 14 Jun 2003 17:58:43 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Mail::POP3Client - Login Issue
Message-Id: <bcfgpv$il0kq$1@ID-184292.news.dfncis.de>
SeanCIK wrote:
> Gunnar Hjalmarsson wrote:
>> SeanCIK wrote:
>>>
>>> but now I'm getting this: you have -1 new messages.
>>>
>>> I've had this "-1" problem before. I know there are two new
>>> messages on the server. Any ideas as to what's going on?
>>
>> No.
It struck me that it's a good idea to check the docs... ;-)
"To test for a connection failure, you will need to check the number
of messages: -1 indicates a connection error."
In other words, the -1 tells you that the connection failed.
> Thanks for Net::POP3 sample (and sorry for the top post). I ran
> that and now I seem to be getting no count of new messages (and
> I've double-checked, I still have two new msgs on the server).
> Here's the results:
>
> Use of uninitialized value in concatenation (.) or string at
> pop3test2.pl line 7. you have new messages.
>
> I've had this same problem with other Net::POP3 scripts I've tried.
Same explanation, i.e. according to the docs for Net::POP3, an undef
value from the login method indicates an authenticating error.
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 14 Jun 2003 12:22:37 -0400
From: "SeanCIK" <soneill@ciktech.com>
Subject: Re: Mail::POP3Client - Login Issue
Message-Id: <3eeb4d11_1@corp.newsgroups.com>
> "To test for a connection failure, you will need to check the number
> of messages: -1 indicates a connection error."
>
> In other words, the -1 tells you that the connection failed.
Thanks, Gunnar. That was the key piece. I didn't know that -1 indicated a
connection failure. Turns out, my host was incorrect and I checked with my
ISP for the appropriate POP3 server. Got the right host and connection
successful!
Much appreciated,
Sean
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: Sat, 14 Jun 2003 15:59:44 +0200
From: "Martin Theiß" <m.theiss@web.de>
Subject: Re: Newline and here-doc
Message-Id: <bcf9ok$mc0$1@news.uni-stuttgart.de>
Thx for your advice
Martin
"Big and Blue" <No_4@dsl.pipex.com> schrieb im Newsbeitrag
news:3eea5c37$0$18493$cc9e4d1f@news.dial.pipex.com...
> Big and Blue wrote:
> >
> > but I suspect that your here document will be longer in practice so try
> > this.
>
> or, making the variable localized only to the print statement:
>
> print do {chomp (my $text = <<EOF);
> Hello
> EOF
> $text}, " plus more...\n";
>
>
> --
> -*- Just because I've written it here doesn't -*-
> -*- mean that you should, or I do, believe it. -*-
>
------------------------------
Date: Sat, 14 Jun 2003 10:26:40 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Newline and here-doc
Message-Id: <slrnbemflg.7sm.tadmc@magna.augustmail.com>
Martin Theiß <m.theiss@web.de> wrote:
> I've got a problem with a here-doc statement.
here-docs are just a way of quoting strings, consider some other
way of quoting strings when you don't like how here-docs do it.
> I tried this:
>
> print <<EOF
> Hello
> EOF
> ;
>
> As a Result "Hello" ist printed with a leading
I'm having trouble believing that part.
There is no leading newline when I tried it...
> and a trailing newline, but i
> don't want to have a trailing newline. Does anybody know a workaround (maybe
> a regex) for this problem.
Use a different form of quoting:
print 'Hello';
Perhaps your example code is rather too simple for what you meant
to ask? Do you need interpolation?
-------------
print <<EOF;
stuff with a $variable in it
EOF
-------------
Then without a trailing newline:
print "stuff with a $variable in it";
Maybe you have lots of lines and interpolation?
-------------
print <<EOF;
stuff with a $variable in it
more stuff
yet more $variable to interpolate
EOF
-------------
Then without a trailing newline:
-------------
print "stuff with a $variable in it
more stuff
yet more $variable to interpolate";
-------------
Or, maybe lots of lines, and interpolation, and some double quotes
that you don't want to have to escape?
-------------
print <<EOF;
stuff with a $variable in it
He said "Perl rocks"!
yet more $variable to interpolate
EOF
-------------
Then without a trailing newline:
-------------
print qq(stuff with a $variable in it
He said "Perl rocks"!
yet more $variable to interpolate);
-------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 14 Jun 2003 16:45:18 GMT
From: "Dean Arnold" <darnold@presicient.com>
Subject: Perl to PHP translator
Message-Id: <yiIGa.2725$z82.1822@newssvr19.news.prodigy.com>
Does one exist, and if so, where can I lay hands on it?
I've got some users interested in PHP versions of some
of my Perl drivers (DBD::Chart, DBD::Teradata), but
I'm clueless wrt PHP. Tried Google & SourceForge, but couldn't find
anything.
TIA
Dean Arnold
Presicient Corp.
www.presicient.com
------------------------------
Date: Sat, 14 Jun 2003 23:41:08 +1000
From: Derek Thomson <dthomson@users.sf.net>
Subject: Re: regexp challenge
Message-Id: <3eeb25e0$1@duster.adelaide.on.net>
Hi Bob,
Bob Gotchall wrote:
> Folks.
>
> I think I have discovered a pattern that is really easy to see by the
> human eye, but doesn't seem to have an elegant perl solution. I have
> data (it's test vector data from a Teradyne Catalyst, if that means
> anything to you)
Not to me, what is it? I will resist the urge to google for it until you
reply ;)
> that looks like this:
>
> opcode arg opcode opcode opcode arg X XX 10101 LHHL LHL 10 LLH
> HLLH11010110 XX
> opcode opcode opcode arg X 1X 10101 LHHL LHL -- LLH
> HLLH11011110 XX
> opcode arg opcode opcode opcode arg X XX 10101 LLLL HHL -- LLH
> HLLH01010110 XX
> opcode arg opcode opcode opcode 1 X0 10101 LHHL LHL -1 LLH
> HLLH11110111 XX
> opcode arg opcode X XX 10101 HHHH LHH -- LLH
> HLLH11010010 XX
> opcode arg opcode opcode opcode arg X XX 10101 LHHL LHL -- LLH
> HLLH01010010 XX
>
> Where an "opcode" is a word like JUMP, REPEAT, LABEL and an "arg" is
> any old alphanumeric. The vector field is all [10HLX-]. and the
> width and composition of the file varies with file, of course. There
> is no column number assumptions that will work.
>
> So I want to do something like this:
> $line =~ / (stuff thats opcode words)\s+(stuff that's vector words)/;
> $opcodes=$1; $vectors=$2;
>
>
> Thoughts? Is it impossible?
If "arg" can be *any* alphanumeric there is no way to tell where the
args end and the "vector fields" start.
There are two possible escape hatches that I can see.
1) if "arg" is really nothing like the vector fields. How about some
real examples?
2) In the examples you provide, the vector field has the same structure
on each line. If you assume that this is always the case, we use that
fact to split the line, like this:
#!/usr/bin/env perl
use warnings;
use strict;
my %field;
my $field = qr/[10HLX-]/;
my $vector_field = qr/$field $field{2} $field{5} $field{4} $field{3}
$field{2} $field{15} $field{2}/;
my $opcode = qr/JUMP|REPEAT|LABEL/;
# my $opcode = qr/JUMP/;
my $arg = qr/[a-zA-Z0-9]/;
my $vector_data = qr/$opcode ($opcode|$arg)*\s+$vector_field/;
while (<>) {
print if m/$vector_data$/;
}
I've split the regex up to make it more understandable, and I've left it
to you to arrange the capturing braces. I've also only allowed a
variable amount of whitespace between the (opcode|arg) part and the
vector fields - you may want to allow any amount of white space
*everywhere*.
I suspect there may be pathological cases where this is a bit slow, as
the regex engine will have to backtrack to work out if the last arg is
really a part of the vector field or not. However, my failure cases
seemed to work ok. Perhaps reversing the string and matching it with the
same regex but expressed backwards would help avoid this?
Also, if the vector fields really are rigid, right down to spacing, you
could just split the string before you do any other processing:
use constant VECTOR_FIELDS_LENGTH => 41;
...
$first_part = substr($line, 0, length($line)-VECTOR_FIELDS_LENGTH-1);
$second_part = substr($line, length($line)-VECTOR_FIELDS_LENGTH)
...
Then, it's much easier to split the first and second parts into their
components with regexes!
Derek.
------------------------------
Date: Sat, 14 Jun 2003 09:55:17 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: regexp challenge
Message-Id: <slrnbemdql.7q0.tadmc@magna.augustmail.com>
James E Keenan <jkeen@concentric.net> wrote:
> "Bob Gotchall" <rgotchall@austin.rr.com> wrote in message
> news:488df089.0306131427.40f619ce@posting.google.com...
>> The vector field is all [10HLX-].
> 1. Your character class should probably include the wordspace: [10HLX- ]
But not like that. Like this:
[10HLX -]
Hyphen in a char class forms a range, unless the hyphen is the
first or last character (or if it is backslashed).
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 5121
***************************************