[32866] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4144 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Feb 22 14:09:39 2014

Date: Sat, 22 Feb 2014 11:09:05 -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, 22 Feb 2014     Volume: 11 Number: 4144

Today's topics:
    Re: How do I get "system" to report the correct failure <ben@morrow.me.uk>
    Re: How do I get "system" to report the correct failure (Tim McDaniel)
    Re: obj 8-2 <kaz@kylheku.com>
    Re: obj 8-2 <janek_schleicher@yahoo.de>
    Re: obj 8-2 <johnblack@nospam.com>
    Re: obj 8-2 <mach2@hushmail.com>
    Re: obj 8-2 <dave@invalid.invalid>
    Re: obj 8-2 <mach2@hushmail.com>
    Re: obj 8-2 <mach2@hushmail.com>
    Re: obj 8-2 <mach2@hushmail.com>
    Re: obj 8-2 <rweikusat@mobileactivedefense.com>
    Re: obj 8-2 <dave@invalid.invalid>
    Re: obj 8-2 <ben@morrow.me.uk>
    Re: obj 8-2 <mach2@hushmail.com>
    Re: obj 8-2 <mach2@hushmail.com>
    Re: obj 8-2 <johnblack@nospam.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 22 Feb 2014 00:19:55 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How do I get "system" to report the correct failure message?
Message-Id: <b27lta-k513.ln1@anubis.morrow.me.uk>


Quoth laredotornado@zipmail.com:
> 
> I'm using Perl 5.16 on mac 10.9.1.  I'm trying to get Perl to output the
> correct error message when I run a shell process and that process fails.
> I have this ...
> 
>         system("$jbossHome/bin/jboss-cli.sh", "--file=$file");
>         if ( $? != 0 )
>         {
>                 die"Failed to deploy $downloadedFile: $!\n";

You are checking $? and then printing $!. Properly speaking you need to
check both:

    use Fcntl qw/:sys_wait_h/;

    my $st = system (...);
    if ($st == -1) {
        die "failed to run jboss-cli: $!";
    }
    elsif ($st != 0) {
        if (WIFEXITED($?)) {
            die sprintf "jboss-cli exited with status %i", 
                WEXITSTATUS($?);
        }
        elsif (WIFSIGNALED($?)) {
            die sprintf "jboss-cli was killed with signal %i",
                WTERMSIG($?);
        }
        else {
            # shouldn't happen
            die "jboss-cli failed: $?";
        }
    }
        
On systems which use non-standard return values from wait(2) you may
need to replace $? with ${^CHILD_ERROR_NATIVE}, though IIRC this is not
consistent across perl versions.

>         }
> 
> However, what Perl reports is "Failed to deploy /tmp/my.war:
> Inappropriate ioctl for device" and when I run the command in a shell,
> the actual error message is "{"JBAS014653: Composite operation failed
> and was rolled back. Steps that failed:" => {"Operation step-2" =>
> {"JBAS014671: Failed services" =>
> {"jboss.web.deployment.default-host./my" =>
> "org.jboss.msc.service.StartException in service
> jboss.web.deployment.default-host./my: JBAS018040: Failed to start
> context"},"JBAS014771: Services with missing/unavailable dependencies"
> => ["jboss.deployment.unit.\"my.war\".jboss.security.jacc
> Missing[JBAS014861: <one or more transitive dependencies>]"]}}}".  
> 
> Any ideas how I can output the correct error message?

Does the command print this to stderr (or, indeed, stdout)? I would
expect this to be visible when you run the command via system, given
that you haven't redirected either. What matters is to make sure your
Perl program also realises something went wrong.

Ben



------------------------------

Date: Sat, 22 Feb 2014 04:19:30 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: How do I get "system" to report the correct failure message?
Message-Id: <le98gi$s9k$1@reader1.panix.com>

In article <87bny03zln.fsf@sable.mobileactivedefense.com>,
Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:
>laredotornado@zipmail.com writes:
>> I'm using Perl 5.16 on mac 10.9.1.  I'm trying to get Perl to output
>the correct error message when I run a shell process and that process
>fails.  I have this ...
>>
>>         system("$jbossHome/bin/jboss-cli.sh", "--file=$file");
 ...
>$! is the Perl-equivalent of errno and it is set when an operation
>performed via the C library fails, usually, a system call. But no
>system calls made by your program failed during system,

To clarify standard Perl (UNIX, really) terminology: "system call"
roughly means a call into the operating system (or, I assume, if the
OS in question doesn't provide a particular Perl builtin function, it
might be an implementation provided by Perl).

Anyway, "system call" means things like sysread, fork, gmtime, and
such.  It does not mean a call to the sub that happens to have the
name "system".  In Perl, "system()" is not referred to as a "system
call".

>But (for obvious reasons), the system cannot provide error messages
>arbitrary "application failures".

Indeed, that's the operative point.  If I want to analyze the results
of an external program, I have to capture its output in Perl using
`...` or `... 2>&1` (if supported by the OS and/or shell) or more
sophisticated methods.  And I usually have to look at $?, as program
that fail often exit with a non-zero exit code, and that goes into $?.
See the perlfunc documentation for system to learn the structure of
$?.

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Sat, 22 Feb 2014 08:02:53 +0000 (UTC)
From: Kaz Kylheku <kaz@kylheku.com>
Subject: Re: obj 8-2
Message-Id: <20140221225039.921@kylheku.com>

On 2014-02-21, Marek Novotny <mach2@hushmail.com> wrote:
> #!/usr/bin/perl
> # File: Obj8-2.pl
> use strict;
> use warnings;
>
> my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
> my $answer = 0;

Your algorithm only works for a non-empty array of non-negative numbers.


------------------------------

Date: Sat, 22 Feb 2014 12:51:54 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: obj 8-2
Message-Id: <bmrhavFf8gsU1@mid.individual.net>

Hi Marek,

certainly you got this as exercise to learn the language than to solve 
the Problem. So it's probably not intended to just use a module, but 
somewhat should at least mention it:

A short and nice solution is also:

#!/usr/bin/perl

use strict;
use warnings;

use List::Util qw/max/;

my @array = (42,41,42,31,54,42,56,57,46,58,59,60,34,61);

print "Max of @array is: ", max(@array);


As usual, there is more than way to do it.

Greetings,
Janek


------------------------------

Date: Sat, 22 Feb 2014 09:31:48 -0600
From: John Black <johnblack@nospam.com>
Subject: Re: obj 8-2
Message-Id: <MPG.2d727edf27c6dc7d9897af@news.eternal-september.org>

In article <87r46w42t8.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com 
says...
> 
> Marek Novotny <mach2@hushmail.com> writes:
> > On Fri, 21 Feb 2014 19:03:10 +0000, Henry Law wrote:
> 
> [...]
> 
> >> But for an algorithm, why not just loop through the array (Perl is good
> >> at that) and keep a running note of the largest number you found?
> >> 
> >> You can code a loop over the values of an array like this:
> >> 
> >>    for my $current_number ( @array ) {
> >>      # Here $current_number is each value in the array,
> >>      # in order, one by one
> >> 
> >> ... and you can code your 'if' statement more perlishly
> >> 
> >>    $max_number = $currentnumber if  # Put your condition after the if
> >>                                     # and don't forget the semicolon.
> >> 
> >> Then when the loop finishes, print out the maximum number you found.
> >> About six lines of code is what you're aiming for.
> >
> > Thank you for all the replies first of all. I read what you write, but as 
> > a beginner it's not always easy to translate the information into actual 
> > code. That being said, this is what I came up with. 
> >
> > #!/usr/bin/perl
> > # File: Obj8-2.pl
> > use strict;
> > use warnings;
> >
> > my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
> > my $answer = 0;
> > my $elem = 0;
> >
> > foreach $elem (@array){
> > 	if ($elem > $answer){
> > 		$answer = $elem;
> > 	}
> > }
> > print "The Answer is: $answer.\n";
> 
> This obviously breaks down when the array contains numbers < 0.
> 
> @array = (42,41,42,31,54,42,56,57,46,58,59,60,34,61);
> 
> $max = $array[0];
> $_ > $max and $max = $_ for @array[1 .. $#array];
> 
> print "The Answer is: $max\n"; 

Honestly, I like Marek's second solution much better than all of yours.  Marek's code is very 
readable and easy to understand what its doing.  Sorry Rainer, but yours is a cryptic mess 
(IMO).  And so are all the other ones that use $_.  The only thing Marek needs to do to allow 
him to handle an array of all negative values is to set:

my $answer = $array[0];  # instead of = 0

John Black


------------------------------

Date: Sat, 22 Feb 2014 09:31:47 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <I4OdnRWw3cp-X5XOnZ2dnUVZ_vOdnZ2d@supernews.com>

On Sat, 22 Feb 2014 08:02:53 +0000, Kaz Kylheku wrote:

> On 2014-02-21, Marek Novotny <mach2@hushmail.com> wrote:
>> #!/usr/bin/perl # File: Obj8-2.pl use strict;
>> use warnings;
>>
>> my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
>> my $answer = 0;
> 
> Your algorithm only works for a non-empty array of non-negative numbers.

I just tested this code:

#!/usr/bin/perl
# File: Obj8-2.pl
use strict;
use warnings;

my @array=(-5,-4,-3,-1,-7,0,-53,-12,-5,-43,-29,-32,-18,-49);
my $answer = 0;
my $elem = 0;

foreach $elem (@array){
	if ($elem > $answer){
		$answer = $elem;
	}
}
print "The Answer is: $answer.\n";

It yielded the corrected answer, that being zero. 
-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 22 Feb 2014 15:44:21 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: obj 8-2
Message-Id: <fV45K0OBJxbE-pn2-dNYNWyXT5Vyw@paddington.bear.den>

On Sat, 22 Feb 2014 15:31:47 UTC, Marek Novotny <mach2@hushmail.com> 
wrote:

> On Sat, 22 Feb 2014 08:02:53 +0000, Kaz Kylheku wrote:
> 
> > On 2014-02-21, Marek Novotny <mach2@hushmail.com> wrote:
> >> #!/usr/bin/perl # File: Obj8-2.pl use strict;
> >> use warnings;
> >>
> >> my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
> >> my $answer = 0;
> > 
> > Your algorithm only works for a non-empty array of non-negative numbers.
> 
> I just tested this code:
> 
> #!/usr/bin/perl
> # File: Obj8-2.pl
> use strict;
> use warnings;
> 
> my @array=(-5,-4,-3,-1,-7,0,-53,-12,-5,-43,-29,-32,-18,-49);
> my $answer = 0;
> my $elem = 0;
> 
> foreach $elem (@array){
> 	if ($elem > $answer){
> 		$answer = $elem;
> 	}
> }
> print "The Answer is: $answer.\n";
> 
> It yielded the corrected answer, that being zero. 

Remove 0 from the array and try again............

-- 
Regards
Dave Saville


------------------------------

Date: Sat, 22 Feb 2014 09:48:21 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <I4OdnRSw3cpYW5XOnZ2dnUVZ_vMAAAAA@supernews.com>

On Sat, 22 Feb 2014 12:51:54 +0100, Janek Schleicher wrote:

> Hi Marek,
> 
> certainly you got this as exercise to learn the language than to solve
> the Problem. So it's probably not intended to just use a module, but
> somewhat should at least mention it:
> 
> A short and nice solution is also:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> use List::Util qw/max/;
> 
> my @array = (42,41,42,31,54,42,56,57,46,58,59,60,34,61);
> 
> print "Max of @array is: ", max(@array);
> 
> 
> As usual, there is more than way to do it.
> 
> Greetings,
> Janek

Thank you. Yeah, no mention of anything like that just yet. I just picked 
up 3 O'Reilly books:

Learning Perl 6th edition
Intermediate Perl 2nd edition
Mastering Perl 2nd edition

And then also picked up Programming Perl 4th edition.

I just started on Learning Perl and am just about to the chapter 3 quiz. 
I wrote about 17 tiny scripts to illustrate chapter 2.
Quite literally just getting started with Perl. 

The above is actually part of an admin course where they introduce us to 
scripting. A teeny tiny bit of bash, seriously like a mention of awk and 
the rest is Perl, which I find myself very attracted to. 

I read and performed all the exercises in a Dummies book for bash shell 
scripting and like that very much as well. Bought a book on sed and awk, 
and also a separate book on just awk programming. 

This is all fascinating for me. 

-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 22 Feb 2014 09:54:28 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <I4OdnRew3cqpVZXOnZ2dnUVZ_vOdnZ2d@supernews.com>

On Sat, 22 Feb 2014 15:44:21 +0000, Dave Saville wrote:

> On Sat, 22 Feb 2014 15:31:47 UTC, Marek Novotny <mach2@hushmail.com>
> wrote:
> 
>> On Sat, 22 Feb 2014 08:02:53 +0000, Kaz Kylheku wrote:
>> 
>> > On 2014-02-21, Marek Novotny <mach2@hushmail.com> wrote:
>> >> #!/usr/bin/perl # File: Obj8-2.pl use strict;
>> >> use warnings;
>> >>
>> >> my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
>> >> my $answer = 0;
>> > 
>> > Your algorithm only works for a non-empty array of non-negative
>> > numbers.
>> 
>> I just tested this code:
>> 
>> #!/usr/bin/perl # File: Obj8-2.pl use strict;
>> use warnings;
>> 
>> my @array=(-5,-4,-3,-1,-7,0,-53,-12,-5,-43,-29,-32,-18,-49);
>> my $answer = 0;
>> my $elem = 0;
>> 
>> foreach $elem (@array){
>> 	if ($elem > $answer){
>> 		$answer = $elem;
>> 	}
>> }
>> print "The Answer is: $answer.\n";
>> 
>> It yielded the corrected answer, that being zero.
> 
> Remove 0 from the array and try again............

Thank you. It indeed went berserk. :(


-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 22 Feb 2014 09:57:57 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <I4OdnRaw3cqYVJXOnZ2dnUVZ_vMAAAAA@supernews.com>

On Sat, 22 Feb 2014 09:31:48 -0600, John Black wrote:

> Honestly, I like Marek's second solution much better than all of yours. 
> Marek's code is very readable and easy to understand what its doing. 
> Sorry Rainer, but yours is a cryptic mess (IMO).  And so are all the
> other ones that use $_.  The only thing Marek needs to do to allow him
> to handle an array of all negative values is to set:
> 
> my $answer = $array[0];  # instead of = 0
> 
> John Black

#!/usr/bin/perl
# File: Obj8-2.pl
use strict;
use warnings;

my @array=(-5,-4,-3,-1,-7,-53,-12,-5,-43,-29,-32,-18,-49);
my $answer = $array[0];
my $elem = 0;

foreach $elem (@array){
	if ($elem > $answer){
		$answer = $elem;
	}
}
print "The Answer is: $answer.\n";

I made the change you suggested and it works well now with all negative 
numbers. 

What is so different about these two lines of code that made that 
difference?

my $answer = 0;
my $answer = $array[0];

Thank you!!

-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 22 Feb 2014 17:25:14 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: obj 8-2
Message-Id: <874n3rvz45.fsf@sable.mobileactivedefense.com>

John Black <johnblack@nospam.com> writes:
> In article <87r46w42t8.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com 
> says...
>> Marek Novotny <mach2@hushmail.com> writes:
>> > On Fri, 21 Feb 2014 19:03:10 +0000, Henry Law wrote:

[...]

>> > #!/usr/bin/perl
>> > # File: Obj8-2.pl
>> > use strict;
>> > use warnings;
>> >
>> > my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
>> > my $answer = 0;
>> > my $elem = 0;
>> >
>> > foreach $elem (@array){
>> > 	if ($elem > $answer){
>> > 		$answer = $elem;
>> > 	}
>> > }
>> > print "The Answer is: $answer.\n";
>> 
>> This obviously breaks down when the array contains numbers < 0.
>> 
>> @array = (42,41,42,31,54,42,56,57,46,58,59,60,34,61);
>> 
>> $max = $array[0];
>> $_ > $max and $max = $_ for @array[1 .. $#array];
>> 
>> print "The Answer is: $max\n"; 
>
> Honestly, I like Marek's second solution much better than all of
> yours.  Marek's code is very readable and easy to understand what its
> doing.  Sorry Rainer, but yours is a cryptic mess (IMO).

Referring to a single line of code with three expressions as "cryptic
mess" seems a little out of place (George's arithmetic solution was
relatively cryptic because of the 'if' hidden inside the the
abs-function but it was far to simple and obviously too organized that
it could be labeled as 'a mess'). Looking at it, what is used here is

	- a short-circuiting boolean operator for conditional evaluation
        - a statement modifier
        - $_ with it usual meaning of "the current thing"
        - an array slice expression using the integer range operator

These are all pretty basic Perl features and if they're sufficiently
unfamiliar to you that they seem 'cryptic' to you, that's not my fault.
Perl has sufficient expressive power that really simple things, like
finding the largest number in an array, can be expressed succinctly and
that's one of the reasons why I consider it a much more useful language
(to me, at least), than, say, Java, where nothing can be done without
adding two dozen lines of boilerplate code to it (if not more). That's
what makes a text complicated to read for me: Loads and loads and yet
more loads of words and in the end "Parturient montes, nascetur
ridiculus mus".




------------------------------

Date: Sat, 22 Feb 2014 17:42:03 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: obj 8-2
Message-Id: <fV45K0OBJxbE-pn2-ddgdds2IQ3U7@paddington.bear.den>

On Sat, 22 Feb 2014 15:57:57 UTC, Marek Novotny <mach2@hushmail.com> 
wrote:

>> 
> What is so different about these two lines of code that made that 
> difference?
> 
> my $answer = 0;
> my $answer = $array[0];

In the first case you are setting $answer to an arbitrary value which,
if you are looking for a maximum, needs to be *smaller* than anything 
in the data to be searched. 0 works for all positive numbers as all 
positive numbers are bigger than 0. But for negative numbers 0 is 
*bigger* than all of them - even if there is no 0 in the list as you 
found by removing it.

In the second case you are starting with a value you *know* is 
actually in the list. It matters not what that value is - just that it
is from the list. You could be really retentive and only search from 
the second [1] element onwards, but its hardly worth the bother to 
save one compare and an index increment.   

HTH
-- 
Regards
Dave Saville


------------------------------

Date: Sat, 22 Feb 2014 17:43:30 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: obj 8-2
Message-Id: <274nta-d0q.ln1@anubis.morrow.me.uk>


Quoth Marek Novotny <mach2@hushmail.com>:
> On Sat, 22 Feb 2014 09:31:48 -0600, John Black wrote:
> 
> > Honestly, I like Marek's second solution much better than all of yours. 
> > Marek's code is very readable and easy to understand what its doing. 
> > Sorry Rainer, but yours is a cryptic mess (IMO).  And so are all the
> > other ones that use $_.  The only thing Marek needs to do to allow him
> > to handle an array of all negative values is to set:
> > 
> > my $answer = $array[0];  # instead of = 0
> > 
> > John Black
> 
> #!/usr/bin/perl
> # File: Obj8-2.pl
> use strict;
> use warnings;
> 
> my @array=(-5,-4,-3,-1,-7,-53,-12,-5,-43,-29,-32,-18,-49);
> my $answer = $array[0];
> my $elem = 0;
> 
> foreach $elem (@array){
> 	if ($elem > $answer){
> 		$answer = $elem;
> 	}
> }
> print "The Answer is: $answer.\n";
> 
> I made the change you suggested and it works well now with all negative 
> numbers. 
> 
> What is so different about these two lines of code that made that 
> difference?
> 
> my $answer = 0;

What is $answer set to in this case?

> my $answer = $array[0];

What is $answer set to in this case?

What do you want $answer set to in the end? Is this value larger or
smaller than the value you started with?

Ben



------------------------------

Date: Sat, 22 Feb 2014 12:26:27 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <I4OdnRGw3cpOdpXOnZ2dnUVZ_vOdnZ2d@supernews.com>

On Sat, 22 Feb 2014 17:42:03 +0000, Dave Saville wrote:

> On Sat, 22 Feb 2014 15:57:57 UTC, Marek Novotny <mach2@hushmail.com>
> wrote:
> 
> 
>> What is so different about these two lines of code that made that
>> difference?
>> 
>> my $answer = 0;
>> my $answer = $array[0];
> 
> In the first case you are setting $answer to an arbitrary value which,
> if you are looking for a maximum, needs to be *smaller* than anything in
> the data to be searched. 0 works for all positive numbers as all
> positive numbers are bigger than 0. But for negative numbers 0 is
> *bigger* than all of them - even if there is no 0 in the list as you
> found by removing it.
> 
> In the second case you are starting with a value you *know* is actually
> in the list. It matters not what that value is - just that it is from
> the list. You could be really retentive and only search from the second
> [1] element onwards, but its hardly worth the bother to save one compare
> and an index increment.
> 
> HTH

That helps like asking for a dollar and getting a 53' container full of 
money. As soon as I read the first line I turned red. Of course!! Duh. 
Thank you!!!! Thank you so much. I really have to think more like a 
programmer. My mind set is not correct for this, yet. Now that I see it 
clearly I will remember this. Thank you! 



-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 22 Feb 2014 12:30:26 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <I4OdnRCw3cpfcZXOnZ2dnUVZ_vMAAAAA@supernews.com>

On Sat, 22 Feb 2014 17:43:30 +0000, Ben Morrow wrote:

> Quoth Marek Novotny <mach2@hushmail.com>:
>> On Sat, 22 Feb 2014 09:31:48 -0600, John Black wrote:
>> 
>> > Honestly, I like Marek's second solution much better than all of
>> > yours.
>> > Marek's code is very readable and easy to understand what its doing.
>> > Sorry Rainer, but yours is a cryptic mess (IMO).  And so are all the
>> > other ones that use $_.  The only thing Marek needs to do to allow
>> > him to handle an array of all negative values is to set:
>> > 
>> > my $answer = $array[0];  # instead of = 0
>> > 
>> > John Black
>> 
>> #!/usr/bin/perl # File: Obj8-2.pl use strict;
>> use warnings;
>> 
>> my @array=(-5,-4,-3,-1,-7,-53,-12,-5,-43,-29,-32,-18,-49);
>> my $answer = $array[0];
>> my $elem = 0;
>> 
>> foreach $elem (@array){
>> 	if ($elem > $answer){
>> 		$answer = $elem;
>> 	}
>> }
>> print "The Answer is: $answer.\n";
>> 
>> I made the change you suggested and it works well now with all negative
>> numbers.
>> 
>> What is so different about these two lines of code that made that
>> difference?
>> 
>> my $answer = 0;
> 
> What is $answer set to in this case?
> 
>> my $answer = $array[0];
> 
> What is $answer set to in this case?
> 
> What do you want $answer set to in the end? Is this value larger or
> smaller than the value you started with?
> 
> Ben

Thank you Ben! This is so appreciated. I'm humbled. I feel like a giant 
dope. I'm not used to thinking this way yet. So I want to apologize in 
advance for all the really stupid questions I will likely ask in the 
future. This should have been obvious to me. I'm glad I asked. I honestly 
didn't see it and had I not asked, I would have tried to employ it not 
really knowing why. You're questions are perfect switches for turning on 
the light bulb above my head. 

-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 22 Feb 2014 12:47:47 -0600
From: John Black <johnblack@nospam.com>
Subject: Re: obj 8-2
Message-Id: <MPG.2d72accb414424c89897b0@news.eternal-september.org>

In article <874n3rvz45.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com 
says...
> 
> John Black <johnblack@nospam.com> writes:
> > In article <87r46w42t8.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com 
> > says...
> >> Marek Novotny <mach2@hushmail.com> writes:
> >> > On Fri, 21 Feb 2014 19:03:10 +0000, Henry Law wrote:
> 
> [...]
> 
> >> > #!/usr/bin/perl
> >> > # File: Obj8-2.pl
> >> > use strict;
> >> > use warnings;
> >> >
> >> > my @array=(5,4,3,1,7,23,12,5,43,29,32,18,49);
> >> > my $answer = 0;
> >> > my $elem = 0;
> >> >
> >> > foreach $elem (@array){
> >> > 	if ($elem > $answer){
> >> > 		$answer = $elem;
> >> > 	}
> >> > }
> >> > print "The Answer is: $answer.\n";
> >> 
> >> This obviously breaks down when the array contains numbers < 0.
> >> 
> >> @array = (42,41,42,31,54,42,56,57,46,58,59,60,34,61);
> >> 
> >> $max = $array[0];
> >> $_ > $max and $max = $_ for @array[1 .. $#array];
> >> 
> >> print "The Answer is: $max\n"; 
> >
> > Honestly, I like Marek's second solution much better than all of
> > yours.  Marek's code is very readable and easy to understand what its
> > doing.  Sorry Rainer, but yours is a cryptic mess (IMO).
> 
> Referring to a single line of code with three expressions as "cryptic
> mess" seems a little out of place (George's arithmetic solution was
> relatively cryptic because of the 'if' hidden inside the the
> abs-function but it was far to simple and obviously too organized that
> it could be labeled as 'a mess'). Looking at it, what is used here is
> 
> 	- a short-circuiting boolean operator for conditional evaluation
>         - a statement modifier
>         - $_ with it usual meaning of "the current thing"
>         - an array slice expression using the integer range operator
> 
> These are all pretty basic Perl features and if they're sufficiently
> unfamiliar to you that they seem 'cryptic' to you, that's not my fault.
> Perl has sufficient expressive power that really simple things, like
> finding the largest number in an array, can be expressed succinctly

I know how your code works but I stand by what I said.  Some people like to sacrifice 
readability and maintainability to save one or two lines of code ("succinctly").  Burying a 
decision inside of a short circuit AND is one of those cute tricks people do to avoid using a 
simple IF but not something I would encourage people (especially beginner programmers) to do 
except for those few applications where it makes sense (this is not one).  Even using $#array 
is not necessary.  foreach $elem (@array) is clear and unmistakable.  I could probably show 
Marek's solution to someone who doesn't even know Perl and they'd be able to easily tell me 
exactly what it does.  Not so with yours.

John Black


------------------------------

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 4144
***************************************


home help back first fref pref prev next nref lref last post