Ask a Question related to PERL Miscellaneous, Design and Development.
-
J Krugman #1
"Surrogate infinity" in Perl?
In C, for example, one can use certain predefined constants as
"surrogate infinity", meaning that no number (of the appropriate
type) handled by a C program will ever be greater than the appropriate
constant. For example:
int min_val;
....
min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
for(i = 0; i < ITER; ++i) {
if ( min_val > arr[i] ) {
min_val = arr[i];
}
}
Is there a similar facility in Perl?
Thanks!
-Jill
J Krugman Guest
-
Proj cannot run on LCDS 2.6 ES due to "Unable to resolveresource bundle "datamanagement" for locale "en_US"
hi, all, We have developped an application on Flex Build 3 (run successfully), but failed when we try to deploy it on Tomcat with LCDS 2.5 ES... -
"Devel::DProf" on a PERL script uses "Test::More"
Dear all, I encountered a problem while run profiling on a script which uses "Test::More". May I ask whether anybody have some idea or wrap... -
CFINPUT type="radio" w/ "value" requires "label"
On a Flash form, when you specify type='radio' and value='whatever', the value of the 'value' attribute will be displayed as a label if no 'label'... -
How do you simulate "." or "source" in a perl script ?
On 7 Aug 2003 08:21:27 -0700 c_j_marshall@hotmail.com (Chris Marshall) wrote: Shell::Source perhaps this CPAN module will do it for you. ... -
"Start" "Program" "Menu" list is empty
For what ever reason my list of installed programs in my "Start" "Programs" menu is empty. Anyone know how to restore the list. Thanks for your... -
John Bokma #2
Re: "Surrogate infinity" in Perl?
J Krugman wrote:
Better:> In C, for example, one can use certain predefined constants as
> "surrogate infinity", meaning that no number (of the appropriate
> type) handled by a C program will ever be greater than the appropriate
> constant. For example:
>
> int min_val;
>
> ...
>
> min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
> for(i = 0; i < ITER; ++i) {
> if ( min_val > arr[i] ) {
> min_val = arr[i];
> }
> }
>
> Is there a similar facility in Perl?
min_val = arr[0];
for(i = 0; i < ITER; i++) {
if (arr[i]) < min_val) min_val = arr[i];
}
or a perl way:
my $min = shift @array;
foreach my $value (@array) {
$min = $value if $value < $min;
}
Why is the latter more readable? :-D.
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
John Bokma #3
Re: "Surrogate infinity" in Perl?
John Bokma wrote:
should read: i = 1 and assumes array has at least 2 values...> min_val = arr[0];
> for(i = 0; i < ITER; i++) {
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
Purl Gurl #4
Re: "Surrogate infinity" in Perl?
J Krugman wrote:
> int min_val;> min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
> for(i = 0; i < ITER; ++i) {
> if ( min_val > arr[i] ) {
> min_val = arr[i];
> }
> }
You need to review your thinking.
Based on your incomplete C code snippet, your value
for min_val will be set to the first number found
in your array which is less than min_val. This
new value of min_val will remain so, in lieu of
further lesser values, defeating the purpose of
continued iteration looping.
Otherwords, min_val will be set to the lowest
value in your array, and remain so.
Are you trying to set min_val to the lowest value
in your array, or trying to set min_val to the
highest value which is also a lesser value than
your constant INT_MAX?
My Perl code examples beneath my signature exemplifies
what I believe to be your logic, whatever this may be.
Purl Gurl
[url]http://www.purlgurl.net[/url]
--
#!perl
use constant INT_MAX => 2;
use constant ITER => 11;
$minimum_value = INT_MAX;
@Array = (0 .. 10);
for ($iterate = 0; $iterate < ITER; $iterate++)
{
if ($minimum_value > $Array[$iterate])
{ $minimum_value = $Array[$iterate]; }
}
print $minimum_value;
PRINTED RESULTS:
________________
0
**
#!perl
use constant INT_MAX => 2;
use constant ITER => 11;
$minimum_value = INT_MAX;
@Array = (0 .. 10);
for ($iterate = 0; $iterate < ITER; $iterate++)
{
if (INT_MAX > $Array[$iterate])
{ $minimum_value = $Array[$iterate]; }
}
print $minimum_value;
PRINTED RESULTS:
________________
1
Purl Gurl Guest
-
John W. Krahn #5
Re: "Surrogate infinity" in Perl?
J Krugman wrote:
>
> In C, for example, one can use certain predefined constants as
> "surrogate infinity", meaning that no number (of the appropriate
> type) handled by a C program will ever be greater than the appropriate
> constant. For example:
>
> int min_val;
> ...
>
> min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
> for(i = 0; i < ITER; ++i) {
> if ( min_val > arr[i] ) {
> min_val = arr[i];
> }
> }
>
> Is there a similar facility in Perl?
use POSIX;
my $min_val = INT_MAX;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
J Krugman #6
Re: "Surrogate infinity" in Perl?
In <3F5CC5D3.136AD0FC@purlgurl.net> Purl Gurl <purlgurl@purlgurl.net> writes:
>J Krugman wrote:
>
>>> int min_val;>>> min_val = INT_MAX; /* INT_MAX used as a surrogate infinity */
>> for(i = 0; i < ITER; ++i) {
>> if ( min_val > arr[i] ) {
>> min_val = arr[i];
>> }
>> }>You need to review your thinking.Not so. "Further lesser values" will result in the if statement's>Based on your incomplete C code snippet, your value
>for min_val will be set to the first number found
>in your array which is less than min_val. This
>new value of min_val will remain so, in lieu of
>further lesser values, defeating the purpose of
>continued iteration looping.
test evaluating to true, and min_val being reset to the lower value.
What's the problem?
-Jill
J Krugman Guest
-
J Krugman #7
Re: "Surrogate infinity" in Perl?
In <3F5CC784.D3675FF5@acm.org> "John W. Krahn" <krahnj@acm.org> writes:
>use POSIX;That's the ticket. Thanks!>my $min_val = INT_MAX;
-Jill
J Krugman Guest
-
Purl Gurl #8
Re: "Surrogate infinity" in Perl?
J Krugman wrote:
(snipped)> Purl Gurl wrote:> > J Krugman wrote:
> >You need to review your thinking.> >Based on your incomplete C code snippet, your value
> >for min_val will be set to the first number foundYour problem is twofold. Your original article is poorly> What's the problem?
written leaving a reader guessing at what you are doing.
This subsequent article of yours, to which I am responding,
carefully avoids issues addressed by me, in my first article.
You need to review your thinking.
Purl Gurl
--
Kick Ass Perl And Kick Ass Rock N Roll!
[url]http://www.purlgurl.net[/url]
Purl Gurl Guest
-
Vlad Tepes #9
Re: "Surrogate infinity" in Perl?
J Krugman <jill_krugman@yahoo.com> wrote:
I'm don't know much C, but I suspect your wanting to set INT_MAX comes
from a desire to restrict yourself. Bondage could be implemented by
tie()ing your variables. I'm not usually into that sort of thing,
but I had to check it out:
#!/usr/bin/perl
use strict;
use warnings;
package Bondage;
require Tie::Scalar;
our @ISA = qw( Tie::StdScalar );
my %MAX;
sub TIESCALAR {
my $class = shift;
my $value;
my $self = bless \$value, $class;
$MAX{$self} = shift;
$self;
}
sub STORE {
my $self = shift;
my $value = shift;
if ( $value > $MAX{$self} ) {
die "Overflow: $MAX{$self} < $value"
}
$$self = $value;
$value;
}
package main;
tie my $i, "Bondage", 5; # Overflow if $i > 5
printf "i = %d\n", $i = $_ for (1..7);
--
Vlad
Vlad Tepes Guest
-
Anno Siegel #10
Re: "Surrogate infinity" in Perl?
J Krugman <jill_krugman@yahoo.com> wrote in comp.lang.perl.misc:
....or not. INT_MAX is the maximum value a native integer can assume.> In <3F5CC784.D3675FF5@acm.org> "John W. Krahn" <krahnj@acm.org> writes:
>>> >use POSIX;>> >my $min_val = INT_MAX;
> That's the ticket. Thanks!
Perl numbers (even integers) can be larger than that, because Perl
switches to float point representation when needed.
In fact, initializing a minimum to "some huge value" is only a cop-out.
The right method is to initialize it to one of the values the minimum
to be taken of.
Anno
Anno Siegel Guest
-
Anno Siegel #11
Re: "Surrogate infinity" in Perl?
J Krugman <jill_krugman@yahoo.com> wrote in comp.lang.perl.misc:
....or not. INT_MAX is the maximum value a native integer can assume.> In <3F5CC784.D3675FF5@acm.org> "John W. Krahn" <krahnj@acm.org> writes:
>>> >use POSIX;>> >my $min_val = INT_MAX;
> That's the ticket. Thanks!
Perl numbers (even integers) can be larger than that, because Perl
switches to float point representation when needed.
In fact, initializing a minimum to "some huge value" is only a cop-out.
The right method is to initialize it to one of the values the minimum
is to be taken of.
Anno
Anno Siegel Guest
-
Purl Gurl #12
Re: "Surrogate infinity" in Perl?
Anno Siegel wrote:
(snipped)> J Krugman wrote:> > John W. Krahn wrote:
> > >use POSIX;> > >my $min_val = INT_MAX;> > That's the ticket. Thanks!> ...or not. INT_MAX is the maximum value a native integer can assume.
> Perl numbers (even integers) can be larger than that, because Perl
> switches to float point representation when needed.> In fact, initializing a minimum to "some huge value" is only a cop-out.
> The right method is to initialize it to one of the values the minimum
> to be taken of.
You will find a good number of articles validating your comments here:
[url]http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=INT_MAX+overflow&btnG=Google+Search[/url]
A reader will discover problems with overflow, returns which are not
of an integer value and problems of incorrect returns, such as a return
being negative instead of positive. My hunch is INT_MAX will vary with
operating system type and most likely, operating system version. I recall
somewhere in time, specific versions of Linux exhibit INT_MAX problems.
Use of POSIX is risky. This is evidenced by failure of "NMS" scripts
to print a date, a simple task, under some operating systems, despite
claims NMS is a source for "correctly" written Perl scripts.
Siegel's comment of "cop-out" is an understatment. Using default INT_MAX
as a value, is a poor programming practice, just as NMS' use of POSIX for
date generation, is a poor programming practice.
use constant INT_MAX => "a carefully selected value";
Purl Gurl
--
[url]http://www.purlgurl.net[/url]
Purl Gurl Guest



Reply With Quote

