Re: uniq array creation

2015-11-29 Thread Paul Johnson
On Sun, Nov 29, 2015 at 03:30:53PM +0530, Pritish Pattanaik wrote:

> *Remove duplicate elelments from an array, It will maintain the original
> order*
> 
> __CODE__
> @array = qw(11 2 3 4 55 4 3 2);
> %hash = ();
> for(my $i=0;$i<=$#array;$i++){
> # store the position from array. use array element as key n position as
> value
> $hash{$array[$i]} = $i if ( ! exists $hash{$array[$i]} ) ;
> }
> @array = ();
> push(@array, $_) for (sort { $hash{$a} <=> $hash{$b}}  keys %hash ) ;
> print "Array:@array\n";
> __END__

Ooh, you're working far too hard!

  my @array = qw(11 2 3 4 55 4 3 2);
  my %seen;
  my @unique = grep !$seen{$_}++, @array;

This method is mentioned in the Perl Cookbook that was linked to earlier
in the thread.  But I doubt that link should have been online, so get
hold of a legal copy if this is useful to you.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: uniq array creation

2015-11-29 Thread Shlomi Fish
Hi Jitendra,

some comments on your code:

On Sun, 29 Nov 2015 13:09:42 +0530
Jitendra Barik  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/




Re: uniq array creation

2015-11-29 Thread Pritish Pattanaik
Hi,


*Remove duplicate from an array using hash,  It will disorder the original
position of an array *
__CODE__
my @array = qw(11 2 3 4 55 4 3 2);
my %hash;
map { $hash{$_}++ } @array;
@array = keys %hash;
print "Array:@array\n";
__END__





*Remove duplicate elelments from an array, It will maintain the original
order*

__CODE__
@array = qw(11 2 3 4 55 4 3 2);
%hash = ();
for(my $i=0;$i<=$#array;$i++){
# store the position from array. use array element as key n position as
value
$hash{$array[$i]} = $i if ( ! exists $hash{$array[$i]} ) ;
}
@array = ();
push(@array, $_) for (sort { $hash{$a} <=> $hash{$b}}  keys %hash ) ;
print "Array:@array\n";
__END__


Regards,
Pritish



On Sun, Nov 29, 2015 at 1:09 PM, Jitendra Barik  wrote:

> Hi Sunita,
>
> Please find the code snippet here:
>
> @array = qw(1 2 3 4 5 4 3 2);
> foreach (@array){
> $myhash{$_} = $myhash{$_} + 1;
>  }
> while(($s,$k) = each(%myhash)){
>
>   push @res,$k;
> }
>print "Array in unique is : @res\n";
>
> Regards,
> Jitendra
>
> On Thu, Nov 26, 2015 at 10:46 PM, Mike Flannigan  wrote:
>
>>
>> See if this meets your needs:
>> http://www.arl.wustl.edu/projects/fpx/references/perl/cookbook/ch04_07.htm
>>
>>
>> Mike
>>
>>
>> On 11/25/2015 1:53 AM, beginners-digest-h...@perl.org wrote:
>>
>>
>>
>>
>>
>> Hi
>>
>> I want to create a unique array .
>>
>> I have the code below. It is creating a array which will have duplicate
>> data . How can I filter duplicate data ?
>>
>> #!/usr/bin/perl
>>
>> use Data::Dumper;
>>
>>
>>
>> #my $cnt = scalar @$net_int_parsed_output; $net_int_parsed_output =
>> [lif_1 ,lif_2,lif_3,lif_4,lif_1,lif_2]; foreach my $n
>> (@$net_int_parsed_output){
>>
>> my $lif_cnt++;
>>
>> print "lif of vserver >>> $n\n";
>>
>> push
>> (@{$lun_PR_resv_info->{'lun'}->{'vserver'}->{'fcp_lifs'}},$n);
>>
>> }
>>
>>
>>
>> print "lun PR resv related info>",Dumper
>> $lun_PR_resv_info,"\n";
>>
>>
>> Thanks
>>
>> Sunita
>>
>>
>>
>
>
> --
> Regards,
> Jitendra
>


Re: uniq array creation

2015-11-28 Thread Jitendra Barik
Hi Sunita,

Please find the code snippet here:

@array = qw(1 2 3 4 5 4 3 2);
foreach (@array){
$myhash{$_} = $myhash{$_} + 1;
 }
while(($s,$k) = each(%myhash)){

  push @res,$k;
}
   print "Array in unique is : @res\n";

Regards,
Jitendra

On Thu, Nov 26, 2015 at 10:46 PM, Mike Flannigan  wrote:

>
> See if this meets your needs:
> http://www.arl.wustl.edu/projects/fpx/references/perl/cookbook/ch04_07.htm
>
>
> Mike
>
>
> On 11/25/2015 1:53 AM, beginners-digest-h...@perl.org wrote:
>
>
>
>
>
> Hi
>
> I want to create a unique array .
>
> I have the code below. It is creating a array which will have duplicate
> data . How can I filter duplicate data ?
>
> #!/usr/bin/perl
>
> use Data::Dumper;
>
>
>
> #my $cnt = scalar @$net_int_parsed_output; $net_int_parsed_output = [lif_1
> ,lif_2,lif_3,lif_4,lif_1,lif_2]; foreach my $n (@$net_int_parsed_output){
>
> my $lif_cnt++;
>
> print "lif of vserver >>> $n\n";
>
> push
> (@{$lun_PR_resv_info->{'lun'}->{'vserver'}->{'fcp_lifs'}},$n);
>
> }
>
>
>
> print "lun PR resv related info>",Dumper
> $lun_PR_resv_info,"\n";
>
>
> Thanks
>
> Sunita
>
>
>


-- 
Regards,
Jitendra


Re: uniq array creation

2015-11-26 Thread Mike Flannigan


See if this meets your needs:
http://www.arl.wustl.edu/projects/fpx/references/perl/cookbook/ch04_07.htm


Mike


On 11/25/2015 1:53 AM, beginners-digest-h...@perl.org wrote:





Hi

I want to create a unique array .

I have the code below. It is creating a array which will have 
duplicate data . How can I filter duplicate data ?


#!/usr/bin/perl

use Data::Dumper;

#my $cnt = scalar @$net_int_parsed_output; $net_int_parsed_output = 
[lif_1 ,lif_2,lif_3,lif_4,lif_1,lif_2]; foreach my $n 
(@$net_int_parsed_output){


my $lif_cnt++;

print "lif of vserver >>> $n\n";

push (@{$lun_PR_resv_info->{'lun'}->{'vserver'}->{'fcp_lifs'}},$n);

}

print "lun PR resv related info>",Dumper 
$lun_PR_resv_info,"\n";



Thanks

Sunita





Re: uniq array creation

2015-11-25 Thread Shlomi Fish
Hi Sunita,

On Wed, 25 Nov 2015 13:23:24 +0530
Sunita Pradhan  wrote:

> Hi 
> I want to create a unique array . 

You probably mean an "array with unique elements". To do so, you should use a
hash. See:

http://perl-begin.org/topics/hashes/

> I have the code below. It is creating a array which will have duplicate
> data . How can I filter duplicate data ? 

Some comments on your code:

1. The indentation is erratic and inconsistent. See:

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

2. It lacks "use strict;" and "use warnings;" . See:

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

3. "my $lif_cnt++;" does not make sense.

Perhaps go over the rest of the page -
http://perl-begin.org/tutorials/bad-elements/ .

Regards,

Shlomi Fish

> #!/usr/bin/perl
> 
> use Data::Dumper;
> 
>  
> 
> #my $cnt = scalar @$net_int_parsed_output;
> $net_int_parsed_output = [lif_1 ,lif_2,lif_3,lif_4,lif_1,lif_2]; foreach my $n
> (@$net_int_parsed_output){
> 
> my
> $lif_cnt++;
> 
> print
> "lif of vserver >>> $n\n";
> 
>
> push
> (@{$lun_PR_resv_info->{'lun'}->{'vserver'}->{'fcp_lifs'}},$n);
> 
> }
> 
>  
> 
> print "lun PR resv related
> info>",Dumper  
> $lun_PR_resv_info,"\n";
> ThanksSunita


-- 
-
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/




uniq array creation

2015-11-24 Thread Sunita Pradhan
Hi 
I want to create a unique array . 
I have the code below. It is creating a array which will have duplicate data . 
How can I filter duplicate data ?
#!/usr/bin/perl

use Data::Dumper;

 

#my $cnt = scalar @$net_int_parsed_output;
$net_int_parsed_output = [lif_1 ,lif_2,lif_3,lif_4,lif_1,lif_2]; foreach my $n
(@$net_int_parsed_output){

my
$lif_cnt++;

print
"lif of vserver >>> $n\n";

   
push
(@{$lun_PR_resv_info->{'lun'}->{'vserver'}->{'fcp_lifs'}},$n);

}

 

print "lun PR resv related
info>",Dumper
$lun_PR_resv_info,"\n";
ThanksSunita