Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: add missing elements and remove duplicates....and some more questions.

by AnomalousMonk (Archbishop)
on Apr 24, 2017 at 16:34 UTC ( [id://1188784]=note: print w/replies, xml ) Need Help??


in reply to Re^2: add missing elements and remove duplicates....and some more questions.
in thread add missing elements and remove duplicates....and some more questions.

Hi pritesh_ugrankar. If this was posted by you, then you have recognized the problem I wanted to bring to your attention. If it was not, please run the  do_it_all() function given here with the arguments  (1, 2, 3, 4) and see what you get.

And just as a general observation, code that looks great and doesn't work is worthless. I've written a lot of code that looked wonderful, but...


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: add missing elements and remove duplicates....and some more questions.
by pritesh_ugrankar (Monk) on Apr 24, 2017 at 20:04 UTC

    Hi,

    No that was not me.

    And I now understand what has happened. So I have changed the code. And you are right, just because my code looks good does not mean it's good.
    use strict; use warnings; my @arr = (1, 2, 3, 4); sub do_it_all { my $biggest = $_[0] ; foreach my $num (@_) { if ($num > $biggest) { $biggest = $num; } } my $smallest = $_[0]; foreach my $smallnum (@_) { if ($smallnum < $smallest) { $smallest = $smallnum; } } print "\$smallest = $smallest\t\$biggest = $biggest\n"; my @unique = ($smallest..$biggest); print "@unique\n"; } &do_it_all(@arr);

    And it now correctly prints:

    C:\>perl anomalous_func.pl $smallest = 1 $biggest = 4 1 2 3 4

    Update: I have updated the main thread with your observation. Thank you very much. I did a stupid mistake that should have been avoided at all costs.

    Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!
      A few additional tips. The indenting of your code levels should be consistent. The "smallest foreach loop" should be moved to the left to match the level of the "biggest". However, better would be to combine the 2 loops. Also Perl has a special single action syntax putting the "if" at the end of the statement. This can make the code easier to read with fewer curly braces required.

      Another big point: What you name variables DOES matter. @unique does not really describe what that is (these are not the uniques of the original array, this is the consecutive range. I would suggest @consecutive as an alternative name.

      use strict; use warnings; my @arr = (1,2,2,3,4,6); sub do_it_all { my $biggest = $_[0]; my $smallest = $_[0]; foreach my $num (@_) { $biggest = $num if ($num > $biggest); $smallest = $num if ($num < $smallest); } print "\$smallest = $smallest\t\$biggest = $biggest\n"; my @consecutive = ($smallest..$biggest); print "@consecutive\n"; } do_it_all(@arr); __END__ $smallest = 1 $biggest = 6 1 2 3 4 5 6
      Update: Instead of "do_it_all", perhaps "conseq_range" or similar would be better?

        Hi Marshall,

        Those are some awesome points. Thanks for the suggestions.

        Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1188784]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found