[25267] in Perl-Users-Digest
Perl-Users Digest, Issue: 7512 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 12 14:10:31 2004
Date: Sun, 12 Dec 2004 11:10:09 -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 Sun, 12 Dec 2004 Volume: 10 Number: 7512
Today's topics:
Re: Newbie questions, migrating from c++ <spamtrap@dot-app.org>
Re: Newbie questions, migrating from c++ <1usa@llenroc.ude.invalid>
Re: Newbie questions, migrating from c++ <mritty@gmail.com>
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ <spamtrap@dot-app.org>
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ <spamtrap@dot-app.org>
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ <spamtrap@dot-app.org>
Re: Newbie questions, migrating from c++ <barbr-en_delete_@online.no.invalid>
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ <1usa@llenroc.ude.invalid>
Re: Newbie questions, migrating from c++ <barbr-en_delete_@online.no.invalid>
Re: Newbie questions, migrating from c++ <1usa@llenroc.ude.invalid>
Re: Newbie questions, migrating from c++ <barbr-en_delete_@online.no.invalid>
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ <mritty@gmail.com>
Re: Newbie questions, migrating from c++ <joe@inwap.com>
Re: Newbie questions, migrating from c++ <spamtrap@dot-app.org>
Re: Newbie questions, migrating from c++ gg500@lycos.com
Re: Newbie questions, migrating from c++ <matthew.garrish@sympatico.ca>
Re: Obtaining length of binary string <1usa@llenroc.ude.invalid>
Re: replace some words in a file with a perl script. <tadmc@augustmail.com>
Re: sprintf rounding puzzlements <junk@blackwater-pacific.com>
Re: sprintf rounding puzzlements <junk@blackwater-pacific.com>
Re: sprintf rounding puzzlements <joe@inwap.com>
Re: sprintf rounding puzzlements <spamtrap@dot-app.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 12 Dec 2004 09:20:31 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <kMKdndaSo5wtziHcRVn-vg@adelphia.com>
Lots of different questions here - I'll answer a few of the ones I have
answers for off the top of my head.
gg500@lycos.com wrote:
> Few things puzzles me in perl. I'm trying to use warnings and strict
> and run with perl -w, but I get warnings which I do not understand. For
Yes, but you're halfway there - you're enabling strict and warnings, and
making an effort to understand their output. That's a good habit to
have, and it's far too rare.
> example I have to use "use Fcntl qw(:DEFAULT :seek);" in order to get
> no warnings using SEEK_SET etc, this phrase I copied got from net while
> not understanding what it means. Could you describe this statement?
Which part of it?
qw() is a convenient way to declare an array by splitting some text on
white space - the above is equivalent to "use Fcntl (':DEFAULT', ':seek');
See "Quote and Quote-like Operators", in:
perldoc perlop
When you pass an array as the second argument to a use(), you're asking
the Exporter module to export symbols from the module you're using into
the current module's name space. If you didn't do that, you'd have to
fully-qualify the package names of the functions you call, instead of
being able to simply call them with the function name alone.
There's more to it than that - for example, the ':DEFAULT' above is
actually exporting a group of symbols, not a single symbol by that name.
But that's the general idea of it.
See also:
perldoc -f use
perldoc Exporter
> Is there some other useful things which I can set to make warnings as
> explanatory and abundant as possible? I mean something equal to gcc
> -Wall -pedantic.
use diagnostics;
See also:
perldoc diagnostics
> If I write "split /\./, $ENV(REMOTE_ADDR)"
Don't do that. Use CGI.pm instead.
> I'm also a bit confused with calling functions or "sub routines".
> Writing "&subroutine();" works but I do not understand what exactly is
> the difference when omitting "()" and/or "&"...?
See:
perldoc perlsub
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 12 Dec 2004 14:44:28 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <Xns95BD631845B4Easu1cornelledu@132.236.56.8>
gg500@lycos.com wrote in news:1102858622.622328.166600
@c13g2000cwb.googlegroups.com:
> Few things puzzles me in perl. I'm trying to use warnings and strict
> and run with perl -w,
use warnings;
is preferable these days.
> but I get warnings which I do not understand.
Then this would be a great time to read the posting guidelines for this
group and note
use diagnostics;
> For example I have to use "use Fcntl qw(:DEFAULT :seek);" in order
> to get no warnings using SEEK_SET etc, this phrase I copied got
> from net while not understanding what it means. Could you describe
> this statement?
I am not in the habit of explaining random statements from "the net".
If
use strict;
is in effect, symbols need to be declared before being used. The Fcntl
module declares and defines those symbols. the use statement above then
imports them into your scripts namespace.
> Is there some other useful things which I can set to make warnings as
> explanatory and abundant as possible? I mean something equal to gcc
> -Wall -pedantic.
use diagnostics;
> If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using
That is not how you access a hash element.
> undefined variable, so without knowing how exactly I should proceed I
> have written abundant amount of lines like "defined $ENV{REMOTE_ADDR} ?
> $ENV{REMOTE_ADDR} : """.
What is """ supposed to do?
> It must have taken me 10 hours to try to get information how to refer
> single byte (char) in "string/array", like in C: char a[5]; return
> a[2]; without realizing that's not how things are done in perl. Is this
> correct? So if I'm to do something with chars in string (or perl
> variable that is) I am to split the variable to @array first? Does this
> not impose performance problems in general?
Please give an example where you think it is necessary to pretend a Perl
scalar is a C char array, then we can comment on whether that is the
right way in that particular case.
> I'm also a bit confused with calling functions or "sub routines".
> Writing "&subroutine();" works but I do not understand what exactly is
> the difference when omitting "()" and/or "&"...?
&subroutine() has specific effects which are explained in
perldoc perlsub
It is not a good idea to use this method of subroutine invocation unless
you know these effects and specifically need them.
You can invoke your subroutine without the parantheses under certain
circumstances. For example:
#! perl
use strict;
use warnings;
sub no_need {
print "Don't need no stinking parantheses to call me\n"
}
no_need;
need();
sub need {
no_need;
}
__END__
Again, please read the documentation first and ask for clarifications.
> Also do I need to flock UN_LOCK before closing the file
No.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sun, 12 Dec 2004 10:17:09 -0500
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <cphnec$edk$1@misc-cct.server.rpi.edu>
gg500@lycos.com wrote:
I'll comment on the one question Sherm did not...
> It must have taken me 10 hours to try to get information how to refer
> single byte (char) in "string/array", like in C: char a[5]; return
> a[2]; without realizing that's not how things are done in perl. Is this
> correct?
Yes, in Perl, a string is a scalar datatype. A string is not created
nor stored as an array of characters as it is in C++. This also means
that there is no internal difference between:
$str = 'a';
and
$str = 'hello world.';
The only difference is that one string contains one character, while the
other contains 12.
> So if I'm to do something with chars in string (or perl
> variable that is) I am to split the variable to @array first? Does this
> not impose performance problems in general?
If you really *need* to operate on one single character from a string,
then yes, you generally split the string into an array of characters.
And yes, this is probably inefficient.
*However*, Perl contains so many features for dealing with strings as a
whole (regular expressions, and the l-value return value of substr, for
example), that is almost never necessary to operate on a specific
character. If you have a situation in which you find yourself wanting
to modify/read/delete single characters from an existing string, it's
likely that you're either ignoring or unaware of a Perl feature that is
better suited to your task. If you have one of these situations
happening right now, feel free to post an example to this group, and
someone will probably be able to show you a better way.
Paul Lalli
------------------------------
Date: 12 Dec 2004 08:51:34 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102870294.063203.250760@c13g2000cwb.googlegroups.com>
> Sherm Pendley wrote:
> When you pass an array as the second argument to a use(), you're
asking the Exporter module to export symbols from the module you're
using into the
> ...See also:
> perldoc Exporter
Thanks, this was helpful.
>> If I write "split /\./,$ENV(REMOTE_ADDR)"
> Don't do that. Use CGI.pm instead.
It's not my intention to learn the libraries yet. I try to understand
how to do this right.
------------------------------
Date: 12 Dec 2004 09:05:48 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102871148.054841.299940@f14g2000cwb.googlegroups.com>
A. Sinan Unur wrote:
> Then this would be a great time to read the posting guidelines for
this group and note
Why bother because no matter how newbies put their questions, there are
people like you who will point out things out of context what the
poster got wrong. Typical usenet replies.
> I am not in the habit of explaining random statements from "the net".
...
>> If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using
> That is not how you access a hash element.
...
>> have written abundant amount of lines like "defined
$ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : """.
> What is """ supposed to do?
You know perfectly well there was quotes around the whole piece of
code. You are again increasing the usenet noise by stupid writing like
this, which does not answer the question or do anything good for the
group.
> Please give an example where you think it is necessary to pretend a
Perl scalar is a C char array, then we can comment on whether that is
the right way in that particular case.
Again, that has little to do with the question of confirming is there
way to do this in perl. The answer was confirmed by other posters.
Thanks for help, although 50% of your post was just plain arrogance.
------------------------------
Date: Sun, 12 Dec 2004 12:15:19 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <xrmdnbVwNdk64SHcRVn-ug@adelphia.com>
gg500@lycos.com wrote:
> It's not my intention to learn the libraries yet. I try to understand
> how to do this right.
Using CGI.pm *is* the right way to parse form input.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 12 Dec 2004 09:25:36 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102872336.518853.50250@f14g2000cwb.googlegroups.com>
Paul Lalli wrote:
>> So if I'm to do something with chars in string (or perl
>> variable that is) I am to split the variable to @array first? Does
this
>> not impose performance problems in general?
>If you really *need* to operate on one single character from a string,
> then yes, you generally split the string into an array of characters.
> And yes, this is probably inefficient.
> *However*, Perl contains so many features for dealing with strings as
a
> whole (regular expressions, and the l-value return value of substr,
for
> example), that is almost never necessary to operate on a specific
> character. If you have a situation in which you find yourself wanting
> to modify/read/delete single characters from an existing string, it's
> likely that you're either ignoring or unaware of a Perl feature that
is
> better suited to your task. If you have one of these situations
> happening right now, feel free to post an example to this group, and
> someone will probably be able to show you a better way.
Thanks for the reply. You are right that such single character
modifications aren't probably necessary if one knows perl. I presumed
this but it's difficult to master the regex and whole tools, and when I
try to accomplish something for different ways and spent time reading
references to do something simple, I end up trying to modify single
characters. I originally got stuck while trying to the number of
whitespaces in the beginning of string. Now I can think (out of my
head) at least one way to do it, which is something like this:
$i=0;while(/^ /){++$i;} and how i holds the number of spaces, but I
didn't figure this back then.
To give you an example what I still don't know how to do. I could think
of some kind of simple keyed rotation (in C):
for(i=0,j=strlen(s);i<j;++i)s[i]=(s[i]+key[i%keylen])%m;
Note that I do not need exactly solution for the above, that is just an
example of similar situations which I found myself in. I know this is
mainly because I just have not used to the powerful features which are
the core of perl and try to solve things in a low level way.
------------------------------
Date: 12 Dec 2004 09:35:13 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102872913.176850.86550@f14g2000cwb.googlegroups.com>
I do not understand. Are you saying that the situation is so unique
that there is no other situation where I need to split entry from
hashed array?
------------------------------
Date: Sun, 12 Dec 2004 12:47:44 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <btCdnUiyGrjdGSHcRVn-ow@adelphia.com>
gg500@lycos.com wrote:
> A. Sinan Unur wrote:
>
>>Then this would be a great time to read the posting guidelines for
>
> Why bother
So you don't make a fool of yourself in public. So you don't find
yourself in the killfiles of those who are best qualified to answer your
questions.
If nothing else, out of basic courtesy.
> Typical usenet replies.
If you consistently get nasty replies to your usenet posts, perhaps you
should be asking yourself what you're doing to attract them.
>>Please give an example where you think it is necessary to pretend a
>>Perl scalar is a C char array, then we can comment on whether that is
>>the right way in that particular case.
>
> Again, that has little to do with the question of confirming is there
> way to do this in perl.
Nonsense. While it's *possible* write Perl that emulates C's "char at a
time" approach to string processing, it's rarely necessary. More often
than not, there's an easier way to do the same job that treats the
string as a single unit.
For example, consider converting a string to uppercase. In C you'd loop
through the array and examine in character in turn, converting it as
necessary. In Perl you'd simply use the built-in uc() function.
A. Sinan didn't give you noise, he gave you good advice. Arrogance is
what you're giving him back, by flaming him in return for his help.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 12 Dec 2004 09:53:33 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102874013.327419.300190@z14g2000cwz.googlegroups.com>
Just to correct myself, my space counting code does not work. But I
tried this and it did work:
$i=1;while (/^ {$i}/) {++$i;}--$i;
But this didn't work:
$i=0;while (/^ {$i+1}/) {++$i;}
which needless to say I do not understand.
------------------------------
Date: 12 Dec 2004 09:57:50 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102874270.733419.181610@c13g2000cwb.googlegroups.com>
Sherm Pendley wrote:
> A. Sinan didn't give you noise, he gave you good advice. Arrogance is
what you're giving him back, by flaming him in return for his help.
If you honestly didn't recognize his reply as arrogant then I suggest
you take another look.
------------------------------
Date: Sun, 12 Dec 2004 12:58:21 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <aPOdnY7P6uwgGyHcRVn-gw@adelphia.com>
gg500@lycos.com wrote:
> I do not understand. Are you saying that the situation is so unique
> that there is no other situation where I need to split entry from
> hashed array?
Don't be absurd, I made no such gross over-generalization.
In your example, you tried to read the environment variable REMOTE_ADDR.
That implies that you're writing a CGI script and you want the remote
host address. In that case, the normal way to get it is to call the
remote_host() method in CGI.pm.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sun, 12 Dec 2004 19:05:02 +0100
From: Kåre Olai Lindbach <barbr-en_delete_@online.no.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <rn1pr01p2i6vugrtdj077v8pvk4d6mktds@4ax.com>
On 12 Dec 2004 09:53:33 -0800, gg500@lycos.com wrote:
>Just to correct myself, my space counting code does not work. But I
>tried this and it did work:
>$i=1;while (/^ {$i}/) {++$i;}--$i;
>But this didn't work:
>$i=0;while (/^ {$i+1}/) {++$i;}
>which needless to say I do not understand.
use strict;
use warnings:
$_ = ' string with spaces first';
s/^\s+//;
print "$_\n";
If you want to migrate from C++ to Perl, why don't you start reading
up/learing those thing that are done more easily in Perl than C++
first? Like (string-) variables and regexes and similar...?
--
mvh/Regards Kåre Olai Lindbach
(News: Remove '_delete_' and '.invalid')
(HTML-written email from unknown will be discarded)
------------------------------
Date: 12 Dec 2004 10:08:25 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102874905.963771.220910@f14g2000cwb.googlegroups.com>
Right, I plan to make full use of CGI.pm, this is not the point. If we
take a look at the origin of this case I was referring to warnings got
by undefined arguments perl says are undefined and I have to add these
defined() conditions which seem like overhead. I asked about these
warnings, and I'm not at all asking about some specific variable in my
example.
------------------------------
Date: 12 Dec 2004 10:14:47 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102875287.797730.244320@f14g2000cwb.googlegroups.com>
K=E5re Olai Lindbach wrote:
> $_ =3D ' string with spaces first';
> s/^\s+//;
> print "$_\n";
But the code you propose cuts the spaces from the beginning of the line
and does not solve the problem. I was posing a problem which I needed
to know how many spaces there were.
> If you want to migrate from C++ to Perl, why don't you start reading
> up/learing those thing that are done more easily in Perl than C++
first? Like (string-) variables and regexes and similar...?
A good suggestion, but unfortunately the lack of patience leads to
wanting to learn all at once.
------------------------------
Date: 12 Dec 2004 18:19:03 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <Xns95BD87790B9Easu1cornelledu@132.236.56.8>
gg500@lycos.com wrote in
news:1102871148.054841.299940@f14g2000cwb.googlegroups.com:
> A. Sinan Unur wrote:
>> Then this would be a great time to read the posting guidelines for
>> this group and note
>
> Why bother because no matter how newbies put their questions,
I am not sure what you are saying here. The answer to "Why bother reading
the guidelines?" is that they mention the diagnostics package and assorted
niceties involved in posting here:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html#posting_t
o_comp.lang.perl.misc (http://tinyurl.com/466lp if you have problems
opening that).
>> I am not in the habit of explaining random statements from "the net".
Exactly. There are good Perl books and Perl comes with excellent
documentation which you would do well to read. It is natural that you would
still have questions after having read the docs but then you would be in a
position to ask better questions.
>>> If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using
>> That is not how you access a hash element.
Indeed, that is not how you access a hash element. We can only comment on
what you wrote.
>>> have written abundant amount of lines like "defined
>>> $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : """.
>> What is """ supposed to do?
>
> You know perfectly well there was quotes around the whole piece of
> code.
No, I don't know perfectly well. I did not see the " mark before the
defined there. If you want to minimize errors such as this one, you might
want to help your readers by formatting your post better:
<example>
have written abundant amount of lines like
defined $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : "";
Obviously I do not understand ...
</example>
> You are again increasing the usenet noise by stupid writing
I am happy with my IQ.
>> Please give an example where you think it is necessary to pretend a
>> Perl scalar is a C char array, then we can comment on whether that is
>> the right way in that particular case.
>
> Again, that has little to do with the question of confirming is there
> way to do this in perl.
It has everything to do with it. Just recently, I found myself doing it in
response to another post (see http://tinyurl.com/5m6wt).
> The answer was confirmed by other posters.
Which answer?
Sinan.
------------------------------
Date: Sun, 12 Dec 2004 19:25:46 +0100
From: Kåre Olai Lindbach <barbr-en_delete_@online.no.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <203pr0hms8oafn5d8r56kec146pkpcemnb@4ax.com>
On 12 Dec 2004 10:14:47 -0800, gg500@lycos.com wrote:
>K? Olai Lindbach wrote:
>> $_ = ' string with spaces first';
>> s/^\s+//;
>> print "$_\n";
>
>But the code you propose cuts the spaces from the beginning of the line
>and does not solve the problem. I was posing a problem which I needed
>to know how many spaces there were.
Sorry, I got so upset with earlier questions in this thread that I
didn't get this question/problem correctly... ;-)
use strict;
$_ = ' string with spaces first';
if(/^(\s+)/) {
print length($1),"\n";
}
or using a decent variable name:
$atext = ' string with spaces first';
if($atext =~ /^(\s+)/) {
print length($1),"\n";
}
This is not the most concise way, I believe.
--
mvh/Regards Kåre Olai Lindbach
(News: Remove '_delete_' and '.invalid')
(HTML-written email from unknown will be discarded)
------------------------------
Date: 12 Dec 2004 18:30:11 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <Xns95BD895C0B112asu1cornelledu@132.236.56.8>
gg500@lycos.com wrote in
news:1102874013.327419.300190@z14g2000cwz.googlegroups.com:
> Just to correct myself, my space counting code does not work. But I
> tried this and it did work:
> $i=1;while (/^ {$i}/) {++$i;}--$i;
> But this didn't work:
> $i=0;while (/^ {$i+1}/) {++$i;}
> which needless to say I do not understand.
I am not even sure what you mean by "work" here. On the other hand, you
could find a better way by actually reading
perldoc perlop
and thinking about this a little instead of attempting write C in Perl.
use strict;
use warnings;
my $s = ' sdfj sdlkfj sdkfj sdkfj sdfk lk';
my $i = 0;
# Writing C in Perl can be fun
++$i while substr($s, $i, 1) eq ' ';
print "$i\n";
# One way to do it with a regex
my ($spaces) = ($s =~ /^( *)/);
print length($spaces), "\n";
Sinan
------------------------------
Date: Sun, 12 Dec 2004 19:29:28 +0100
From: Kåre Olai Lindbach <barbr-en_delete_@online.no.invalid>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <id3pr0lb4hfai8b6c3d53l4i7rp71unvrn@4ax.com>
On Sun, 12 Dec 2004 19:25:46 +0100, Kåre Olai Lindbach
<barbr-en_delete_@online.no.invalid> wrote:
>use strict;
[snipp]
>or using a decent variable name:
>
>$atext = ' string with spaces first';
>
>if($atext =~ /^(\s+)/) {
> print length($1),"\n";
>}
This last one wount compile without a 'my'. Where to put it is left as
an exercise...
--
mvh/Regards Kåre Olai Lindbach
(News: Remove '_delete_' and '.invalid')
(HTML-written email from unknown will be discarded)
------------------------------
Date: 12 Dec 2004 10:41:31 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102876890.974390.30480@c13g2000cwb.googlegroups.com>
Thanks, that seems is the correct way. However I get those warnings
which I have mentioned and I have to add the weird condition not to
receive warning:
/^(\s+)/;
$i=defined ($1) ? length($1) : 0;
If I write:
/^(\s+)/;
$i=length($1);
I get "Use of uninitialized value in length..."
------------------------------
Date: Sun, 12 Dec 2004 13:49:31 -0500
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <cpi3sj$f6i$2@misc-cct.server.rpi.edu>
gg500@lycos.com wrote:
> Thanks, that seems is the correct way. However I get those warnings
> which I have mentioned and I have to add the weird condition not to
> receive warning:
>
> /^(\s+)/;
> $i=defined ($1) ? length($1) : 0;
>
> If I write:
>
> /^(\s+)/;
> $i=length($1);
>
> I get "Use of uninitialized value in length..."
That's because you snipped a very important part of the code:
if(/^(\s+)/) {
print length($1),"\n";
}
You should never ever use $1, $2, etc without ensuring that the pattern
match succeeded. A better way to write this might be:
$len = (/^(\s+)/ ? length($1) : 0);
print "$len space characters start the string\n";
Paul Lalli
------------------------------
Date: Sun, 12 Dec 2004 18:50:43 GMT
From: Joe Smith <joe@inwap.com>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <7q0vd.182514$V41.64075@attbi_s52>
gg500@lycos.com wrote:
> If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using
> undefined variable, so without knowing how exactly I should proceed I
> have written abundant amount of lines like "defined $ENV{REMOTE_ADDR}
You need to use the correct punctuation.
$ENV is not the same a %ENV.
$ENV(function_arguments) is not the same as $ENV{hash_key}.
> It must have taken me 10 hours to try to get information how to refer
> single byte (char) in "string/array"
You'll find this spelled out in any decent book on Perl.
Typically perl programs process strings using regular expressions
instead of one character at a time.
> migrating from c++
Use the command
perldoc perltrap
and read the section "C programmers should take note of ...".
-Joe
------------------------------
Date: Sun, 12 Dec 2004 13:51:04 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <vNKdnWpvDbuFDiHcRVn-hw@adelphia.com>
gg500@lycos.com wrote:
> Thanks, that seems is the correct way. However I get those warnings
> which I have mentioned and I have to add the weird condition not to
> receive warning:
>
> /^(\s+)/;
> $i=defined ($1) ? length($1) : 0;
$1 is only valid if the preceding match succeeded. If you want to avoid
the defined(), you could write it like this:
my $i = (/^(\s+)/) ? length($1) : 0;
You could also avoid the call to length(). Just use the offset within
the string at the end of the match, which is in @+:
my $i = (/^\s+/) ? $+[0] : 0;
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 12 Dec 2004 10:59:19 -0800
From: gg500@lycos.com
Subject: Re: Newbie questions, migrating from c++
Message-Id: <1102877959.019978.88720@f14g2000cwb.googlegroups.com>
Paul Lalli wrote:
> You should never ever use $1, $2, etc without ensuring that the
pattern
> match succeeded. A better way to write this might be:
Ok, thanks. I understand now. I must study what other similar cases of
warnings this explains (probably most of them).
------------------------------
Date: Sun, 12 Dec 2004 13:47:50 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Newbie questions, migrating from c++
Message-Id: <mn0vd.1446$pb.78124@news20.bellglobal.com>
<gg500@lycos.com> wrote in message
news:1102874013.327419.300190@z14g2000cwz.googlegroups.com...
> Just to correct myself, my space counting code does not work. But I
> tried this and it did work:
> $i=1;while (/^ {$i}/) {++$i;}--$i;
> But this didn't work:
> $i=0;while (/^ {$i+1}/) {++$i;}
> which needless to say I do not understand.
>
Both are ugly, in my opinion, since you keep trying the regex until it
fails:
s/^(\s+)/print length($1)/e;
Also, your second code snippet doesn't work because you can't perform
addition within a matching regex like that (only interpolation, so your code
becomes /^ {1 + 0}/, for example, and never matches).
Matt
------------------------------
Date: 12 Dec 2004 15:07:38 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Obtaining length of binary string
Message-Id: <Xns95BD670566334asu1cornelledu@132.236.56.8>
Ben Morrow <usenet@morrow.me.uk> wrote in
news:aurm82-vqi.ln1@osiris.mauzo.dyndns.org:
> Quoth "A. Sinan Unur" <1usa@llenroc.ude.invalid>:
>> use strict;
>> use warnings;
>>
>> use bytes ();
>>
>> my $s = "\x{2022}";
>>
>> print bytes::length($s), "\n", length($s);
>
> This will give you the length in bytes of "\x{2022}" represented in
> perl's internal character encoding. THIS IS NOT A USEFUL VALUE.
Thank you for the correction.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sun, 12 Dec 2004 08:27:37 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: replace some words in a file with a perl script.
Message-Id: <slrncrolap.tnu.tadmc@magna.augustmail.com>
Wang Penghui <wangpenghui@realss.com> wrote:
> I am a newbie with perl. I have met a pazzle now.
Your Question is Asked Frequently.
How do I change one line in a file/
delete a line in a file/
insert a line in the middle of a file/
append to the beginning of a file?
> I have a file with thousands of lines.
But your code only reads ONE of those lines!
> @instead=split(/\t/,<ZH>);
This is the only input in your code, it reads one line.
> open (ZH,">file") || die "could not open filename!"
So this will stomp over the file contents with one line.
> foreach (@instead) {
> s/original/changed/g
^^
^^
Syntax error.
It is pretty rude of you to not post Real Perl Code.
Have you seen the Posting Guidelines that are posted here frequently?
> print ZH $_;
> };
> This script would replace all the words matched in each field.
Have you run that program?
Didn't you notice the one-line thing already?
> But it's
> not what i want to get.
> Anyone could pick me up?
If you want to s/// on only one element in @instead then don't
loop over ALL of the elements of @instead.
Replace the foreach with:
$instead[3] =~ s/original/changed/g;
print ZH $_ foreach @instead; # but don't you want to put the
# tab chars back in?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 12 Dec 2004 10:25:25 -0800
From: Steve May <junk@blackwater-pacific.com>
Subject: Re: sprintf rounding puzzlements
Message-Id: <10rp3106ve5le84@corp.supernews.com>
Jürgen Exner wrote:
> Steve May wrote:
>
>>I ran across something today that has me scratching my head.
>>
>>What I was doing was playing with control of the rounding
>>that occurs when using sprintf to set decimal places and
>>*when* it rounds up. (hate to leave money on the table)
>
> [...]
>
>>What am I missing? Or is this a known issue? Or???
>
>
> I didn't read all those gazillion examples (would be nice if you would have
> used two or three to make your point) but is there anything that is not
> explained in 'perldoc -q 999'?
>
> jue
>
>
Thought about that, but decided someone would wonder if 25.555 was the
only number with that behavior. So I stuck the full run in and <-NOTE
indicators at the areas of interest in the output.
Yes I read perldoc -q 999, but that didn't really clarify matters...
However, Sherm's suggestion of reading perldoc perlnumber DID help a bit.
That's the problem with being a self-mistaught hacker, sometimes basic
concepts are not as clear as they should be.
thanks,
\s
------------------------------
Date: Sun, 12 Dec 2004 10:28:18 -0800
From: Steve May <junk@blackwater-pacific.com>
Subject: Re: sprintf rounding puzzlements
Message-Id: <10rp36dr3ssnu11@corp.supernews.com>
Sherm Pendley wrote:
> Steve May wrote:
>
>> <code>
>>
>> #! /usr/bin/perl -w
>
>
> use warnings; # This is preferred over -w for new code
Depending on what you want to do, yes.
>
>> use strict;
>>
>> my $one = 10;
>> my $two = 2.555;
>>
>> for(2..6){
>> my $test = "$two$_";
>> &float_product( $one, $test );
>
>
> # The use of '&' to call subs has been deprecated for years now.
> # Don't use it for Perl 5 unless you know what it's doing.
> #
> # See: 'perldoc perlsub'
>
> float_product( $one, $test );
>
>> }
>>
>> exit;
>
>
> # exit() at the end of a script is useless.
>
>> sub float_product {
>> my( $one, $two ) = @_;
>> my $product = $one * $two;
>> print "raw product of $one x $two is '$product'\n";
>> $product += .001; # this line commented out for second run
>> print "product to sprintf is '$product'\n";
>> $product = sprintf("%.2f",$product);
>> print "Multiply $one by $two result: $product\n\n";
>> } # endo sub float_product
>>
>> </code>
>>
>> Results of first run where .001 is added to product:
>
>
> ... snip ...
>
>> I've spent some time with the docs, but seem to be missing something
>> fundamental and for the life of me, I can't spot it......
>>
>> For crying out loud, this is a pretty straight up use of sprintf.
>>
>> What am I missing? Or is this a known issue? Or???
>
>
> It's a known issue that relates to how computers represent floating
> point numbers - it's not specific to Perl.
>
Knew about float problems, but couldn't see the problem with this
specific example.
> See:
>
> 'perldoc -q 999'
> 'perldoc perlnumber'
>
> Additionally, keep in mind that because of how floating point numbers
> are stored as a mantissa and exponent, addition and subtraction will
> especially aggravate the issue.
>
This bit here cleared things up immensely. Thanks.
\s
------------------------------
Date: Sun, 12 Dec 2004 18:30:47 GMT
From: Joe Smith <joe@inwap.com>
Subject: Re: sprintf rounding puzzlements
Message-Id: <r70vd.748446$8_6.438239@attbi_s04>
Steve May wrote:
> Sherm Pendley wrote:
>>> #! /usr/bin/perl -w
>>
>> use warnings; # This is preferred over -w for new code
>
> Depending on what you want to do, yes.
What do you mean there? When would you use '-w' for new code?
-Joe
------------------------------
Date: Sun, 12 Dec 2004 13:39:05 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: sprintf rounding puzzlements
Message-Id: <E9GdnXW2L7fUDSHcRVn-sQ@adelphia.com>
Joe Smith wrote:
> When would you use '-w' for new code?
When you're writing it for an old Perl. :-)
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
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 7512
***************************************