[28931] in Perl-Users-Digest
Perl-Users Digest, Issue: 175 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 27 00:34:38 2007
Date: Mon, 26 Feb 2007 21:34:31 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 26 Feb 2007 Volume: 11 Number: 175
Today's topics:
Question about News::NNTPClient <itfred@cdw.com>
Re: Question about News::NNTPClient <nobull67@gmail.com>
Reading Directory Mystery ChristopherSimone@gmail.com
Re: Reading Directory Mystery ChristopherSimone@gmail.com
Re: Reading Directory Mystery <joe@inwap.com>
Reading in data until I have a full structure <pwaring@gmail.com>
Re: Reading in data until I have a full structure <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Reading in data until I have a full structure <1usa@llenroc.ude.invalid>
Re: Reading in data until I have a full structure <pwaring@gmail.com>
Re: Reading in data until I have a full structure <1usa@llenroc.ude.invalid>
Re: sorting an array with associated values in separate <njus@larshaugseth.com>
Thread issue (The Doctor)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 25 Feb 2007 13:43:49 -0500
From: Fred <itfred@cdw.com>
Subject: Question about News::NNTPClient
Message-Id: <X4-dnVITX6t4RXzYnZ2dnUVZ_tmknZ2d@comcast.com>
The script below lists ALL news groups for a particular news
server using the News::NNTPClient module. The list returned
looks similar to this with the group name, first article#,
last article#, and 'y' or 'n' if posting is allowed:
alt.test 0003911619 0003889349 y
alt.test-ns 0000006714 0000003335 y
alt.test.9 0000004684 0000002874 y
alt.test.a 0000057454 0000042977 y
The list is printed using "print $c->list()" (See below). I'm
trying to parse just the newsgroup name, so the list above
would look like:
alt.test
alt.test-ns
alt.test.9
I tried unsuccessfully using split like this:
my ($grpname, $first, $last, $postingallowed) = split(/ /, print
$c->list('active', 'alt.test*'));
print "$grpname\n";
I know I can parse the output and truncate 3 spaces to the right,
but is there a way to do this witrhin the script?
-Thanks
#!/usr/bin/perl -w
use strict;
use News::NNTPClient;
my $newshost = 'news.sprynet.com';
my $username = 'myaddy@comcast.net';
my $password = 'mypass';
my $port = '119';
# Connect to news server on port 119, debug level 1.
my $c = new News::NNTPClient($newshost, $port, 1);
unless ($nc->ok()) {
$nc->quit();
die $nc->message();
}
$c -> authinfo($username,$password);
print $c->list();
$c->quit();
------------------------------
Date: 25 Feb 2007 11:13:04 -0800
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Question about News::NNTPClient
Message-Id: <1172430784.395857.157760@j27g2000cwj.googlegroups.com>
On Feb 25, 6:43 pm, Fred <itf...@cdw.com> wrote:
> Subject: Question about News::NNTPClient
Are you sure your question is about News::NNTPClient?
What makes you think that?
Don't fall into the trap of imagining that just because you happen to
be using News::NNTPClient at the time you encountered the question
does not mean it's a question about News::NNTPClient.
The only question you could be asking about News::NNTPClient is "what
does the list() method return" and that you either know or could
trivially look up.
Once you know the format how to parse it is not a question about
News::NNTPClient.
> The script below lists ALL news groups for a particular news
> server using the News::NNTPClient module. The list returned
> looks similar to this with the group name, first article#,
> last article#, and 'y' or 'n' if posting is allowed:
>
> alt.test 0003911619 0003889349 y
> alt.test-ns 0000006714 0000003335 y
> alt.test.9 0000004684 0000002874 y
> alt.test.a 0000057454 0000042977 y
>
> The list is printed using "print $c->list()" (See below).
Right, that _prints_ the list that is returned by the list() method.
> I'm
> trying to parse just the newsgroup name, so the list above
> would look like:
>
> alt.test
> alt.test-ns
> alt.test.9
>
> I tried unsuccessfully using split like this:
>
> my ($grpname, $first, $last, $postingallowed) = split(/ /, print
> $c->list('active', 'alt.test*'));
What to you think is the purpose of the print() in there?
Anyhow, there's one group name _per_line_ of the data returned by
list(). The above code is only trying to parse a single record. That's
gotta be wrong! You obviously need some sort of loop to perform some
action _for_each_line returned by list().
foreach( $c->list('active', 'alt.test*') ) {
my ($grpname, $first, $last, $postingallowed) = split;
# do whatever
}
------------------------------
Date: 26 Feb 2007 09:55:53 -0800
From: ChristopherSimone@gmail.com
Subject: Reading Directory Mystery
Message-Id: <1172512553.408446.241760@t69g2000cwt.googlegroups.com>
I am working on a hashing program that reads a .ini file and creates a
md5hash of any directory listed within. I am checking properly if the
file still exists and checks the hash and if a new file has been
created and there is no saved hash. What I am trying to fix now is if
I have a stored hash of a file and that file is no longer in the place
it should. Listed below is my code and what I am seeing.
sub missing
{
print "ok1\n";
my $newOD;
open $newOD, $digestdir or die "Cannot open digestdir: $!\n";
#digestdir is where the md5 digest files are
#stored
print "ok2\n";
while( defined( my $x = readdir $newOD ) )
{
print "inside";
my $count = 0;
if( -f "$digestdir/$x" )
{
print "$x\n";
# %hashed is a hash of all my md5s. It is populated with file/hash
pairs from the original directory.
# If I have a file in my digest directory that matches up with one of
the pairs then the program will not worry about
# it. If it doesn't find a match that means a file that had a hash no
longer exists.
foreach my $j (keys %hashed)
{
if( $j eq "$digestdir/$x" )
{
$count++;
}
}
}
if( $count == 0 )
{
print "This file no longer exists: $x\n";
}
}
print "outside\n";
close $newOD;
}
When I run the program I get the following output:
ok1
ok2
outside
The program seems to skip the while loop all together (I am guessing
that my condition is not defined), but I use the exact same code in a
similar manner in another section that seems to work fine. Maybe it
has something to do with the scope of the directory handle, but I made
sure that it was local to the sub and not used elsewhere in the
program. Any advice?
------------------------------
Date: 26 Feb 2007 10:12:48 -0800
From: ChristopherSimone@gmail.com
Subject: Re: Reading Directory Mystery
Message-Id: <1172513568.791756.28610@z35g2000cwz.googlegroups.com>
Figured it out. I was using open $OD instead of opendir $OD.
On Feb 26, 12:55 pm, ChristopherSim...@gmail.com wrote:
> I am working on a hashing program that reads a .ini file and creates a
> md5hash of any directory listed within. I am checking properly if the
> file still exists and checks the hash and if a new file has been
> created and there is no saved hash. What I am trying to fix now is if
> I have a stored hash of a file and that file is no longer in the place
> it should. Listed below is my code and what I am seeing.
>
> sub missing
> {
> print "ok1\n";
> my $newOD;
> open $newOD, $digestdir or die "Cannot open digestdir: $!\n";
> #digestdir is where the md5 digest files are
>
> #stored
> print "ok2\n";
>
> while( defined( my $x = readdir $newOD ) )
> {
> print "inside";
> my $count = 0;
>
> if( -f "$digestdir/$x" )
> {
> print "$x\n";
> # %hashed is a hash of all my md5s. It is populated with file/hash
> pairs from the original directory.
> # If I have a file in my digest directory that matches up with one of
> the pairs then the program will not worry about
> # it. If it doesn't find a match that means a file that had a hash no
> longer exists.
> foreach my $j (keys %hashed)
> {
> if( $j eq "$digestdir/$x" )
> {
> $count++;
> }
> }
> }
>
> if( $count == 0 )
> {
> print "This file no longer exists: $x\n";
> }
> }
> print "outside\n";
> close $newOD;
>
> }
>
> When I run the program I get the following output:
> ok1
> ok2
> outside
>
> The program seems to skip the while loop all together (I am guessing
> that my condition is not defined), but I use the exact same code in a
> similar manner in another section that seems to work fine. Maybe it
> has something to do with the scope of the directory handle, but I made
> sure that it was local to the sub and not used elsewhere in the
> program. Any advice?
------------------------------
Date: Mon, 26 Feb 2007 13:11:17 -0800
From: Joe Smith <joe@inwap.com>
Subject: Re: Reading Directory Mystery
Message-Id: <uqSdndhUYLFh0X7YnZ2dnUVZ_vDinZ2d@comcast.com>
ChristopherSimone@gmail.com wrote:
> foreach my $j (keys %hashed)
> {
> if( $j eq "$digestdir/$x" )
> {
> $count++;
> }
> }
That's dumb. There is no reason to loop over all the keys in
a hash just to check if a given key is being used.
Just access the hash like it is supposed to be accessed.
$count++ if exists %hashed{"$digestdir/$x"};
-Joe
------------------------------
Date: 25 Feb 2007 10:55:32 -0800
From: "pwaring@gmail.com" <pwaring@gmail.com>
Subject: Reading in data until I have a full structure
Message-Id: <1172429732.393516.115680@j27g2000cwj.googlegroups.com>
I've got a text file which is full of questions in a format similar to
the following:
QUESTION_ID "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
...
ANSWER_N)
At the moment I can parse each individual question into its component
parts without any problems (it's not the most pleasant regex in the
world, but it works), however I'm having trouble turning the whole
file into an array of questions which I can then parse individually.
Each question is separated from the next by at least two newlines, but
unfortunately there is sometimes two newlines between SHORT_QUESTION
and (ANSWER_1, so I can't assume that two newlines indicate the end of
a question, which is what I've been doing so far.
I was wondering if anyone could point me in the right direction for a
way to get around this problem - basically I need to read in data
until I know I've got a full question with answers (assuming this ends
at two newlines often means I get the answers separately, which causes
problems when I try to split this into smaller parts), parse that
(which I can already do), save the results somewhere (already done as
well) and then carry on to read in the next question.
If anyone has any ideas as to how I can get around this, I'd be very
grateful.
Thanks in advance,
Paul
------------------------------
Date: Sun, 25 Feb 2007 20:05:04 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Reading in data until I have a full structure
Message-Id: <45e1ddce$0$5092$ba4acef3@news.orange.fr>
pwaring@gmail.com wrote:
> I've got a text file which is full of questions in a format similar to
> the following:
>
> QUESTION_ID "QUESTION_META_DATA
> FULL_QUESTION"
> /"SHORT_QUESTION"
> (ANSWER_1,
> ANSWER_2,
> ...
> ANSWER_N)
>
> At the moment I can parse each individual question into its component
> parts without any problems (it's not the most pleasant regex in the
> world, but it works), however I'm having trouble turning the whole
> file into an array of questions which I can then parse individually.
> Each question is separated from the next by at least two newlines, but
> unfortunately there is sometimes two newlines between SHORT_QUESTION
> and (ANSWER_1, so I can't assume that two newlines indicate the end of
> a question, which is what I've been doing so far.
>
> I was wondering if anyone could point me in the right direction for a
> way to get around this problem - basically I need to read in data
> until I know I've got a full question with answers (assuming this ends
> at two newlines often means I get the answers separately, which causes
> problems when I try to split this into smaller parts), parse that
> (which I can already do), save the results somewhere (already done as
> well) and then carry on to read in the next question.
>
I'm sure someone here who knows far more about regular expressions than
I do will come up with a workable solution, but personally I'd be
tempted to use a lexer instead.
http://www.perl.com/pub/a/2006/01/05/parsing.html
Mark
------------------------------
Date: Sun, 25 Feb 2007 19:49:31 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Reading in data until I have a full structure
Message-Id: <Xns98E296CC5189Easu1cornelledu@127.0.0.1>
"pwaring@gmail.com" <pwaring@gmail.com> wrote in
news:1172429732.393516.115680@j27g2000cwj.googlegroups.com:
> I've got a text file which is full of questions in a format similar to
> the following:
Please read the posting guidelines for this group before posting again.
> QUESTION_ID "QUESTION_META_DATA
> FULL_QUESTION"
> /"SHORT_QUESTION"
> (ANSWER_1,
> ANSWER_2,
> ...
> ANSWER_N)
>
...
> I was wondering if anyone could point me in the right direction for a
> way to get around this problem - basically I need to read in data
> until I know I've got a full question with answers (assuming this ends
> at two newlines often means I get the answers separately, which causes
> problems when I try to split this into smaller parts), parse that
> (which I can already do), save the results somewhere (already done as
> well) and then carry on to read in the next question.
You might want to read perldoc perlvar, especially about $/ :
#!/usr/bin/perl
use strict;
use warnings;
local $/ = ")\n\n";
my %questions;
while( my $chunk = <DATA> ) {
chomp $chunk;
$chunk =~ s/\A\s+//;
$chunk =~ s/\s+\z//;
if( $chunk =~ m{
\A
\s*
(\w+) # QUESTION_ID
\s+"
(\w+) # QUESTION_META_DATA
\n+\s+
(\w+) # FULL_QUESTION
"\n\s+/"
(\w+) # SHORT_QUESTION
"\n+\s+\(
(.+) # ANSWERS
}xms
)
{
my %q;
@q{ qw( qmeta qfull qshort ) } = ($2, $3, $4);
$q{ answers } = [ split /,\n\s+/, $5 ];
$questions{ $1 } = \%q;
}
}
use Data::Dumper;
print Dumper \%questions;
__DATA__
QUESTION_1 "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
ANSWER_3,
ANSWER_4,
ANSWER_N)
QUESTION_2 "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
ANSWER_N)
QUESTION_3 "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
ANSWER_X,
ANSWER_N)
C:\DOCUME~1\asu1\LOCALS~1\Temp\2> t
$VAR1 = {
'QUESTION_3' => {
'qfull' => 'FULL_QUESTION',
'qshort' => 'SHORT_QUESTION',
'answers' => [
'ANSWER_1',
'ANSWER_2',
'ANSWER_X',
'ANSWER_N'
],
'qmeta' => 'QUESTION_META_DATA'
},
'QUESTION_1' => {
'qfull' => 'FULL_QUESTION',
'qshort' => 'SHORT_QUESTION',
'answers' => [
'ANSWER_1',
'ANSWER_2',
'ANSWER_3',
'ANSWER_4',
'ANSWER_N'
],
'qmeta' => 'QUESTION_META_DATA'
},
'QUESTION_2' => {
'qfull' => 'FULL_QUESTION',
'qshort' => 'SHORT_QUESTION',
'answers' => [
'ANSWER_1',
'ANSWER_2',
'ANSWER_N'
],
'qmeta' => 'QUESTION_META_DATA'
}
};
------------------------------
Date: 25 Feb 2007 12:42:45 -0800
From: "pwaring@gmail.com" <pwaring@gmail.com>
Subject: Re: Reading in data until I have a full structure
Message-Id: <1172436165.434037.47720@t69g2000cwt.googlegroups.com>
On Feb 25, 7:49 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> You might want to read perldoc perlvar, especially about $/ :
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> local $/ = ")\n\n";
That looks almost like what I want, but I should have mentioned in my
original post that the brackets are optional if there is only one
answer, so I don't think that looking for )\n\n would work.
Paul
------------------------------
Date: Sun, 25 Feb 2007 21:30:47 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Reading in data until I have a full structure
Message-Id: <Xns98E2A7F7DFE8Basu1cornelledu@127.0.0.1>
"pwaring@gmail.com" <pwaring@gmail.com> wrote in
news:1172436165.434037.47720@t69g2000cwt.googlegroups.com:
> On Feb 25, 7:49 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>> You might want to read perldoc perlvar, especially about $/ :
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> local $/ = ")\n\n";
>
> That looks almost like what I want, but I should have mentioned in my
> original post that the brackets are optional if there is only one
> answer, so I don't think that looking for )\n\n would work.
Well, here's your last fish:
#!/usr/bin/perl
use strict;
use warnings;
my %questions;
LINE: while( my $line = <DATA> ) {
next LINE unless $line =~ /\AQUESTION/;
NEW_QUESTION: my $chunk = $line;
do {
$line = <DATA>;
unless ( defined $line ) {
parse_chunk( $chunk );
last LINE;
}
if ( $line =~ /\AQUESTION/ ) {
parse_chunk( $chunk );
goto NEW_QUESTION;
}
$chunk .= $line;
} while ( 1 );
}
sub parse_chunk {
my ($chunk) = @_;
$chunk =~ s/\A\s+//;
$chunk =~ s/\s+\z//;
if( $chunk =~ m{
\A
\s*
(\w+) # QUESTION_ID
\s+"
(\w+) # QUESTION_META_DATA
\n+\s+
(\w+) # FULL_QUESTION
"\n\s+/"
(\w+) # SHORT_QUESTION
"\n+\s+\(
(.+) # ANSWERS
}xms
)
{
my %q;
@q{ qw( qmeta qfull qshort ) } = ($2, $3, $4);
$q{ answers } = [ split /,\n\s+/, $5 ];
$questions{ $1 } = \%q;
}
}
use Data::Dumper;
print Dumper \%questions;
__DATA__
QUESTION_1 "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
ANSWER_3,
ANSWER_4,
ANSWER_N)
QUESTION_2 "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
ANSWER_N)
QUESTION_3 "QUESTION_META_DATA
FULL_QUESTION"
/"SHORT_QUESTION"
(ANSWER_1,
ANSWER_2,
ANSWER_X,
ANSWER_N)
$VAR1 = {
'QUESTION_3' => {
'qfull' => 'FULL_QUESTION',
'qshort' => 'SHORT_QUESTION',
'answers' => [
'ANSWER_1',
'ANSWER_2',
'ANSWER_X',
'ANSWER_N)'
],
'qmeta' => 'QUESTION_META_DATA'
},
'QUESTION_1' => {
'qfull' => 'FULL_QUESTION',
'qshort' => 'SHORT_QUESTION',
'answers' => [
'ANSWER_1',
'ANSWER_2',
'ANSWER_3',
'ANSWER_4',
'ANSWER_N)'
],
'qmeta' => 'QUESTION_META_DATA'
},
'QUESTION_2' => {
'qfull' => 'FULL_QUESTION',
'qshort' => 'SHORT_QUESTION',
'answers' => [
'ANSWER_1',
'ANSWER_2',
'ANSWER_N)'
],
'qmeta' => 'QUESTION_META_DATA'
}
};
Sinan
------------------------------
Date: Mon, 26 Feb 2007 11:58:45 +0100
From: Lars Haugseth <njus@larshaugseth.com>
Subject: Re: sorting an array with associated values in separate arrays
Message-Id: <871wkd89sq.fsf@durin.larshaugseth.com>
* "Ben" <jbenjam@gmail.com> wrote:
>
> Hi,
>
> I have the following arrays, each with the same number of elements.
> Each element of @main is associated with each corresponding element of
> @b, @c, and @d. i.e., the $main[1] is associated with $b[1],
> $x[1], ..., $z[1].
>
> @main = ( 1, 3, 2, 12, 5, 7, 2, 9); # these are likely to be non-
> unique floating point values in my real application
> @b = ( 1, 1, 2, 1, 1, 3, 1, 3); # the rest of these are
> integer values
> @x = ( 0, 9, 9, 9, 8, 8, 8, 1);
> @d = ( 1, 1, 1, 1, 1, 1, 1, 1);
> ...
>
> I would like to sort @main numerically in ascending order, and have
> @b, @x, @d, ... rearranged according to the new sorted order while
> maintaining the original association. Seems like a perfect job for
> a hash, I'm just not that comfortable with how to set it up in this
> case.
The approach I'd use is to store the data as an array of hashes:
my @data = ({ main => 1, b => 1, x => 0, d => 1, ...},
{ main => 3, b => 1, x => 9, d => 1, ...},
{ main => 2, b => 2, x => 9, d => 1, ...},
... );
You can then get a sorted version by simply doing
my @data_sorted = sort { $a->{main} <=> $b->{main} } @data;
--
Lars Haugseth
"If anyone disagrees with anything I say, I am quite prepared not only to
retract it, but also to deny under oath that I ever said it." -Tom Lehrer
------------------------------
Date: Sat, 24 Feb 2007 16:36:10 +0000 (UTC)
From: doctor@doctor.nl2k.ab.ca (The Doctor)
Subject: Thread issue
Message-Id: <erpphq$fvu$1@gallifrey.nk.ca>
Help. I tried to do a recompile of perl 5.8.8 and I forgot
the -Dusethreads option.
When I realized my mistake, I did try to add it back on
a recompile but suddenly B stops the whole process.
Running BSD/OS 4.3.1 .
Remedy please.
--
Member - Liberal International
This is doctor@nl2k.ab.ca Ici doctor@nl2k.ab.ca
God Queen and country! Beware Anti-Christ rising!
26 Mars 2007 - Quebec eliminez totalemant du carte politique le PQ!
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 175
**************************************