[28838] in Perl-Users-Digest
Perl-Users Digest, Issue: 82 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 27 14:06:29 2007
Date: Sat, 27 Jan 2007 11:05:07 -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 Sat, 27 Jan 2007 Volume: 11 Number: 82
Today's topics:
Re: adding floating point <tadmc@augustmail.com>
Re: adding floating point <tadmc@augustmail.com>
Re: adding floating point <perl4hire@softouch.on.ca>
Re: adding floating point <perl4hire@softouch.on.ca>
Re: adding floating point <see.sig@rochester.rr.com>
Re: adding floating point <jurgenex@hotmail.com>
Re: adding floating point <perl4hire@softouch.on.ca>
Re: custom regexp modifiers <tadmc@augustmail.com>
help with mime head jcharth@hotmail.com
Re: help with mime head <bik.mido@tiscalinet.it>
Re: How can I convert a scalar to a range list ??? hekutu@gmail.com
Re: How can I convert a scalar to a range list ??? hekutu@gmail.com
Re: How can I convert a scalar to a range list ??? <noreply@gunnar.cc>
Re: How can I convert a scalar to a range list ??? <mritty@gmail.com>
Re: How can I convert a scalar to a range list ??? <bik.mido@tiscalinet.it>
Re: How can I convert a scalar to a range list ??? <bik.mido@tiscalinet.it>
Re: How can I convert a scalar to a range list ??? xhoster@gmail.com
Undefined subroutine even though it appears in symbol t <namedcoward@gmx.ch>
Re: Undefined subroutine even though it appears in symb <mritty@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 27 Jan 2007 07:46:58 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: adding floating point
Message-Id: <slrnermlui.44j.tadmc@tadmc30.august.net>
Amer Neely <perl4hire@softouch.on.ca> wrote:
> Amer Neely wrote:
>> #printf "%8.2f\n",(shipping * $fuelfee) + $shipping + $subtotal; # when
>
> There is a typo in my pasted code above. 'shipping' should obviously be
> '$shipping'.
A typo in a comment will not have any effect on program execution. :-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 27 Jan 2007 08:13:31 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: adding floating point
Message-Id: <slrnermngb.44j.tadmc@tadmc30.august.net>
Amer Neely <perl4hire@softouch.on.ca> wrote:
> I've read the FAQ 'Why am I getting long decimals (eg, 19.9499999999999)
> instead of the numbers I should be getting (eg, 19.95)?' and other
> references, but am still confused by the results I'm getting.
If you can arrange to use integer data rather than floating point data,
then you wouldn't really need to understand the number theory issues
of accuracy and precision. See below.
> unless ($#ARGV == 1)
That means "if there are not 2 command line arguments".
A more natural what of writing that (to me anyway) would be:
if ( @ARGV != 2 )
> Total: $ 212.19
> So I'm confused why perl is out by a penny.
Since your data appears to be in units of (floating point) dollars,
you should consider instead using units of (integer) cents in
all of your calculations, then convert to dollars only for
final output.
If you need it to be dead-accurate (as is often the case when the
data is money :-), then take care to perform only integer arithmetic
(eg. watch for division operations).
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 27 Jan 2007 11:02:24 -0500
From: Amer Neely <perl4hire@softouch.on.ca>
Subject: Re: adding floating point
Message-Id: <5HKuh.18184$Ah.7894@read2.cgocable.net>
Abigail wrote:
> Amer Neely (perl4hire@softouch.on.ca) wrote on MMMMDCCCXCVII September
> MCMXCIII in <URL:news:wJyuh.3189$WL2.65@read1.cgocable.net>:
> ## I've read the FAQ 'Why am I getting long decimals (eg, 19.9499999999999)
> ## instead of the numbers I should be getting (eg, 19.95)?' and other
> ## references, but am still confused by the results I'm getting.
> ##
> ## #! /usr/bin/perl
> ##
> ## BEGIN
> ## {
> ## open (STDERR,">>$0-err.txt");
> ## print STDERR "\n",scalar localtime,"\n$0\n";
> ## }
> ##
> ## use strict;
> ## use warnings;
> ##
> ## unless ($#ARGV == 1)
> ## {
> ## print "Usage: calc subtotal shipping\n";
> ## exit;
> ## }
> ##
> ## my $subtotal=$ARGV[0];
> ## my $shipping=$ARGV[1];
> ## #my $fuelfee = .1;
> ## my $fuelfee = 10;
> ##
> ## printf "%8.2f\n",$subtotal;
> ## printf "%8.2f\n",$shipping;
> ## printf "%8.2f\n",($shipping / $fuelfee); # when $fuelfee = 10
> ## printf "%8.2f\n",($shipping / $fuelfee) + $shipping + $subtotal; # when
> ## $fuelfee = 10
> ## #printf "%8.2f\n",($shipping * $fuelfee); # when $fuelfee = .1;
> ## #printf "%8.2f\n",(shipping * $fuelfee) + $shipping + $subtotal; # when
> ## $fuelfee = .1
> ##
> ## __END__
> ## Subtotal: $ 185.63
> ## Shipping: $ 24.15
> ## Fuel Fee: $ 2.42
> ## Total: $ 212.19
> ##
> ## If you add the numbers above in your head or a calculator, you get 212.2.
>
> Yes.
>
> ## So I'm confused why perl is out by a penny.
> ##
> ## Is it related to the FAQ?
>
> Not really.
>
> ## $fuelfee is being rounded up by perl from 2.415 to 2.42.
>
> No, it's not. $fuelfee = 10 and doesn't change. But Perl also doesn't
> round $shipping / $fuelfee to 2.42. What it does is round what it
> *displays*.
>
> ## But it appears
> ## to be using 2.415 in the addition, to get 212.195 but then not rounding
> ## that to 212.2.
>
> That's the difference between rounding intermediate results and rounding
> the final results. If you want to get 212.20 as an answer, you must round
> the intermediate values as well:
>
> printf "%8.2f" => sprintf ("%8.2f" => $shipping / $fuelfee)
> + $shipping + $subtotal;
>
>
> In general, the following is not true:
>
> f(a) op f(b) = f(a op b)
>
> and that's what you were doing, with 'op' addition, and 'f' printf's
> rounding functionality.
>
>
> Abigail
OK, this is making more sense now. I was confusing the 'display' value
with the underlying real one (no pun intended). I've managed to get it
working by applying a rounding operation to each value along the way.
But if I understand, printf (or sprintf) to each value would achieve the
same? I'll try that next. Thanks :)
--
Amer Neely
w: www.softouch.on.ca/
b: www.softouch.on.ca/blog/
Perl | MySQL programming for all data entry forms.
"We make web sites work!"
------------------------------
Date: Sat, 27 Jan 2007 11:05:58 -0500
From: Amer Neely <perl4hire@softouch.on.ca>
Subject: Re: adding floating point
Message-Id: <rKKuh.18185$Ah.8352@read2.cgocable.net>
Tad McClellan wrote:
> Amer Neely <perl4hire@softouch.on.ca> wrote:
>
>> I've read the FAQ 'Why am I getting long decimals (eg, 19.9499999999999)
>> instead of the numbers I should be getting (eg, 19.95)?' and other
>> references, but am still confused by the results I'm getting.
>
>
> If you can arrange to use integer data rather than floating point data,
> then you wouldn't really need to understand the number theory issues
> of accuracy and precision. See below.
>
Hmm. Yes, I can see that would work. I may give that a try.
>
>> unless ($#ARGV == 1)
>
>
> That means "if there are not 2 command line arguments".
>
> A more natural what of writing that (to me anyway) would be:
>
> if ( @ARGV != 2 )
>
>
>> Total: $ 212.19
>
>> So I'm confused why perl is out by a penny.
>
>
> Since your data appears to be in units of (floating point) dollars,
> you should consider instead using units of (integer) cents in
> all of your calculations, then convert to dollars only for
> final output.
>
> If you need it to be dead-accurate (as is often the case when the
> data is money :-), then take care to perform only integer arithmetic
> (eg. watch for division operations).
>
>
I've since applied a rounding function to each operation along the way,
instead of the final result as pointed out by Abigail, and got it to
work (at least for this example). I'll test a few more examples and see
what happens. Thanks for clearing up the mud :)
--
Amer Neely
w: www.softouch.on.ca/
b: www.softouch.on.ca/blog/
Perl | MySQL programming for all data entry forms.
"We make web sites work!"
------------------------------
Date: Sat, 27 Jan 2007 12:34:19 -0500
From: Bob Walton <see.sig@rochester.rr.com>
Subject: Re: adding floating point
Message-Id: <45bb8d1b$0$18850$4c368faf@roadrunner.com>
Amer Neely wrote:
> Bob Walton wrote:
>> Amer Neely wrote:
>>> Amer Neely wrote:
...
>>> There is a typo in my pasted code above. 'shipping' should obviously
>>> be '$shipping'.
>>>
>> That's why you should always copy/paste your real working tested code
>> into newsgroup notes, as suggested by the posting guidelines, rather
>> than retyping it.
>
> You missed the part where I said '...pasted code'. I *did* paste it in,
> but the original was wrong as well.
>
Yes, sorry, my apologies. Knee-jerk reaction on my part. I'll read
more carefully in the future.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Sat, 27 Jan 2007 17:57:31 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: adding floating point
Message-Id: <foMuh.2319$yB5.1011@trndny03>
Amer Neely wrote:
>> FAQ 'Why am I getting long decimals (eg, 19.9499999999999) ## instead of
>> the numbers I should be getting (eg, 19.95)?' and other
>
> OK, this is making more sense now. I was confusing the 'display' value
> with the underlying real one (no pun intended). I've managed to get it
> working by applying a rounding operation to each value along the way.
That is not a good idea. You are replacing one place of introducing
inaccuracy with numerous places of introducing inaccuracy.
There are two options:
- Live with 'real' numbers being inaccurate
- use a different representation for your values, e.g. compute your prices
in pennies instead of in dollars. And convert them to dollar for display
only. Natural numbers can be represented accurately without approximation.
jue
------------------------------
Date: Sat, 27 Jan 2007 13:40:43 -0500
From: Amer Neely <perl4hire@softouch.on.ca>
Subject: Re: adding floating point
Message-Id: <w%Muh.18198$Ah.4137@read2.cgocable.net>
Tad McClellan wrote:
> Amer Neely <perl4hire@softouch.on.ca> wrote:
>> Amer Neely wrote:
>
>>> #printf "%8.2f\n",(shipping * $fuelfee) + $shipping + $subtotal; # when
>> There is a typo in my pasted code above. 'shipping' should obviously be
>> '$shipping'.
>
>
> A typo in a comment will not have any effect on program execution. :-)
>
>
Yeah, I know, but I've seen so many posters get jumped on for exactly
that kind of thing. :)
--
Amer Neely
w: www.softouch.on.ca/
b: www.softouch.on.ca/blog/
Perl | MySQL programming for all data entry forms.
"We make web sites work!"
------------------------------
Date: Sat, 27 Jan 2007 07:21:32 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: custom regexp modifiers
Message-Id: <slrnermkes.44j.tadmc@tadmc30.august.net>
Paul <paul@probulletin.com> wrote:
> Actually the following may be better:
>
>
> $path =~ s|\\(?:\\)?|/|sg;
The "s" modifier applies only to dots in the pattern.
You do not have any dots in your pattern.
That "s" modifier is a no-op, so why is it there?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 27 Jan 2007 06:48:27 -0800
From: jcharth@hotmail.com
Subject: help with mime head
Message-Id: <1169909307.068309.250560@l53g2000cwa.googlegroups.com>
Hello I am trygint to get the header of a string. I dont thnk this can
be done with the mime head module but may bet there is a work around.
I am fetching a message to a variable called
$message using LWP, i tried MIME::HEAD->read"$message" and -
>from_file("$message) but now luck. May be I need a funtion that
writes the buffer to the tempfolder and returns the name of the temp
fie?
$head = MIME::Head->read(\*STDIN);
### Parse a new header from a file, or a readable pipe:
$testhead = MIME::Head->from_file("/tmp/test.hdr");
$a_b_head = MIME::Head->from_file("cat a.hdr b.hdr |");
------------------------------
Date: Sat, 27 Jan 2007 16:31:13 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: help with mime head
Message-Id: <hfrmr2d3105d8ptraoiro2jppn2e7bj9pt@4ax.com>
On 27 Jan 2007 06:48:27 -0800, jcharth@hotmail.com wrote:
>Hello I am trygint to get the header of a string. I dont thnk this can
>be done with the mime head module but may bet there is a work around.
>I am fetching a message to a variable called
>$message using LWP, i tried MIME::HEAD->read"$message" and -
The module is MIME::Head, and the above is not syntactically valid
Perl: parens around method calls are mandatory except when the
argument list is empty. Also, not strictly an error, but you don't
need to quote $message. See
perldoc -q quoting
Also, reading M::H's docs I see
: Before reading further, you should see MIME::Tools to make sure that
: you understand where this module fits into the grand scheme of things.
: Go on, do it now. I'll wait.
However, it also says
: ### Parse a new header from a filehandle:
: $head = MIME::Head->read(\*STDIN);
:
: ### Parse a new header from a file, or a readable pipe:
: $testhead = MIME::Head->from_file("/tmp/test.hdr");
: $a_b_head = MIME::Head->from_file("cat a.hdr b.hdr |");
Thus you can open() $message in memory:
open my $fh, '<', \$message or die "D'Oh! $!\n";
my $head = MIME::Head->read($fh);
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: 27 Jan 2007 07:54:34 -0800
From: hekutu@gmail.com
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <1169913274.009638.123540@v45g2000cwv.googlegroups.com>
Dear Paul:
Thank you very much! I have read the perldoc of "eval".
The suggestion you give me is the exact solution to my
problem. Since "eval" is so dangerous could you give
me more hints?
I'm writting a perl script which needs the user to input
one or more integers:
[hekutu@localhost]$ ./atom.pl 1 2 3 4 5 6 7 8 9 10
The input may be very long so I try to interprate the
parameters as perl-style list:
[hekutu@localhost]$ ./atom.pl 1..10
So in my question the text scalar, which is the parameter
that user inputs, needs to be cast into list range.
I hope you could understand my question, could you give
me more hints? Thank you!
Regards
hekutu
On 1=D4=C226=C8=D5, =CF=C2=CE=E77=CA=B159=B7=D6, "Paul Lalli" <mri...@gmail=
.com> wrote:
> On Jan 26, 6:56 am, "Paul Lalli" <mri...@gmail.com> wrote:
>
>
>
>
>
> > On Jan 26, 6:35 am, hek...@gmail.com wrote:
>
> > > But I want to replace the range list with a
> > > flexible scalar, the new program looks like:
> > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
> > > my $a =3D "(1..10)";
> > > my @b =3D $a;
> > > print @b;
> > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
> > > The output is:(1..10)
>
> > > The scalar is still the scalar. How can I convert
> > > the text scalar "(1..10)" to a range list?I'm wary to give this answe=
r, because if you don't understand its
> > dangers, you can really mess things up. Regardless, it is the actual
> > answer to your question. The function that converts a plain string to
> > executable Perl code is C<eval>. Before reading further, or trying it
> > out, *please* read:
> > perldoc -f eval
>
> > Once you've read that, take a look at this sample solution:
> > $ perl -le'
> > my $x =3D q{(1..10)};
> > my @y =3D eval $x;
> > print "@y";
> > '
> > 1 2 3 4 5 6 7 8 9 10
>
> > Please make sure you understand the dangers involved here. If $x could
> > be any user-entered string, avoid this method like the plague. Use it
> > if and ONLY if you are 100% sure you know the possible values this
> > string can have. (Consider for example, what would happen if the user
> > did not enter "(1..10)", but instead "system('rm -rf /home/')" )
>
> > At the very least, you should be checking the format of the
> > user-entered string:
>
> > if ($x !~ /^\(\d+\.\.\d+\)$/) {
> > die "'$x' not in correct format!!\n";
> > }... and even as I pushed the Post button, I realized the advice I
> should have given. Abandon eval(). Completely. Instead retain that
> pattern match above, and use that to get your range:
>
> my @y;
> if ($x =3D~ /^\((\d+)\.\.\(d+)\)$/) {
> @y =3D ($1..$2);} else { die "'$x' not in correct format!!\n";
>
> }Much better.
>
> Paul Lalli- =D2=FE=B2=D8=B1=BB=D2=FD=D3=C3=CE=C4=D7=D6 -- =CF=D4=CA=BE=D2=
=FD=D3=C3=B5=C4=CE=C4=D7=D6 -
------------------------------
Date: 27 Jan 2007 08:06:37 -0800
From: hekutu@gmail.com
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <1169913997.562828.163500@q2g2000cwa.googlegroups.com>
Hi, Michele
I'm writting a perl script which needs the user to input
one or more integers:
[hekutu@localhost]$ ./atom.pl 1 2 3 4 5 6 7 8 9 10
The input may be very long so I try to use perl-style list
parameters.
[hekutu@localhost]$ ./atom.pl 1..10
In my question the text scalar, which is the parameter
that user inputs "1..10", needs to be cast into list range.
Sorry for disturbing you.
Regards
hekutu
On 1=D4=C226=C8=D5, =CF=C2=CE=E710=CA=B105=B7=D6, Michele Dondi <bik.m...@t=
iscalinet.it> wrote:
> On 26 Jan 2007 03:35:23 -0800, hek...@gmail.com wrote:
>
> >my $a =3D "(1..10)";
> >my @b =3D $a;
> >print @b;
> >=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
> >The output is:(1..10)
>
> >The scalar is still the scalar. How can I convert
> >the text scalar "(1..10)" to a range list?As others explained you the st=
rict answer to your question would be to
> use string eval(). But as others also explained eval() and in
> particular string eval() is evil(TM). Thus you can use a workaround
> like the one suggested by Paul Lalli: use a regex to validate your
> scalar and extract the range extremes, and use them to build the range
> itself. OTOH it seems strange to me that you have the exact "(1..10)"
> scalar rather than the extremes in the first place, so I smell some
> degree of XY problem (see e.g.
> <http://perlmonks.org/?node=3DXY+problem>) here. What are you really
> trying to do?
>
> Michele
> --
> {$_=3Dpack'B8'x25,unpack'A8'x32,$a^=3Dsub{pop^pop}->(map substr
> (($a||=3Djoin'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=3D'
> .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=3D~/./g)x2,$_,
> 256),7,249);s/[^\w,]/ /g;$ \=3D/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sat, 27 Jan 2007 17:39:42 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <521dmdF1kvvh0U1@mid.individual.net>
hekutu@gmail.com wrote:
> The follwing small program runs well.
> ==================
> my @b = (1..10);
> print @b;
> ==================
> The output is:12345678910
>
> But I want to replace the range list with a
> flexible scalar, the new program looks like:
> =========================
> my $a = "(1..10)";
> my @b = $a;
my @b = eval $a;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 27 Jan 2007 08:54:16 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <1169916856.608086.88650@s48g2000cws.googlegroups.com>
On Jan 27, 10:54 am, hek...@gmail.com wrote:
> Thank you very much! I have read the perldoc of "eval".
> The suggestion you give me is the exact solution to my
> problem. Since "eval" is so dangerous could you give
> me more hints?
Did you read the message to which you are replying? My second
message, that said to NOT use eval at all? Just use a pattern match:
my @y;
if ($x =~ /^\((\d+)\.\.\(d+)\)$/) {
@y = ($1..$2);
} else {
die "'$x' not in correct format!!\n";
}
Paul Lalli
------------------------------
Date: Sat, 27 Jan 2007 19:42:54 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <p37nr2t7ibblq6rg4u9thahlmls5phv70l@4ax.com>
On 27 Jan 2007 08:06:37 -0800, hekutu@gmail.com wrote:
> The input may be very long so I try to use perl-style list
>parameters.
> [hekutu@localhost]$ ./atom.pl 1..10
> In my question the text scalar, which is the parameter
>that user inputs "1..10", needs to be cast into list range.
I answered to you in another post. But to some extent this question of
yours had already been answered.
> Sorry for disturbing you.
No disturb at all! We're here to (ask for and) provide help, if we
can. But...
>On 1=D4=C226=C8=D5, =CF=C2=CE=E710=CA=B105=B7=D6, Michele Dondi <bik.m...@t=
>iscalinet.it> wrote:
[snip full quoted content]
...*Please* do not top-post. It is considered rude here and it is also
likely to irritate exactly the most resourceful regulars here, i.e.
exactly those that are more likely to give you precious help.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sat, 27 Jan 2007 19:42:59 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <8i6nr2pp8fag06vhfg0ur5vneuj7o3b7j4@4ax.com>
On 27 Jan 2007 07:54:34 -0800, hekutu@gmail.com wrote:
> I'm writting a perl script which needs the user to input
>one or more integers:
> [hekutu@localhost]$ ./atom.pl 1 2 3 4 5 6 7 8 9 10
> The input may be very long so I try to interprate the
>parameters as perl-style list:
> [hekutu@localhost]$ ./atom.pl 1..10
But he told you how to do that. I would use perhaps a single dash or a
colon to separate the numbers on the command line (I don't think a
dash would interfere with Getopt::* modules) thus you may do something
like:
@ARGV = map /^(\d+)-(\d+)$/ ? $1..$2 : $_, @ARGV;
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: 27 Jan 2007 18:42:09 GMT
From: xhoster@gmail.com
Subject: Re: How can I convert a scalar to a range list ???
Message-Id: <20070127134329.253$Cv@newsreader.com>
hekutu@gmail.com wrote:
> Dear Paul:
> Thank you very much! I have read the perldoc of "eval".
> The suggestion you give me is the exact solution to my
> problem. Since "eval" is so dangerous could you give
> me more hints?
> I'm writting a perl script which needs the user to input
> one or more integers:
> [hekutu@localhost]$ ./atom.pl 1 2 3 4 5 6 7 8 9 10
> The input may be very long so I try to interprate the
> parameters as perl-style list:
> [hekutu@localhost]$ ./atom.pl 1..10
I do this something like this:
foreach (split /,/,$expression) {
if (/^\d+$/) {
push @group,$_;
next;
};
if ( /^(\d+)[-.]+(\d+)$/) {
push @group, ($1 .. $2 );
next;
};
die "The subexpression $_ in $expression is invalid\n";
};
It lets me specify disjoint ranges, such as 1-20,23,28-50, on the command
line. Also, you can use the perlish ".." or the more intuitive but
nonPerlish "-". (it also lets you use ".", which may cause problems
if things don't need to be integers.)
Xho
> So in my question the text scalar, which is the parameter
> that user inputs, needs to be cast into list range.
> I hope you could understand my question, could you give
> me more hints? Thank you!
>
> Regards
>
> hekutu
>
> On 1=D4=C226=C8=D5, =CF=C2=CE=E77=CA=B159=B7=D6, "Paul Lalli"
> <mri...@gmail= .com> wrote:
> > On Jan 26, 6:56 am, "Paul Lalli" <mri...@gmail.com> wrote:
> >
> >
> >
> >
> >
> > > On Jan 26, 6:35 am, hek...@gmail.com wrote:
> >
> > > > But I want to replace the range list with a
> > > > flexible scalar, the new program looks like:
> > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
> > > > 3D=
> =3D=3D
> > > > my $a =3D "(1..10)";
> > > > my @b =3D $a;
> > > > print @b;
> > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
> > > > 3D=
> =3D=3D
> > > > The output is:(1..10)
> >
> > > > The scalar is still the scalar. How can I convert
> > > > the text scalar "(1..10)" to a range list?I'm wary to give this
> > > > answe=
> r, because if you don't understand its
> > > dangers, you can really mess things up. Regardless, it is the actual
> > > answer to your question. The function that converts a plain string
> > > to executable Perl code is C<eval>. Before reading further, or
> > > trying it out, *please* read:
> > > perldoc -f eval
> >
> > > Once you've read that, take a look at this sample solution:
> > > $ perl -le'
> > > my $x =3D q{(1..10)};
> > > my @y =3D eval $x;
> > > print "@y";
> > > '
> > > 1 2 3 4 5 6 7 8 9 10
> >
> > > Please make sure you understand the dangers involved here. If $x
> > > could be any user-entered string, avoid this method like the plague.
> > > Use it if and ONLY if you are 100% sure you know the possible values
> > > this string can have. (Consider for example, what would happen if
> > > the user did not enter "(1..10)", but instead "system('rm -rf
> > > /home/')" )
> >
> > > At the very least, you should be checking the format of the
> > > user-entered string:
> >
> > > if ($x !~ /^\(\d+\.\.\d+\)$/) {
> > > die "'$x' not in correct format!!\n";
> > > }... and even as I pushed the Post button, I realized the advice I
> > should have given. Abandon eval(). Completely. Instead retain that
> > pattern match above, and use that to get your range:
> >
> > my @y;
> > if ($x =3D~ /^\((\d+)\.\.\(d+)\)$/) {
> > @y =3D ($1..$2);} else { die "'$x' not in correct format!!\n";
> >
> > }Much better.
> >
> > Paul Lalli- =D2=FE=B2=D8=B1=BB=D2=FD=D3=C3=CE=C4=D7=D6 --
> > =CF=D4=CA=BE=D2=
> =FD=D3=C3=B5=C4=CE=C4=D7=D6 -
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 27 Jan 2007 07:58:46 -0800
From: "max.gruber" <namedcoward@gmx.ch>
Subject: Undefined subroutine even though it appears in symbol table?
Message-Id: <1169913526.079258.208560@k78g2000cwa.googlegroups.com>
Suppose I've got a subroutine subA in PackageA which calls subB from
PackageB.
Now somehow I keep getting "Undefined subroutine &PackageB::subB
called at PackageA line n" (line number points to subA) depending on
from where I call subA.
For example, I can call subA from a subroutine in PackageC without
getting any error, but when I call subA from another subroutine in
PackageA, the error will appear. PackageA has a use statement for
PackageB and PackageB will export subB automatically. PackageC doesn't
have a use statement for PackageB.
When I dump the symbol table %:: just before the call to subB, in all
cases where the error appears, it will actually contain a reference to
subB. In all cases where the error doesn't appear, it won't contain a
reference. (WTF?)
Calling subB with its full name (PackageB::subB()) will always work,
by the way. But I'd rather understand why it won't work without the
package name.
Hope that was more or less understandable. ;)
I have absolutely no idea where to go from here. I'm utterly confused
and would appreciate any hints on how to solve this.
Regards,
Max
------------------------------
Date: 27 Jan 2007 08:57:07 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Undefined subroutine even though it appears in symbol table?
Message-Id: <1169917027.363534.170050@m58g2000cwm.googlegroups.com>
On Jan 27, 10:58 am, "max.gruber" <namedcow...@gmx.ch> wrote:
> Suppose I've got a subroutine subA in PackageA which calls subB from
> PackageB.
>
> Now somehow I keep getting "Undefined subroutine &PackageB::subB
> called at PackageA line n" (line number points to subA) depending on
> from where I call subA.
>
> For example, I can call subA from a subroutine in PackageC without
> getting any error, but when I call subA from another subroutine in
> PackageA, the error will appear. PackageA has a use statement for
> PackageB and PackageB will export subB automatically. PackageC doesn't
> have a use statement for PackageB.
>
> When I dump the symbol table %:: just before the call to subB, in all
> cases where the error appears, it will actually contain a reference to
> subB. In all cases where the error doesn't appear, it won't contain a
> reference. (WTF?)
>
> Calling subB with its full name (PackageB::subB()) will always work,
> by the way. But I'd rather understand why it won't work without the
> package name.
>
> Hope that was more or less understandable. ;)
Not really, no. Why not just post a short-but-complete script that
demonstrates the problem you're having, as suggested in the Posting
Guidelines.
Paul Lalli
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 82
*************************************