Hi Jitendra,

some comments on your code:

On Sun, 29 Nov 2015 13:09:42 +0530
Jitendra Barik <jbarik...@gmail.com> wrote:

> Hi Sunita,
> 
> Please find the code snippet here:
> 
> @array = qw(1 2 3 4 5 4 3 2);

You are missing "use strict;", "use warnings;" and "my" declarations here:

http://perl-begin.org/tutorials/bad-elements/#no-strict-and-warnings

> foreach (@array){

You should avoid using "$_" as much as possible because it can get
clobbered/devastated very easily:

http://perl-begin.org/tutorials/bad-elements/#overuse_dollar_underscore

>             $myhash{$_} = $myhash{$_} + 1;

Perl also has "+=" and "++" and you should prefer to use them whenever possible:

        $myhash{$elem}++;

>              }
> while(($s,$k) = each(%myhash)){
> 
>           push @res,$k;
> }

This will not preserve the order of the elements as well as push the values of
the hash (which are the counts of the elements in the array) instead of the
keys.

You can also do a similar loop using map or in this case - keys.
 
>        print "Array in unique is : @res\n";
> 
> Regards,
> Jitendra
> 

Jitendra, I suggest you go over the page at
http://perl-begin.org/tutorials/bad-elements/ and incorporate most of these
practices into your Perl coding, because your Perl code exhibits many problems.
Showing bad Perl code here often does more harm than good because people may be
tempted to use it or learn from it.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish

Please reply to list if it's a mailing list post

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to