[26932] in Perl-Users-Digest
Perl-Users Digest, Issue: 8901 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 30 11:05:31 2006
Date: Mon, 30 Jan 2006 08:05:08 -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, 30 Jan 2006 Volume: 10 Number: 8901
Today's topics:
$lineNumber +=1 does not increment <wh2leung@student.cs.uwaterloo.ca>
Re: $lineNumber +=1 does not increment <d.cretel@wyniwyg.com>
Re: $lineNumber +=1 does not increment <1usa@llenroc.ude.invalid>
Re: $lineNumber +=1 does not increment <xx087@freenet.carleton.ca>
DBD/XBase et Zip file <d.cretel@wyniwyg.com>
Re: DBD/XBase et Zip file <d.cretel@wyniwyg.com>
Re: Embedding private .pm files into a script <hakonrk@fys.uio.no>
Re: Embedding private .pm files into a script (Anno Siegel)
How can I remove trailing commas? <jpearl342@yahoo.com>
Re: How can I remove trailing commas? <jurgenex@hotmail.com>
Re: How can I remove trailing commas? <rvtol+news@isolution.nl>
Re: How can I remove trailing commas? <abigail@abigail.nl>
Re: How can I remove trailing commas? <abigail@abigail.nl>
Re: How can I remove trailing commas? <xx087@freenet.carleton.ca>
Re: How can I remove trailing commas? <1usa@llenroc.ude.invalid>
How to use Registry functions <mrtest@amdosoft.com>
Re: How to use Registry functions <1usa@llenroc.ude.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 30 Jan 2006 09:54:59 -0500
From: William <wh2leung@student.cs.uwaterloo.ca>
Subject: $lineNumber +=1 does not increment
Message-Id: <Pine.GSO.4.64.0601300949370.8910@cpu02.student.cs.uwaterloo.ca>
I am trying to parse a file with the following code:
#!/usr/bin/perl -w
use strict;
# reads in all lines from diff.txt
open ( FD, "diff.txt" ) || die ( "Cannot open file diff.txt" );
my @fileContent = <FD>;
close ( FD );
my $lineNumber = 1; # file starts with line 1
my $C_previousPortfolio;
my $D_newPortfolio;
foreach my $line ( @fileContent ) {
print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
# skip line if line number is odd
if ( 1 == ($lineNumber % 2) ) {
next;
}
else {
if ( $line =~ m/\>/ ) {
( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
$line );
}
}
$lineNumber += 1;
}
File content:
0a1
> "2nd D difference"
3a5
> "IN 27 but not in 26 - D"
Current output:
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Expected output:
, , 1
, 2nd D difference, 2
, , 3
, IN 27 but not in 26 - D, 4
Questions:
1) Why doesn't $lineNumber increment?
------------------------------
Date: Mon, 30 Jan 2006 16:17:28 +0100
From: =?ISO-8859-1?Q?Dominique_Cr=E9tel?= <d.cretel@wyniwyg.com>
Subject: Re: $lineNumber +=1 does not increment
Message-Id: <43de2dc6$0$704$626a14ce@news.free.fr>
$lineNumber start with value 1
In your foreach, you do a 'next' if ($lineNumber % 2) == 1
It's the case, so you jump in the next loop without increasing it's value !
William a écrit :
> my $lineNumber = 1; # file starts with line 1
>
> foreach my $line ( @fileContent ) {
> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;
> }
> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;
------------------------------
Date: Mon, 30 Jan 2006 15:17:19 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: $lineNumber +=1 does not increment
Message-Id: <Xns975B689DCFC92asu1cornelledu@132.236.56.8>
William <wh2leung@student.cs.uwaterloo.ca> wrote in
news:Pine.GSO.4.64.0601300949370.8910@cpu02.student.cs.uwaterloo.ca:
> I am trying to parse a file with the following code:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> # reads in all lines from diff.txt
> open ( FD, "diff.txt" ) || die ( "Cannot open file diff.txt" );
> my @fileContent = <FD>;
> close ( FD );
Please see the posting guidelines for this group to learn how to provide
the data your script needs to work.
> my $lineNumber = 1; # file starts with line 1
> my $C_previousPortfolio;
> my $D_newPortfolio;
>
> foreach my $line ( @fileContent ) {
> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;
> }
> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;
> }
I have no idea what this code is trying to do. I don't know what data you
are expecting to read.
Formatting your source better, and therefore respecting your reader pays
dividends for you as well.
> Questions:
> 1) Why doesn't $lineNumber increment?
Why should it? Stripping the parts that are not essential:
my $lineNumber = 1;
foreach my $line ( @fileContent ) {
if ( 1 == ($lineNumber % 2) ) {
next;
}
$lineNumber += 1;
}
The condition in the if statement above will be true upon entry to this
loop, and therefore it will never get to the statement where $lineNumber is
incremented.
Sinan
------------------------------
Date: 30 Jan 2006 15:27:07 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: $lineNumber +=1 does not increment
Message-Id: <slrndtsc2b.5bi.xx087@smeagol.ncf.ca>
At 2006-01-30 09:54AM, William <wh2leung@student.cs.uwaterloo.ca> wrote:
> I am trying to parse a file with the following code:
>
[...]
> my $lineNumber = 1; # file starts with line 1
> my $C_previousPortfolio;
> my $D_newPortfolio;
>
> foreach my $line ( @fileContent ) {
> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;
> }
Logic error. Since $lineNumber starts as 1, this if expression always
evaluates true, and you call 'next' in every iteration of the loop. You
will never reach your increment statement.
> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;
> }
[...]
> Questions:
> 1) Why doesn't $lineNumber increment?
To fix, put the increment statement in a continue block so it is invoked
for each iteration of the loop even if you call 'next':
my $lineNumber = 1;
foreach my $line (...) {
if (1 == $lineNumber%2) {
next;
}
...
}
continue {
$lineNumber++;
}
--
Glenn Jackman
Ulterior Designer
------------------------------
Date: Mon, 30 Jan 2006 13:40:57 +0100
From: =?ISO-8859-1?Q?Dominique_Cr=E9tel?= <d.cretel@wyniwyg.com>
Subject: DBD/XBase et Zip file
Message-Id: <43de0917$0$697$626a14ce@news.free.fr>
Hello,
I have a DBase files into a zip file.
There is only one directory (firstdir) in the root of mydb.zip file,
which contain another directory (seconddir). This last directory
contains a set od DBase files like this :
mydb.zip
+---firstdir
+---seconddir
+---aaa.dbf
+---bbb.dbf
+---ccc.dbf
+---and so on...
I am looking for something like this (if it's possible ?) :
-----------------
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect("DBI:XBase:./mydb.zip")
or die "error connecting to database :" . $DBI::errstr;
my $sth = $dbh->prepare("SELECT col1, col2 FROM table1")
or die "SQL error SQL:".$dbh->errstr();
$sth->execute or die "error while executing query:".$sth->errstr();
-----------------
Thanks.
------------------------------
Date: Mon, 30 Jan 2006 16:18:58 +0100
From: =?ISO-8859-1?Q?Dominique_Cr=E9tel?= <d.cretel@wyniwyg.com>
Subject: Re: DBD/XBase et Zip file
Message-Id: <43de2e20$0$704$626a14ce@news.free.fr>
Is there any body who can help me ?
------------------------------
Date: Mon, 30 Jan 2006 11:58:10 +0000 (UTC)
From: Haakon Riiser <hakonrk@fys.uio.no>
Subject: Re: Embedding private .pm files into a script
Message-Id: <slrndtrvqi.1p6.hakonrk@fox.venod.com>
[Ala Qumsieh]
> What's wrong with a simple:
>
> use lib '/path/to/your/module/dir';
>
> ?
I can think of at least three things:
(1) I don't want the script to search in a specific location.
I could use relative paths, but that would still be a problem if
someone decides to move the executable to another bin directory.
(2) If someone asks for a script 'foo', I want to be able to give
that to him without having to manually determine the module
dependencies and then copying all of the required files.
(3) I don't want other people to have to deal with the clutter
of separate modules. A single script should be enough for these
trivial things I'm talking about here.
But nevermind -- simply substituting the 'use' statement with the
contents of the respective module is good enough in this case, and
automating that task is simple.
--
Haakon
------------------------------
Date: 30 Jan 2006 14:02:03 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Embedding private .pm files into a script
Message-Id: <drl68r$g6k$1@mamenchi.zrz.TU-Berlin.DE>
Haakon Riiser <hakonrk@fys.uio.no> wrote in comp.lang.perl.misc:
> [A. Sinan Unur]
>
> > See
> >
> > perldoc -q lib
> > perldoc lib
> > perldoc FindBin
> >
> > NAME
> > FindBin - Locate directory of original perl script
> >
> > SYNOPSIS
> > use FindBin;
> > use lib "$FindBin::Bin/../lib";
> >
> > or
> >
> > use FindBin qw($Bin);
> > use lib "$Bin/../lib";
> >
> > So, put your scripts in a directory, and put your packages in a
> > subdirectory of that. Then zip everything up. Give them the zip file.
>
> Thanks for the tip, but I'd prefer not having to reorganize my
> directories.
>
> I think I'll just write a simple script to convert all 'use'
> declarations that refer to my private modules by a simple
>
> package Private::Foo;
> <contents of Private/Foo.pm>
> package main;
>
> This works as long as I don't include my modules in a fancy way
> (such as loading them on demand, etc.)
The "package"-statements are presumably already part of Private::Foo.
However, the contents should go into a block of their own so that
file-scoped lexicals in Private/Foo.pm don't leak out into your
lexical space. Also, if you want to put the module after code that
uses it, that block must be a BEGIN block. Thirdly, in case the module
has an ->import method, it should be called with the parameters that
would otherwise go in the "use" statement.
So, to replace the statement
use Private::Foo qw( fie foe fum);
one should use two BEGIN blocks:
BEGIN {
<contents of Private/Foo.pm>
}
BEGIN {
Private::Foo->import( qw( fie foe fum);
}
That should cover most cases.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: 30 Jan 2006 11:16:36 -0000
From: Jame Pearl <jpearl342@yahoo.com>
Subject: How can I remove trailing commas?
Message-Id: <VCH1H3BQ38747.8031944444@reece.net.au>
How can I remove any trailing commas from the end of $var?
I tried using
$var =~ s/,+$//
but it didn't work.
------------------------------
Date: Mon, 30 Jan 2006 13:46:45 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How can I remove trailing commas?
Message-Id: <9NoDf.23101$5G.19294@trnddc08>
Jame Pearl wrote:
> How can I remove any trailing commas from the end of $var?
>
> I tried using
>
> $var =~ s/,+$//
>
> but it didn't work.
Works for me:
C:\tmp>type t.pl
use strict; use warnings;
my $var = 'foo,,,,';
print "$var\n";
$var =~ s/,+$//;
print "$var\n";
C:\tmp>t.pl
foo,,,,
foo
Maybe your $var does not contain what you think it contains?
jue
------------------------------
Date: Mon, 30 Jan 2006 15:14:55 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: How can I remove trailing commas?
Message-Id: <drlao2.1d0.1@news.isolution.nl>
Jame Pearl schreef:
> How can I remove any trailing commas from the end of $var?
> I tried using
> $var =~ s/,+$//
> but it didn't work.
If
$var =~ s/,+$//m;
does work, see `perldoc perlre` again.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 30 Jan 2006 15:11:02 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How can I remove trailing commas?
Message-Id: <slrndtsb46.e1.abigail@alexandra.abigail.nl>
Jame Pearl (jpearl342@yahoo.com) wrote on MMMMDXXXV September MCMXCIII in
<URL:news:VCH1H3BQ38747.8031944444@reece.net.au>:
!! How can I remove any trailing commas from the end of $var?
!!
!! I tried using
!!
!! $var =~ s/,+$//
!!
!! but it didn't work.
Really? Could you give an example program where it fails to work?
$ perl -wle '$var = "foo,"; $var =~ s/,$//; print $var'
foo
Works here!
Abigail
--
perl -e '$a = q 94a75737420616e6f74686572205065726c204861636b65720a9 and
${qq$\x5F$} = q 97265646f9 and s g..g;
qq e\x63\x68\x72\x20\x30\x78$&eggee;
{eval if $a =~ s e..eqq qprint chr 0x$& and \x71\x20\x71\x71qeexcess}'
------------------------------
Date: 30 Jan 2006 15:12:08 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How can I remove trailing commas?
Message-Id: <slrndtsb68.e1.abigail@alexandra.abigail.nl>
Dr.Ruud (rvtol+news@isolution.nl) wrote on MMMMDXXXV September MCMXCIII
in <URL:news:drlao2.1d0.1@news.isolution.nl>:
^^ Jame Pearl schreef:
^^
^^ > How can I remove any trailing commas from the end of $var?
^^ > I tried using
^^ > $var =~ s/,+$//
^^ > but it didn't work.
^^
^^ If
^^
^^ $var =~ s/,+$//m;
^^
^^ does work, see `perldoc perlre` again.
Hmmm.
$ perl -wle '$var = "foo,\nbar,"; $var =~ s/,$//m; print $var'
foo
bar,
That doesn't remove the comma at "the end".
Abigail
--
sub _'_{$_'_=~s/$a/$_/}map{$$_=$Z++}Y,a..z,A..X;*{($_::_=sprintf+q=%X==>"$A$Y".
"$b$r$T$u")=~s~0~O~g;map+_::_,U=>T=>L=>$Z;$_::_}=*_;sub _{print+/.*::(.*)/s};;;
*_'_=*{chr($b*$e)};*__=*{chr(1<<$e)}; # Perl 5.6.0 broke this...
_::_(r(e(k(c(a(H(__(l(r(e(P(__(r(e(h(t(o(n(a(__(t(us(J())))))))))))))))))))))))
------------------------------
Date: 30 Jan 2006 15:16:02 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: How can I remove trailing commas?
Message-Id: <slrndtsbdi.5bi.xx087@smeagol.ncf.ca>
At 2006-01-30 06:16AM, Jame Pearl <jpearl342@yahoo.com> wrote:
> How can I remove any trailing commas from the end of $var?
>
> I tried using
>
> $var =~ s/,+$//
>
> but it didn't work.
Perhaps $var contains trailing whitespace. Try:
$var =~ s/,+\s+$//;
--
Glenn Jackman
Ulterior Designer
------------------------------
Date: Mon, 30 Jan 2006 15:54:01 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How can I remove trailing commas?
Message-Id: <Xns975B6ED661313asu1cornelledu@132.236.56.8>
Abigail <abigail@abigail.nl> wrote in
news:slrndtsb68.e1.abigail@alexandra.abigail.nl:
> Dr.Ruud (rvtol+news@isolution.nl) wrote on MMMMDXXXV September MCMXCIII
> in <URL:news:drlao2.1d0.1@news.isolution.nl>:
> ^^ Jame Pearl schreef:
> ^^
> ^^ > How can I remove any trailing commas from the end of $var?
> ^^ > I tried using
> ^^ > $var =~ s/,+$//
> ^^ > but it didn't work.
> ^^
> ^^ If
> ^^
> ^^ $var =~ s/,+$//m;
> ^^
> ^^ does work, see `perldoc perlre` again.
>
>
> Hmmm.
>
>
> $ perl -wle '$var = "foo,\nbar,"; $var =~ s/,$//m; print $var'
> foo
> bar,
This seals it for me. I have now been converted to the PBP recommendation
precisely which I can never remember what m and s modifiers do:
#!/usr/bin/perl
use strict;
use warnings;
my $var = "foo,\nbar,";
$var =~ s{ ,\z }{ }msx;
print "$var\n";
__END__
C:\Home\asu1\Perl> perl t.pl
foo,
bar
------------------------------
Date: Mon, 30 Jan 2006 14:56:11 +0100
From: "Marco Roda" <mrtest@amdosoft.com>
Subject: How to use Registry functions
Message-Id: <drl5tr$oi4$1@ss405.t-com.hr>
I should use NTRegOpenKeyEx to read a registry key.
I got some example like:
<
require 'NT.ph';
use Win32::Registry;
$rc = NTRegOpenKeyEx ( &HKEY_LOCAL_MACHINE,
'SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Virtual Roots', &NULL,
&KEY_ALL_ACCESS, $PerlKey );
NTRegQueryValueEx( $PerlKey, '/', &NULL, $type, $HttpRoot );
>
but I have no NT.ph
- where should I found it ?
- how to install ?
Thanks in advance,
Marco
------------------------------
Date: Mon, 30 Jan 2006 15:20:43 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to use Registry functions
Message-Id: <Xns975B69311F7E4asu1cornelledu@132.236.56.8>
"Marco Roda" <mrtest@amdosoft.com> wrote in
news:drl5tr$oi4$1@ss405.t-com.hr:
> I should use NTRegOpenKeyEx to read a registry key.
> I got some example like:
> <
> require 'NT.ph';
> use Win32::Registry;
>
> $rc = NTRegOpenKeyEx ( &HKEY_LOCAL_MACHINE,
> 'SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Virtual Roots',
> &NULL, &KEY_ALL_ACCESS, $PerlKey );
> NTRegQueryValueEx( $PerlKey, '/', &NULL, $type, $HttpRoot );
>>
> but I have no NT.ph
> - where should I found it ?
> - how to install ?
I have no idea.
On the other hand, CPAN is your friend.
http://search.cpan.org/~tyemq/Win32-TieRegistry-0.24/TieRegistry.pm
The documentation shows no mention of NT.ph.
Also, if you had looked, you would have seen:
Win32::Registry - accessing the Windows registry [obsolete, use
Win32::TieRegistry]
Hope this helps.
Sinan
------------------------------
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 V10 Issue 8901
***************************************