[16917] in Perl-Users-Digest
Perl-Users Digest, Issue: 4329 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 15 00:11:06 2000
Date: Thu, 14 Sep 2000 21:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <968991014-v9-i4329@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 14 Sep 2000 Volume: 9 Number: 4329
Today's topics:
Re: sprintf() rounding problem (Abigail)
Re: sprintf() rounding problem (Abigail)
Substring Golf? <wyzelli@yahoo.com>
Re: Substring Golf? <uri@sysarch.com>
Re: Substring Golf? (Tom Vaughan)
Re: Substring Golf? (Mike Stok)
Re: Substring Golf? <jeffp@crusoe.net>
Re: Substring Golf? (Craig Berry)
Re: Substring Golf? (Martien Verbruggen)
Re: Teaching Perl <christopher_j@uswest.net>
Re: Teaching Perl <christopher_j@uswest.net>
Re: Unexpected behavior of shift in a loop (Joe Smith)
Re: UNICODE & SSI <christopher_j@uswest.net>
Were to get a sendmail for a Win98 SE ? <agustinchernitsky@altavista.com>
Re: Were to get a sendmail for a Win98 SE ? <wyzelli@yahoo.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 15 Sep 2000 02:41:10 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sprintf() rounding problem
Message-Id: <slrn8s3305.4mc.abigail@alexandra.foad.org>
David Hugh-Jones (davidhj@mail.com) wrote on MMDLXXI September MCMXCIII
in <URL:news:968934567.255958610@news.uklinux.net>:
''
'' I assume the problem is that weird floating point arithmetic stuff is
'' making 6698 be really a bit less than 6698 before it gets integerized. But I
'' don't know how to work around this. One option that comes up is "do everythin
'' in cents so you are working with integers internally". Unfortunately, I can't
'' do that, because my program takes external input in the form of dollars and
'' cents (or pounds and pence). It would be too difficult to make users calculat
'' everything in cents. So, I have to use floating point numbers. Is there a way
'' of doing rounding while ensuring that 0.5 always gets rounded up?
It's highly, highly unlikely your users are inputting the numbers as
floating point numbers and are not willing to switch to character based
input. In fact, I'm quite certain your users are using characters to
enter the numbers.
Abigail
--
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"
__END__
A rabbit watches near // the forest. A beetle over // a pool. A Bishop.
------------------------------
Date: 15 Sep 2000 02:43:34 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sprintf() rounding problem
Message-Id: <slrn8s334l.4mc.abigail@alexandra.foad.org>
Larry Rosler (lr@hpl.hp.com) wrote on MMDLXXI September MCMXCIII in
<URL:news:MPG.142ac95c2539edad98ad67@nntp.hpl.hp.com>:
//
// Why do the users have to calculate everything in cents? Multiply their
// input by 100 immediately, work with cents internally, then convert back
// on output.
Multipying by 100 forces numerical conversion. At that moment, the bad
roundoff might already happen.
The input comes as characters. Remove the decimal point with a regex
and only then start using it as a number.
Abigail
--
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";
------------------------------
Date: Fri, 15 Sep 2000 10:36:42 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Substring Golf?
Message-Id: <hBew5.16$eI1.3742@vic.nntp.telstra.net>
I am looking for a neater/shorter way of achieving the following:
my $num = shift;
$num =~ s/ //g;
my $numa = substr $num,0,4;
my $numb = substr $num,4,4;
my $numc = substr $num,8,4;
my $numd = substr $num,12,4;
Any ideas? One liners?
Wyzelli
------------------------------
Date: Fri, 15 Sep 2000 01:26:30 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Substring Golf?
Message-Id: <x7ya0ufnsq.fsf@home.sysarch.com>
>>>>> "W" == Wyzelli <wyzelli@yahoo.com> writes:
W> I am looking for a neater/shorter way of achieving the following:
W> my $num = shift;
W> $num =~ s/ //g;
if you are just deleting all spaces, tr/// is faster.
W> my $numa = substr $num,0,4;
W> my $numb = substr $num,4,4;
W> my $numc = substr $num,8,4;
W> my $numd = substr $num,12,4;
you haven't specified the input format in detail. is each 4 char field
just digits? are there multiple spaces between them? without more info,
you can't shrink the code much.
in any case look into split and unpack.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: 15 Sep 2000 01:43:23 GMT
From: vaughan@itdc.edu (Tom Vaughan)
Subject: Re: Substring Golf?
Message-Id: <8prurr$71p$1@malgudi.oar.net>
Wyzelli (wyzelli@yahoo.com) wrote:
: I am looking for a neater/shorter way of achieving the following:
: my $num = shift;
: $num =~ s/ //g;
: my $numa = substr $num,0,4;
: my $numb = substr $num,4,4;
: my $numc = substr $num,8,4;
: my $numd = substr $num,12,4;
: Any ideas? One liners?
($numa,$numb,$numc,$numd)=/(\d{4})(\d{4})(\d{4})(\d{4})/;
Seems like you are trying to break up a 16 digit number into groups of 4
numbers. This should do it.
Tom
: Wyzelli
------------------------------
Date: Fri, 15 Sep 2000 01:59:00 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: Substring Golf?
Message-Id: <Enfw5.2449$3y3.69496@typhoon.austin.rr.com>
In article <8prurr$71p$1@malgudi.oar.net>,
Tom Vaughan <vaughan@itdc.edu> wrote:
>Wyzelli (wyzelli@yahoo.com) wrote:
>: I am looking for a neater/shorter way of achieving the following:
>
>
>
>: my $num = shift;
>: $num =~ s/ //g;
>
>: my $numa = substr $num,0,4;
>: my $numb = substr $num,4,4;
>: my $numc = substr $num,8,4;
>: my $numd = substr $num,12,4;
>
>: Any ideas? One liners?
>
>
>($numa,$numb,$numc,$numd)=/(\d{4})(\d{4})(\d{4})(\d{4})/;
>
>Seems like you are trying to break up a 16 digit number into groups of 4
>numbers. This should do it.
If you're confident about the quality of your input then you might say
@quartet = $num =~ /(\d{4})/g;
and then use $quartet[0] through $quartet[3] as chunks.
If these happen to be credit card numbers and you expect any valid numbers
which aren't multiples of 4 (e.g. American Express) then you might say
@chunks = $num =~ /(\d{1,4})/g;
In any case having the pieces in an array means you can say
print "@chunks";
if you wnat to displat the chunks separated by spaces.
Hope this helps,
Mike
--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ |
GPG PGP Key 1024D/059913DA | Fingerprint 0570 71CD 6790 7C28 3D60
stok@colltech.com (CT - work) | 75D2 9EC4 C1C0 0599 13DA
------------------------------
Date: Thu, 14 Sep 2000 22:51:27 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: Substring Golf?
Message-Id: <Pine.GSO.4.21.0009142249500.12100-100000@crusoe.crusoe.net>
[posted & mailed]
On Sep 15, Wyzelli said:
>I am looking for a neater/shorter way of achieving the following:
>my $num = shift;
>$num =~ s/ //g;
Regex tips:
s/[CHARS]//g
is slower than
s/[CHARS]+//g
And you should probably be using tr/CHARS//d anyway.
>my $numa = substr $num,0,4;
>my $numb = substr $num,4,4;
>my $numc = substr $num,8,4;
>my $numd = substr $num,12,4;
Use unpack().
my @fields = unpack 'A4 A4 A4 A4', $string;
perldoc -f unpack
--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http://www.perlarchive.com/
CPAN - #1 Perl Resource (my id: PINYAN) http://search.cpan.org/
------------------------------
Date: Fri, 15 Sep 2000 03:00:40 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: Substring Golf?
Message-Id: <ss346ododc6104@corp.supernews.com>
Wyzelli (wyzelli@yahoo.com) wrote:
: I am looking for a neater/shorter way of achieving the following:
:
:
:
: my $num = shift;
: $num =~ s/ //g;
That part could be profitably replaced with
(my $num = shift) =~ tr/ //d;
: my $numa = substr $num,0,4;
: my $numb = substr $num,4,4;
: my $numc = substr $num,8,4;
: my $numd = substr $num,12,4;
my ($numa, $numb, $numc, $numd) = unpack 'a4'x4, $num;
Though rather than use named variables like that, this seems a natural for
generating an array and accessing it by index...
my @nums = unpack 'a4'x4, $num;
...or, if you reall want that abcd thing, make them keys of a hash:
@nums{'a'..'d'} = unpack 'a4'x4, $num;
--
| Craig Berry - http://www.cinenet.net/~cberry/
--*-- "Every force evolves a form."
| - Shriekback
------------------------------
Date: Fri, 15 Sep 2000 03:23:40 GMT
From: mgjv@verbruggen.comdyn.com.au (Martien Verbruggen)
Subject: Re: Substring Golf?
Message-Id: <slrn8s35h8.2cp.mgjv@verbruggen.comdyn.com.au>
On Fri, 15 Sep 2000 10:36:42 +0930,
Wyzelli <wyzelli@yahoo.com> wrote:
> I am looking for a neater/shorter way of achieving the following:
>
>
>
> my $num = shift;
> $num =~ s/ //g;
>
> my $numa = substr $num,0,4;
> my $numb = substr $num,4,4;
> my $numc = substr $num,8,4;
> my $numd = substr $num,12,4;
>
> Any ideas? One liners?
Do they have to be those variable names? I'll just use an array.
Your variable names are fairly misleading, because nothing in your
code indicates that they have to be numbers. So, assuming that the
above code means that after removal of all spaces, the first groups of
four characters should be assigned to four variables:
$_ = shift;
tr/ //d;
my @n = unpack "a4" x 4, $_;
You are unclear about what to do with the numbers after they have been
assigned (a one-liner normally does something, or outputs something).
You are unclear about what the original format is. If there, for
example, always is exactly one (or more) space between the groups of
four characters, you could simply use split. You don't specify whether
$numcontains more than the 16 characters you extract and the possible
spaces.
Much more efficient solutions could possibly be found if we knew what
the data was.
Instead of asking for translations of code, it would be much better to
specify the problem, and provide some sample data.
Martien
--
Martien Verbruggen | My friend has a baby. I'm writing
Interactive Media Division | down all the noises the baby makes so
Commercial Dynamics Pty. Ltd. | later I can ask him what he meant -
NSW, Australia | Steven Wright
------------------------------
Date: Thu, 14 Sep 2000 19:15:55 -0700
From: "Christopher M. Jones" <christopher_j@uswest.net>
Subject: Re: Teaching Perl
Message-Id: <JDfw5.710$0j.383699@news.uswest.net>
"Jürgen Exner" <juex@deja.com> wrote:
> This has nothing to do with Perl anymore, but so what.
> For your girlfriend being a mathematician maybe looking at it from a
program
> verification point of view might help.
>
> The c on the left side and the c on the right side are not the same, you
> must index them with a "pre" and a "post".
>
> Now, rewriting this assignment yields (using mathematical equality):
> c[post]=c[pre]+1
Why go through all of that? Anybody sufficiently educated in
mathematics should be familiar with recursive sequences.
For example, c(2) = c(0) + 1, or just c(n+1) = c(n) + 1
It's not too difficult to extend that concept to arbitrary
assignment.
------------------------------
Date: Thu, 14 Sep 2000 19:51:48 -0700
From: "Christopher M. Jones" <christopher_j@uswest.net>
Subject: Re: Teaching Perl
Message-Id: <c9gw5.730$0j.402370@news.uswest.net>
"Christopher M. Jones" <christopher_j@uswest.net> wrote:
> For example, c(2) = c(0) + 1, or just c(n+1) = c(n) + 1
^
Errr, should be c(1).
------------------------------
Date: 15 Sep 2000 02:25:41 GMT
From: inwap@best.com (Joe Smith)
Subject: Re: Unexpected behavior of shift in a loop
Message-Id: <8ps1b5$13fv$1@nntp1.ba.best.com>
In article <m3snrx865e.fsf@dhcp11-177.support.tivoli.com>,
Ren Maddox <ren.maddox@tivoli.com> wrote:
> join(" ", @fields[4..$#fields]);
>
>Off-thread-topic-side-note: I tried to use [4..-1] initially. I don't
>know if it was because of the previous thread related to that, or
>because it just felt like it should work. Of course, it didn't, but
>it still feels like it should....
This has been discussed on the perl5-porters list.
Using a negative number in the .. operator like that would be
1) ambiguous, or
2) break existing scripts.
-Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.
------------------------------
Date: Thu, 14 Sep 2000 19:18:48 -0700
From: "Christopher M. Jones" <christopher_j@uswest.net>
Subject: Re: UNICODE & SSI
Message-Id: <hGfw5.712$0j.386379@news.uswest.net>
"jason" <elephant@squirrelgroup.com> wrote:
> sang <laoxiu100@hotmail.com> wrote ..
> >Why i cannot use SSI in UNICODE file?
>
> and your Perl question is ?
AKA, wrong newsgroup, this is a webserver question not a
Perl question.
------------------------------
Date: Thu, 14 Sep 2000 23:37:16 -0300
From: "Agustin Chernitsky" <agustinchernitsky@altavista.com>
Subject: Were to get a sendmail for a Win98 SE ?
Message-Id: <8ps20v$di7sa$1@ID-48235.news.cis.dfn.de>
Hi!
I'am running a server on my Win98SE for development. I need a sendmail
program since my Internet Server uses sendmail to send it's mail. Can
someone tell me were to get a sendmail for Win32?
Thanks!
--
Agustín Chernitsky
InterEmpresa
E-mail: agustinchernitsky@altavista.com
ICQ: 20020826
------------------------------
Date: Fri, 15 Sep 2000 12:31:17 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Were to get a sendmail for a Win98 SE ?
Message-Id: <Jggw5.21$eI1.4155@vic.nntp.telstra.net>
"Agustin Chernitsky" <agustinchernitsky@altavista.com> wrote in message
news:8ps20v$di7sa$1@ID-48235.news.cis.dfn.de...
> Hi!
>
> I'am running a server on my Win98SE for development. I need a
sendmail
> program since my Internet Server uses sendmail to send it's mail. Can
> someone tell me were to get a sendmail for Win32?
>
http://www.indigostar.com/
Wyzelli
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 4329
**************************************