CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by Alekto

 



Alekto
Novice

Jan 9, 2010, 1:13 PM

Post #1 of 3 (717 views)
Post deleted by Alekto

 


Alekto
Novice

Jan 9, 2010, 3:54 PM

Post #2 of 3 (707 views)
Re: [Alekto] Count elements in an array [In reply to] Can't Post

This is what I got this far:

Code
#!/usr/bin/perl 

open (IN, "< copy") or die ("can't open file: copy");
open (OUT, ">> testOut");

while ($line = <IN>){

@array = split(" ", $line);
@number = $array[2];
%count = ();
for (@number){
$count{$_}++;
}
foreach $keys (keys %count){
print OUT "$keys = $count{$keys} time\n";

}
}

close (IN);
close (OUT);


I'm reading the array form a file as you can see, but this code just gives me an output like this: (not the output I mentioned in the previous post)

Code
1 = 1 time 
1 = 1 time
1 = 1 time
2 = 1 time
2 = 1 time
2 = 1 time
2 = 1 time
2 = 1 time
3 = 1 time
3 = 1 time
4 = 1 time


Tnx!


(This post was edited by Alekto on Jan 9, 2010, 3:55 PM)


toolic
User

Jan 9, 2010, 6:51 PM

Post #3 of 3 (698 views)
Re: [Alekto] Count elements in an array [In reply to] Can't Post


Code
use strict; 
use warnings;
use Data::Dumper;

my @array = (1,2,3,1,4,2,2,1,2,3,2);
my %counts;
for (@array) { $counts{$_}++ }
print Dumper(\%counts);

__END__
$VAR1 = {
'4' => 1,
'1' => 3,
'3' => 2,
'2' => 5
};


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives