help with array elements
--0016e6d6464e06615f049e444d15
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I am a newbie to perl.
I have an array which has few elements like '1,2,3,4,2,3,1,2,1,1,1,4,6,7'
i need to know how many times each element has occurred in the same array.
for example 1-5 times
2-3 times...
could some one throw some light on how i can do this.
thanks in advance..
Regards
Ashwin Thayyullathil Surendran
--0016e6d6464e06615f049e444d15--
Re: help with array elements
--0015174c0b98e10361049e448d5c
Content-Type: text/plain; charset=UTF-8
>
>
> could some one throw some light on how i can do this.
> thanks in advance..
>
use strict;
use warnings;
my [at] array = (1,2,3,4,2,3,1,2,1,1,1,4,6,7);
my %hash;
foreach my $item ( [at] array){
if (exists $hash{$item}) {
$hash{$item} = $hash{$item} + 1;
} else {
$hash{$item} = 1;
}
}
print " [at] array\n";
while(my ($key,$val) = each %hash) {
print "$key => $val\n";
}
~Parag
--0015174c0b98e10361049e448d5c--
Re: help with array elements
Hello Ashwin,
> I have an array which has few elements like
> '1,2,3,4,2,3,1,2,1,1,1,4,6,7' i need to know how many times each
> element has occurred in the same array. for example 1-5 times 2-3
> times... could some one throw some light on how i can do this.
Store the array contents into a hash, updating the count each time:
=pod code
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my [at] elements = ( 1, 2, 3, 4, 2, 3, 1, 2, 1, 1, 1, 4, 6, 7, );
my %count;
$count{$_}++ for [at] elements;
print Dumper \%count;
=cut
Regards,
Alan Haggai Alavi.
--
The difference makes the difference
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
>>>>> "PK" == Parag Kalra <paragkalra [at] gmail.com> writes:
PK> my [at] array = (1,2,3,4,2,3,1,2,1,1,1,4,6,7);
PK> my %hash;
PK> foreach my $item ( [at] array){
PK> if (exists $hash{$item}) {
PK> $hash{$item} = $hash{$item} + 1;
PK> } else {
PK> $hash{$item} = 1;
PK> }
PK> }
this replaces the entire if/else statement:
$hash{$item}++ ;
perl will automatically make undef into 0 without warnings when using
++. there is no need to initialize it the first time. and the rest of
the time it will just increment it. it is a standard perl idiom you need
to know so you don't waste code like that.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
On 2011-03-12 13:17, ashwin ts wrote:
>Hi,
Hello,
>I have an array which has few elements like '1,2,3,4,2,3,1,2,1,1,1,4,6,7'
>i need to know how many times each element has occurred in the same array.
>for example 1-5 times
> 2-3 times...
Use a hash.
#!/usr/bin/perl
use strict;
use warnings;
my [at] array = (1, 1, 2, 4, 2, 1, 9, 42, 0, 0, 0, 1, 0, 0, 2);
my %count;
$count{$_}++ for [at] array;
for my $c( sort{ $b <=> $a } keys(%count) ) {
printf("% 3d appeared %d times\n", $c, $count{$c});
}
Output:
42 appeared 1 times
9 appeared 1 times
4 appeared 1 times
2 appeared 3 times
1 appeared 4 times
0 appeared 5 times
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
On Sat, Mar 12, 2011 at 09:20:01AM +0100, magnus [at] trapd00r.se wrote:
> On 2011-03-12 13:17, ashwin ts wrote:
>
> >I have an array which has few elements like '1,2,3,4,2,3,1,2,1,1,1,4,6,7'
> >i need to know how many times each element has occurred in the same array.
> >for example 1-5 times
> > 2-3 times...
>
> Use a hash.
.... says everyone.
If the elements are actually like you say they are, don't use a hash - use
an array.
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
--e0cb4e43cd57247073049e4ae96b
Content-Type: text/plain; charset=ISO-8859-1
Hi,
Thank you all for the support.It helped me a lot.i tried working with arrays
as well as with hash.Both the approaches works fine in the case of a small
array with few elements.
When i tried the same for a large array , i started getting "out of memory"
error.
Any suggestions would be of great help.
Thanks again,
Regards
Ashwin Thayyullathil Surendran
---------- Forwarded message ----------
From: ashwin ts <ashwints17 [at] gmail.com>
Date: Sat, Mar 12, 2011 at 1:17 PM
Subject: help with array elements
To: beginners [at] perl.org
Hi,
I am a newbie to perl.
I have an array which has few elements like '1,2,3,4,2,3,1,2,1,1,1,4,6,7'
i need to know how many times each element has occurred in the same array.
for example 1-5 times
2-3 times...
could some one throw some light on how i can do this.
thanks in advance..
Regards
Ashwin Thayyullathil Surendran
--e0cb4e43cd57247073049e4ae96b--
Re: help with array elements
On 12/03/2011 15:41, ashwin ts wrote:
>
> Thank you all for the support.It helped me a lot.i tried working with arrays
> as well as with hash.Both the approaches works fine in the case of a small
> array with few elements.
> When i tried the same for a large array , i started getting "out of memory"
> error.
>
> Any suggestions would be of great help.
Please show your code. The methods should be fine for all but a
gargantuan array.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
In article
<AANLkTi=afY09Dh8c2F3tmnCjcM4OM1iHgutAcALn_W5- [at] mail.gmail.com>,
ashwints17 [at] gmail.com (ashwin ts) wrote:
> Hi,
>
> I am a newbie to perl.
> I have an array which has few elements like '1,2,3,4,2,3,1,2,1,1,1,4,6,7'
> i need to know how many times each element has occurred in the same array.
> for example 1-5 times
> 2-3 times...
> could some one throw some light on how i can do this.
> thanks in advance..
>
> Regards
> Ashwin Thayyullathil Surendran
quick write, not tested :
my [at] array = (1,2,3,4,2,3,1,2,1,1,1,4,6,7);
my [at] freq;
for ( [at] array) { $freq[$_]++;}
my $i=0;
for ( [at] freq) {
print "$i : $_ times\n";
$i++;
}
--
klp
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
On 11-03-12 10:56 AM, kurtz le pirate wrote:
> quick write, not tested
I suggest you test it.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
At 03:14 -0500 12/03/2011, Uri Guttman wrote:
>this replaces the entire if/else statement:
>
> $hash{$item}++ ;
>
>...it is a standard perl idiom you need
>to know so you don't waste code like that.
Nice!
so
#!/usr/local/bin/perl
use strict;
my %hash;
my [at] array = split //, 12342312111467;
for ( [at] array){
$hash{$_}++
}
for (sort keys %hash){
print "$_ - $hash{$_}x\n"
}
1 - 5x
2 - 3x
3 - 2x
4 - 2x
6 - 1x
7 - 1x
JD
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
On Sat, Mar 12, 2011 at 05:31:06PM +0000, Rob Dixon wrote:
> On 12/03/2011 15:41, ashwin ts wrote:
> >
> >Thank you all for the support.It helped me a lot.i tried working with arrays
> >as well as with hash.Both the approaches works fine in the case of a small
> >array with few elements.
> >When i tried the same for a large array , i started getting "out of memory"
> >error.
> >
> >Any suggestions would be of great help.
>
> Please show your code. The methods should be fine for all but a
> gargantuan array.
I suspect you mean that when you store the counts in an array and your
input has large values then you hit "Out of memory!" problems. In that
case an array is not a suitable data structure for your problem and you
should be using a hash.
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help with array elements
On Sat, Mar 12, 2011 at 01:35:31PM -0500, Shawn H Corey wrote:
> On 11-03-12 10:56 AM, kurtz le pirate wrote:
> >quick write, not tested
>
> I suggest you test it.
Is this general advice or do you see a problem with the code? If it is
the former, I agree. If it is the latter, please point out the problem.
Thanks,
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/