[22627] in Perl-Users-Digest
Perl-Users Digest, Issue: 4848 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 15 09:05:45 2003
Date: Tue, 15 Apr 2003 06:05:11 -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 Tue, 15 Apr 2003 Volume: 10 Number: 4848
Today's topics:
$name=<STDIN> problem <jon@rogers.tv>
Re: Accesing machines of a network <kasp@epatra.com>
Re: Accesing machines of a network <nobull@mail.com>
Re: Accesing machines of a network <nobull@mail.com>
Re: Accesing machines of a network <kasp@epatra.com>
Re: Accesing machines of a network (Helgi Briem)
Re: Accesing machines of a network <kasp@epatra.com>
Re: Beginners qn about while loops/regex <krahnj@acm.org>
Re: Beginners qn about while loops/regex <nobull@mail.com>
Re: Beginners qn about while loops/regex (Matt T)
Re: Deep Recursion problem in Win32 clipboard.pm (Phil Hibbs)
Re: How does Perl auto increase hash size? <Steffen.Beyer@de.bosch.com>
Re: How does Perl auto increase hash size? <abigail@abigail.nl>
Re: How does Perl auto increase hash size? <Steffen.Beyer@de.bosch.com>
Re: How does Perl auto increase hash size? <abigail@abigail.nl>
How to trap 500 Internal Error? <jkatz@sk.sympatico.ca>
Re: Matrix problem solving in perl <Steffen.Beyer@de.bosch.com>
newbie:substitution in a variable name <stacom@stacom-software.de>
Re: PHP Code to PERL conversion... (S G)
Re: problem with this perl script <D'oh@a.deer>
Re: Specific Regex Question <krahnj@acm.org>
Re: Specific Regex Question <john-s@moving-picture.com>
Re: Specific Regex Question (Cameron)
Re: Validating user input to match certain characters <abigail@abigail.nl>
Re: Validating user input to match certain characters <usenet@NOSPAM.matthewb.org>
Re: Validating user input to match certain characters <usenet@NOSPAM.matthewb.org>
WIN32::ODBC problem <bigus AT creationfactor DOT net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 15 Apr 2003 15:12:35 +0200
From: Jon Rogers <jon@rogers.tv>
Subject: $name=<STDIN> problem
Message-Id: <3E9C0542.C1EBE8BB@rogers.tv>
Hello
I'm experiencing problems with collecting info from the user:
print "press Y to identify yourself or N to move on";
my $key=getc(STDIN);
if ($key eq 'Y') {
print "what is your first name?";
my $firstname = <STDIN>;
print "what is your second name?";
my $secondname = <STDIN>;
}
...
when run, this gives:
"
what is your first name?
what is your second name?
"
for some reason it skips reading in from STDIN. what am I doing wrong?
Thanks for your time,
JR
------------------------------
Date: Tue, 15 Apr 2003 15:35:48 +0530
From: "Kasp" <kasp@epatra.com>
Subject: Re: Accesing machines of a network
Message-Id: <b7gli2$gdb$1@newsreader.mailgate.org>
I am encountering a strange behavior...
On command prompt, when I specify a file on machine2 as follows:
C:\> \\machine2\dir1\dir2\file1.txt
It opens the file.
But when I try this through Perl in my code like this:
$filename = '\\machine2\dir1\dir2\file1.txt';
if(-e $filename){
print "-E Access. $!";
}
else{
print "No -E Access. $!";
}
I get the message "No -E Access. No such file or directory". Why is this?
If Windows can locate the file through command prompt, then why can't Perl?
Another strange thing is that when I say:
C:\> dir \\machine2
It says "The filename, directory name, or volume label syntax is incorrect."
But if I say:
C:\> dir \\machine2\dir1
Then I can see the contents.
--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - Dilbert.
------------------------------
Date: 15 Apr 2003 12:03:16 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Accesing machines of a network
Message-Id: <u94r50auq3.fsf@wcl-l.bham.ac.uk>
"Kasp" <kasp@epatra.com> writes:
> $filename = '\\machine2\dir1\dir2\file1.txt';
The \ character is special in literal strings in Perl. Change it to /
to escape it appropriately.
To see what's actually in $filename try printing out the contents of $filename.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 15 Apr 2003 12:33:37 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Accesing machines of a network
Message-Id: <u9smsk9er2.fsf@wcl-l.bham.ac.uk>
Brian McCauley <nobull@mail.com> writes:
> "Kasp" <kasp@epatra.com> writes:
>
> > $filename = '\\machine2\dir1\dir2\file1.txt';
>
> The \ character is special in literal strings in Perl. Change it to /
> to escape it appropriately.
^^
That should say "or".
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Tue, 15 Apr 2003 17:45:59 +0530
From: "Kasp" <kasp@epatra.com>
Subject: Re: Accesing machines of a network
Message-Id: <b7gt66$tdr$1@newsreader.mailgate.org>
Thanks Brian, that helped. But I still am facing some problems.
1. For eg. I need to authenticate myself when I connect to a machine. Any
idea how I can do this from Perl?
2 . Also, once I am connected to a machine and authenticated, I need to find
out the files & directories that have been shared. I can't seem to be able
to do this. However, if I know that a directory (say dir2) exists under
\\machine2, then I can operate on it using "opendir" & "-e" (file exists)
etc. So the point is how can I come to know what all directories are shared
under \\machine2
TIA.
--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - Dilbert.
------------------------------
Date: Tue, 15 Apr 2003 12:39:46 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: Accesing machines of a network
Message-Id: <3e9bfcac.423711915@news.cis.dfn.de>
On Tue, 15 Apr 2003 15:35:48 +0530, "Kasp" <kasp@epatra.com>
wrote:
>I am encountering a strange behavior...
>On command prompt, when I specify a file on machine2 as follows:
>C:\> \\machine2\dir1\dir2\file1.txt
>It opens the file.
>
>But when I try this through Perl in my code like this:
>$filename = '\\machine2\dir1\dir2\file1.txt';
Use sane slashes, ie
my $dir = '//server/dir';
opendir DIR, $dir or die "Cannot opendir $dir:$!\n";
print join "\n",(grep !/\.{1,2}$/,readdir DIR);
Always, always, always check opens and opendirs for
success and return the path and error.
You could also specify the server with \\\\server,
but don't. There is no need for backslashes.
--
Regards, Helgi Briem
helgi DOT briem AT decode DOT is
------------------------------
Date: Tue, 15 Apr 2003 18:28:04 +0530
From: "Kasp" <kasp@epatra.com>
Subject: Re: Accesing machines of a network
Message-Id: <b7gvl6$2d9$1@newsreader.mailgate.org>
> >$filename = '\\machine2\dir1\dir2\file1.txt';
> Use sane slashes, ie
> my $dir = '//server/dir';
I am on a Windows machine :-). So my slashes are ok on a Windows machine.
For Unix-like machines, you are correct.
> Always, always, always check opens and opendirs for
> success and return the path and error.
I am doing that.
Right now my main problem is getting the first-level of folders when you
connect to a machine. Once I have that I can recursively scan the
directories.
--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - Dilbert.
------------------------------
Date: Tue, 15 Apr 2003 10:26:58 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Beginners qn about while loops/regex
Message-Id: <3E9BDE52.83D741EA@acm.org>
Matt T wrote:
>
> I have a script that while-loops through each line of a file then
> uses another while-loop to run through each word in the line using
> a regex to match the words. I would like to know if it is possible
> to get the index of the start of the word that I matched? I currently
> have a solution which is incorrect. My code is roughly:
>
> while (<INPUTFILE>) {
> $line = $_;
> while ($line =~ /(\w+)/g) {
> # I would like to get the index of the word I matched here!
> # Process the word
$ perl -e'
my $line = q/The quick brown fox jumps over the lazy dog./;
while ( $line =~ /(\w+)/g ) {
my $word = $1;
my $pos = pos( $line ) - length( $word );
printf "%4d %s\n", $pos, $word;
}
'
0 The
4 quick
10 brown
16 fox
20 jumps
26 over
31 the
35 lazy
40 dog
John
--
use Perl;
program
fulfillment
------------------------------
Date: 15 Apr 2003 12:26:05 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Beginners qn about while loops/regex
Message-Id: <u9znms9f3m.fsf@wcl-l.bham.ac.uk>
mctrewin@ihug.com.au (Matt T) writes:
> Subject: Beginners qn about while loops/regex
As a beginner you are not qualified to know if you have a beginner
question or not. Only a more experienced user answering your question
can do that. The more obscure special variables (such as @- (the one
you seek)) are often considered a quite advanced topic.
Do not waste valuable space in your subject line with guesses about
how advanced is the topic.
Your question has nothing really to do with while loops. I has to do
with the use of m//g in a scalar context. However I suppose that's a
subtlty you can be forgiven for not appreciating just yet.
Having removed all the obvious noise from the subject line you'd be
left with pleanty of space to put real hard information about your
question.
E.g.
Subject: Finding index of regex match in while(/(\w+)/g)
See? With only a little thought you could have clearly have expressed
your whole question in the subject line!
Good subject lines are vitally important in making Usenet a useful
searchable resource. As a beginner you can still do your bit to
contribure to the community by puttin a lot of effort into composing
your subject lines.
> I have a script that while-loops through each line of a file
This is in no way relevant to your question. Figuring out what is and
what is not part of your question is 80% of solving it.
> then uses another while-loop to run through each word in the line
> using a regex to match the words. I would like to know if it is
> possible to get the index of the start of the word that I matched?
Yep, but only in recent Perl - the special variable @- is a recent addtion.
> I currently
> have a solution which is incorrect. My code is roughly:
>
> while (<INPUTFILE>) {
> $line = $_;
> while ($line =~ /(\w+)/g) {
Make up you mind. Do you want this in $_ or not? Always declare all
variables as lexically scoped in the smallest appripriate scope unless
there is a reason to do otherwise. Enabling strictures would help you
to remember this.
Make that:
while ( my $line = <INPUTFILE>) {
while ($line =~ /(\w+)/g) {
Or:
while (<INPUTFILE>) {
while (/(\w+)/g) {
> # I would like to get the index of the word I matched here!
It is in $-[0] and $-[1].
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 15 Apr 2003 05:56:02 -0700
From: mctrewin@ihug.com.au (Matt T)
Subject: Re: Beginners qn about while loops/regex
Message-Id: <5e41989.0304150456.19855c05@posting.google.com>
Ah great, I think pos() will help me for what I need to do.
Thanks heaps guys!
Matt.
------------------------------
Date: 15 Apr 2003 04:01:08 -0700
From: gg@snark.freeserve.co.uk (Phil Hibbs)
Subject: Re: Deep Recursion problem in Win32 clipboard.pm
Message-Id: <a9ec249e.0304150301.77844add@posting.google.com>
"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message news:<b5n1v5$r6r$1@nets3.rz.RWTH-Aachen.DE>...
> Also sprach Anno Siegel:
> > Where is constant() defined? If it isn't, you're calling an undefined
> > function from AUTOLOAD, which calls AUTOLOAD again... . A recipe for
> > deep recursion.
>
> And in this case he has to reinstall the module since a missing
> constant() functions suggests that the XS portion of the module either
> isn't there or is not properly loaded by DynaLoader.
> Tassilo
Looking at the files here:
http://ppm.activestate.com/PPMPackages/zips/5xx-builds-only/
My clipboard.pm is identical to the one contained in
Win32-ClipBoard.zip on that page.
Where would you expect the constant() function to be defined? Would it
have to be in the clipboard.pm module? If so, the clipboard.pm from
that distribution is broken, and reinstalling it won't help, not that
I can do that anyway.
Any further advice would be appreciated, given the limitation that I
can't reinstall anything, although I can edit and replace individual
files that are not part of the core functionality (I don't want to
risk breaking anything).
Phil Hibbs.
------------------------------
Date: Tue, 15 Apr 2003 13:55:51 +0200
From: "Steffen Beyer" <Steffen.Beyer@de.bosch.com>
Subject: Re: How does Perl auto increase hash size?
Message-Id: <b7gs07$iou$1@ns2.fe.internet.bosch.com>
Abigail wrote in response to the following posting (snipped):
> -- If a hash wasn't pre-sized large enough so that Perl has to
> -- automatically increase its max size, what algo does Perl implement for
> -- this? -- double the size and redistribute elements?
> [...]
> What the initial size is, and how it's incremented may change from
> version to version. It's not specified. But you can do some experimenting
> yourself, if you don't want to look it up in the source code. Remember
> that a hash in scalar context returns (if the hash is non-empty), a string
> of the form "DD/DD", with the first number the number of used buckets,
> and the second number the number of allocated buckets.
> Abigail
How about the other question above?
AFAIK Perl does actually double the size (in most cases?), but
how does Perl actually re-hash the elements in a hash when it
is increasing the hash's size?
I can't remember having seen an algorithm for this in my CS
classes (but we extensively explored quadratic probing after
hash function conflicts, for example).
Is there a clever way to get around re-hashing altogether?
How does this work?
I'm just being curious...
Thanks a lot in advance!
Best regards,
Steffen Beyer
------------------------------
Date: 15 Apr 2003 12:26:14 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How does Perl auto increase hash size?
Message-Id: <slrnb9nuj6.ag6.abigail@alexandra.abigail.nl>
Steffen Beyer (Steffen.Beyer@de.bosch.com) wrote on MMMDXIV September
MCMXCIII in <URL:news:b7gs07$iou$1@ns2.fe.internet.bosch.com>:
$$ Abigail wrote in response to the following posting (snipped):
$$
$$ > -- If a hash wasn't pre-sized large enough so that Perl has to
$$ > -- automatically increase its max size, what algo does Perl implement for
$$ > -- this? -- double the size and redistribute elements?
$$ > [...]
$$
$$ > What the initial size is, and how it's incremented may change from
$$ > version to version. It's not specified. But you can do some experimenting
$$ > yourself, if you don't want to look it up in the source code. Remember
$$ > that a hash in scalar context returns (if the hash is non-empty), a string
$$ > of the form "DD/DD", with the first number the number of used buckets,
$$ > and the second number the number of allocated buckets.
$$ > Abigail
$$
$$ How about the other question above?
$$
$$ AFAIK Perl does actually double the size (in most cases?), but
$$ how does Perl actually re-hash the elements in a hash when it
$$ is increasing the hash's size?
Just like almost any sane hash function, Perls hash function includes
the current size (amount of buckets) in the hash.
$$ I can't remember having seen an algorithm for this in my CS
$$ classes (but we extensively explored quadratic probing after
$$ hash function conflicts, for example).
Perl hashes use linear chaining, and no double hashing techique.
$$ Is there a clever way to get around re-hashing altogether?
No.
$$ How does this work?
The simplest way possible. Just recalculate the hash value (which only
has a small chance of being the same as the old value).
Abigail
--
perl -wle'print"Κυστ αξοτθες Πεςμ Θαγλες"^"\x80"x24'
------------------------------
Date: Tue, 15 Apr 2003 14:48:48 +0200
From: "Steffen Beyer" <Steffen.Beyer@de.bosch.com>
Subject: Re: How does Perl auto increase hash size?
Message-Id: <b7gv3h$l55$1@ns2.fe.internet.bosch.com>
[Complimentary Cc: sent to abigail@abigail.nl]
Hi Abigail:
> $$ how does Perl actually re-hash the elements in a hash when it
> $$ is increasing the hash's size?
>
> Just like almost any sane hash function, Perls hash function includes
> the current size (amount of buckets) in the hash.
Of course. But this also makes the result of the hashing function
depend on the size of the hash, doesn't it?
> Perl hashes use linear chaining, and no double hashing techique.
Ah, ok.
> > How does this work?
> The simplest way possible. Just recalculate the hash value (which only
> has a small chance of being the same as the old value).
So when you increase the size, you do not only have to recalculate
the hash function for each entry, you also have to move the entry
to its new place, indicated by the new hash function - right?
How is this moving done efficiently?
Through reassigning pointers?
Thanks!
Best regards,
Steffen
------------------------------
Date: 15 Apr 2003 13:02:21 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How does Perl auto increase hash size?
Message-Id: <slrnb9o0mt.ag6.abigail@alexandra.abigail.nl>
Steffen Beyer (Steffen.Beyer@de.bosch.com) wrote on MMMDXIV September
MCMXCIII in <URL:news:b7gv3h$l55$1@ns2.fe.internet.bosch.com>:
`' [Complimentary Cc: sent to abigail@abigail.nl]
`'
`' Hi Abigail:
`'
`' > $$ how does Perl actually re-hash the elements in a hash when it
`' > $$ is increasing the hash's size?
`' >
`' > Just like almost any sane hash function, Perls hash function includes
`' > the current size (amount of buckets) in the hash.
`'
`' Of course. But this also makes the result of the hashing function
`' depend on the size of the hash, doesn't it?
Yes.
`' > Perl hashes use linear chaining, and no double hashing techique.
`'
`' Ah, ok.
`'
`' > > How does this work?
`' > The simplest way possible. Just recalculate the hash value (which only
`' > has a small chance of being the same as the old value).
`'
`'
`' So when you increase the size, you do not only have to recalculate
`' the hash function for each entry, you also have to move the entry
`' to its new place, indicated by the new hash function - right?
Yes. (Note that part of the calculation are cached).
`' How is this moving done efficiently?
`' Through reassigning pointers?
Yes.
Abigail
--
tie $" => A; $, = " "; $\ = "\n"; @a = ("") x 2; print map {"@a"} 1 .. 4;
sub A::TIESCALAR {bless \my $A => A} # Yet Another silly JAPH by Abigail
sub A::FETCH {@q = qw /Just Another Perl Hacker/ unless @q; shift @q}
------------------------------
Date: Tue, 15 Apr 2003 06:46:22 -0600
From: "Syl" <jkatz@sk.sympatico.ca>
Subject: How to trap 500 Internal Error?
Message-Id: <v9nvp7lcebeg98@corp.supernews.com>
How do I trap a "500 Internal server error" from a server that randomly
serves this error (in fact it is occurring with the Google API), sleep for a
bit and then repeat the request until it clears?
I am not using LWP - I am strictly using the Google API as follows (except I
have it in a loop with a changing query):
#!/usr/bin/perl -w
use strict;
use SOAP::Lite;
my $googleSearch =
SOAP::Lite->service('http://api.google.com/GoogleSearch.wsdl');
my $key = 'ENTER YOUR OWN PERSONAL GOOGLE API KEY HERE';
my $query = 'perl';
my $result = $googleSearch->doGoogleSearch($key, $query, 10, 10, 'false',
'', 'false', '', 'latin1', 'latin1');
foreach my $element (@{$result->{'resultElements'}}) {
foreach my $key (keys(%$element)) {
print $key, ': ', $element->{$key}, "\n";
}
print "\n"
}
Syl
------------------------------
Date: Tue, 15 Apr 2003 13:45:30 +0200
From: "Steffen Beyer" <Steffen.Beyer@de.bosch.com>
Subject: Re: Matrix problem solving in perl
Message-Id: <b7grcq$ib7$1@ns2.fe.internet.bosch.com>
"manish" <manish__anand@hotmail.com> wrote:
> I am handling a bulk of data nearly a matrix with around 40000 keys
> and approximately same number of values for each key i.e. nearly
> 1,600,000,000 terms in the matrix. Hence, I am looking for an
> efficient method.
>
> I tried using the very inefficient way of creating a matrix with all
> possible entries from the 2 matrices. But, this leads to creation of
> sparse which takes a lot of memory and hence I was unable to run the
> entire analysis.
>
> If there is no efficient way of performing this in perl, please can
> anyone suggest a better way in C/C++/Java.
>
> It would be great if somebody could reply to this post at the
> earliest.
>
> Thanks in advance,
> Manish Anand
Have you tried using hashes instead of arrays?
I.e.,
$matrix = { };
$matrix->{1}{1} = 1;
$matrix->{1}{4} = 34;
$matrix->{1}{3} = 21;
$matrix->{1}{2} = 12;
$matrix->{4}{1} = 0;
$matrix->{4}{4} = 1;
$matrix->{4}{3} = 2;
$matrix->{4}{2} = 8;
$matrix->{3}{1} = 0;
$matrix->{3}{4} = 0;
$matrix->{3}{3} = 1;
$matrix->{3}{2} = 21;
$matrix->{2}{1} = 0;
$matrix->{2}{4} = 0;
$matrix->{2}{3} = 0;
$matrix->{2}{2} = 1;
$matrix->{1}{1} = 1;
$matrix->{1}{8} = 4;
$matrix->{1}{5} = 3;
$matrix->{8}{1} = 0;
$matrix->{8}{8} = 1;
$matrix->{8}{5} = 2;
$matrix->{5}{1} = 0;
$matrix->{5}{8} = 0;
$matrix->{5}{5} = 1;
Good luck!
Steffen
------------------------------
Date: Tue, 15 Apr 2003 15:01:09 +0200
From: Alexander Eisenhuth <stacom@stacom-software.de>
Subject: newbie:substitution in a variable name
Message-Id: <b7gvq8$q7ni$1@ID-155280.news.dfncis.de>
Hallo,
how can I access the value of these variables in a loop:
my $B_01MASK = 0x01;
my $B_02MASK = 0x02;
my $B_03MASK = 0x04;
my $B_04MASK = 0x08;
my $B_05MASK = 0x10;
my $B_06MASK = 0x20;
my $B_07MASK = 0x40;
my $B_08MASK = 0x80;
my $i = 1;
while ($i < 9) {
print "name of var:B_0$i\MASK value:", ?? , "\n";
$i ++;
}
Thanks
Alexander
------------------------------
Date: 15 Apr 2003 05:26:32 -0700
From: saiid@excite.com (S G)
Subject: Re: PHP Code to PERL conversion...
Message-Id: <9e18be67.0304150426.d6981a6@posting.google.com>
Here you go:
Install this..
http://search.cpan.org/author/GAAS/Digest-HMAC-1.01/
and you're off..
#!/usr/bin/perl
use Digest::HMAC_MD5 qw(hmac_md5 hmac_md5_hex);
print "Content-type: text/html\n\n";
$password="password";
$key="key";
print hmac_md5_hex($password, "key");
:)
------------------------------
Date: Tue, 15 Apr 2003 20:27:47 +1000
From: D'oh <D'oh@a.deer>
Subject: Re: problem with this perl script
Message-Id: <3E9BDEA3.54E57A3B@a.deer>
Jim Horne wrote:
>
> <!--#exec cmd="/home/httpd/cgi-bin/Index/makeindex.pl"-->
>
> We now use:
>
> <!--#exec cmd="C:/perl/cgi-bin/Index/makeindex.pl"-->
>
> #!c:/perl/bin/wperl
Just stabbing in the dark for now, but can you do two things
and let us know what happens?
1. After the shebang line add these lines
use strict;
use warnings;
2. Change the line
<!--#exec cmd="C:/perl/cgi-bin/Index/makeindex.pl"-->
to
<!--#exec cmd="C:\perl\cgi-bin\Index\makeindex.pl"-->
------------------------------
Date: Tue, 15 Apr 2003 10:34:09 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Specific Regex Question
Message-Id: <3E9BE001.55E432D1@acm.org>
Cameron wrote:
>
> I have a comma delimited text file with records in the following format:
>
> B4527D,91,3,PONT,BONN,"5,675.00",600,"2,470.27",,,,32103,HM,WO
> ^^^ ^^^
> Can someone show me how to strip the only the commas that fall within quotes?
$ perl -le'
$_ = q/B4527D,91,3,PONT,BONN,"5,675.00",600,"2,470.27",,,,32103,HM,WO/;
print;
1 while s/("\d+),/$1/;
print;
'
B4527D,91,3,PONT,BONN,"5,675.00",600,"2,470.27",,,,32103,HM,WO
B4527D,91,3,PONT,BONN,"5675.00",600,"2470.27",,,,32103,HM,WO
John
--
use Perl;
program
fulfillment
------------------------------
Date: Tue, 15 Apr 2003 11:36:57 +0100
From: John Strauss <john-s@moving-picture.com>
Subject: Re: Specific Regex Question
Message-Id: <20030415113657.0b9409b3.john-s@moving-picture.com>
On Tue, 15 Apr 2003 10:48:19 +0100
"Jeff Snoxell" <Jeff@aetherweb.co.uk> wrote:
>
> > > I have a comma delimited text file with records in the following format:
> > >
> > > B4527D,91,3,PONT,BONN,"5,675.00",600,"2,470.27",,,,32103,HM,WO
> > > ^^^ ^^^
> > > Can someone show me how to strip the only the commas that fall within
> quotes?
> >
> > this seems to work for your sample record.
> > $record =~ s/\"([^\"|,]),/\"$1/g;
>
> This strips the commas within quotes very effectively...
>
thanks. that's what the OP asked for.
> but perhaps in some data the commas are needed.
>
perhaps. perhaps not.
------------------------------
Date: 15 Apr 2003 05:24:56 -0700
From: cam@ntwk.net (Cameron)
Subject: Re: Specific Regex Question
Message-Id: <e4dad21a.0304150424.1ea8fedd@posting.google.com>
"Jeff Snoxell" <Jeff@aetherweb.co.uk> wrote in message news:<b7g9vq$7hp$1@newsg1.svr.pol.co.uk>...
>
> my $text = 'B4527D,91,3,PONT,BONN,"5,675.00",600,"2,470.27",,,,32103,HM,WO';
> $text =~ s/,\"([^\"]*),([^\"]*)\",/,$1\%COMMA\%$2,/g;
> my @items = split(",", $text);
> @items = map { s/\%COMMA\%/,/g; $_; } @items;
> print join("<BR>\n", @items);
>
>
> Not super efficient I guess but it does the trick.
This did exactly what I need:
$data =~ s/,\"([^\"]*),([^\"]*)\",/,$1$2,/g;
Thnx for pointing me in the right direction Jeff.
Cameron
------------------------------
Date: 15 Apr 2003 10:11:24 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Validating user input to match certain characters
Message-Id: <slrnb9nmmc.a5s.abigail@alexandra.abigail.nl>
Mark Smith (Mark.Smith@sonyericsson.com) wrote on MMMDXIV September
MCMXCIII in <URL:news:cf24885e.0304150134.6e49665d@posting.google.com>:
;; I have some Web forms that take user input. I process these in Perl.
;; What I want to do is limit what characters are valid for certain
;; fields. For example if the user creates a new account I would like to
;; restrict the account name to just A-Z, a-z, 0-9, _ and -.
;;
;; I have tried using the following:
;;
;; if ($accountname !~ m/([A-Za-z0-9_-])/)
;; {
;; print "<B>ERROR: invalid chars, please re-enter</B>";
;; }
;;
;; However aslong as it finds at least one valid character the match
;; fails to output the error.
;;
;; So "%abAB" should be invalid as it contains % but will not fail as
;; contains valid characters. Only strings that contain none of the
;; valid characters I've specified will result in the error message being
;; output.
;;
;; I'm sure this is a fairly standard problem and I know I have done this
;; properly in the past but I don't have access to my notes and other
;; code at the moment. Can anyone please help.
Since you want to match if specific characters appear, you can't use !~,
because that indicates the pattern does not appear. So, you have to use =~.
But regexes also have a way to say "a character class containing anything
but the characters I list", and that's by putting a ^ as the first character
of a class. So, you end up with:
if ($accountname =~ /[^A-Za-z0-9_-]/) { ... }
And there's no need to use parenthesis if you are not going to use the
capture.
Abigail
--
CHECK {print "another "}
BEGIN {print "Just " }
END {print "Hacker\n"}
INIT {print "Perl " }
------------------------------
Date: Tue, 15 Apr 2003 10:51:37 +0100
From: Matthew Browning <usenet@NOSPAM.matthewb.org>
Subject: Re: Validating user input to match certain characters
Message-Id: <pan.2003.04.15.10.51.36.373296.895@NOSPAM.matthewb.org>
On Tue, 15 Apr 2003 10:34:09 +0100, Mark Smith wrote:
> I have some Web forms that take user input. I process these in Perl.
> What I want to do is limit what characters are valid for certain fields.
> For example if the user creates a new account I would like to restrict
> the account name to just A-Z, a-z, 0-9, _ and -.
>
> I have tried using the following:
>
> if ($accountname !~ m/([A-Za-z0-9_-])/) {
> print "<B>ERROR: invalid chars, please re-enter</B>";
> }
>
> However aslong as it finds at least one valid character the match fails
> to output the error.
>
Your brackets are superfluous, unless you want to capture the match, and
you are only asking to match one char. Try something like this:
if ($accountname !~ m/[A-Za-z0-9_-]+/) {
print "<strong>ERROR: invalid chars, please re-enter</strong>"
}
[MB]
------------------------------
Date: Tue, 15 Apr 2003 13:15:58 +0100
From: Matthew Browning <usenet@NOSPAM.matthewb.org>
Subject: Re: Validating user input to match certain characters
Message-Id: <pan.2003.04.15.13.15.57.318015.1428@NOSPAM.matthewb.org>
On Tue, 15 Apr 2003 11:11:24 +0100, Abigail wrote:
> Since you want to match if specific characters appear, you can't use !~,
> because that indicates the pattern does not appear. So, you have to use
> =~. But regexes also have a way to say "a character class containing
> anything but the characters I list", and that's by putting a ^ as the
> first character of a class. So, you end up with:
>
> if ($accountname =~ /[^A-Za-z0-9_-]/) { ... }
>
> And there's no need to use parenthesis if you are not going to use the
> capture.
>
Hmm. My previous answer was incorrect since the + will match even if the
whole string does not conform to the rules of the exression. Apologies.
The above solution is obviously the way to do it, but I feel that the
issue could also be solved by a more specific quantifier.
You could[1] capture the length of the string entered and then use that
as the quantifier in the expression. For example, this works:
#!/usr/bin/perl -w
use strict;
my $input = q[abshkjh-h1g4fd3]; #for example
my $length = sub { return my @length = split( //, $input, 0 ) };
my $quant = scalar(&$length);
if( $input !~ /([a-zA-Z0-9_-]{$quant})/ ) {
print qq[<strong>ERROR: invalid chars, please re-enter</strong>\n]
}
else { print qq[$1 is a nice password\n] }
#ends
Now, I appreciate that you wouldn't particularly want to do that;
however, there are instances, such as the validation of user passwords,
where it is desirable to ascertain the length of the entry. Using
something like this, you might want to encapsulate both jobs into a
subroutine or something.
[MB]
1. If you were a lunatic.
------------------------------
Date: Tue, 15 Apr 2003 13:55:58 +0100
From: "Bigus" <bigus AT creationfactor DOT net>
Subject: WIN32::ODBC problem
Message-Id: <b7gvgv$sc8@newton.cc.rl.ac.uk>
Hi
I just wanted to do (what was supposed to be) a quick adminmistrative task
on a DB, involving changing a flag in an Access DB for about 500 records. As
I'm more familiar with WIN32::ODBC than DBI, I knocked up the following:
======================
# establish DB connection
use Win32::ODBC;
$db = new Win32::ODBC("DSN=jiscmaildb");
# select records to amend
if(defined $db->Sql("SELECT * FROM list_details WHERE DateDeleted IS NOT
NULL AND Deleted = 0")){print "Error selecting records:
".$db->error()."\n";}
# Amend each record
while($db->FetchRow())
{
$count++;
@therow = $db->Data();
if(defined $db->Sql("UPDATE list_details SET Deleted = '1' WHERE listname
= '$therow[1]'")){print "Error amending record $therow[1]:
".$db->error()."\n";}
}
$db->Close();
print "$count records amended";
======================
All it's supposed to do is select the 500 or so records that match the
criteria and then change a column in each one.
Now, it works for the first record it finds, but then just exits the while()
loop and displays "1 records amended". If I comment out the "UPDATE
list_details..." line in the while() loop, it finishes and displays "500
records amended". So, why is the "UPDATE list_details..." line causing it to
exit the loop and what's teh bets way of getting round it?
I'm surprised I've not come across this problem before.. which makes me
worry about some of my previous WIN32::ODBC scripts!
Thanks
Bigus
------------------------------
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 4848
***************************************