Front page | perl.beginners |
Postings from April 2002
Re: Array question
Thread Previous
|
Thread Next
From:
Aman Raheja
Date:
April 1, 2002 11:12
Subject:
Re: Array question
Message ID:
F1258oh41YDgWXPbCsn0000e289@hotmail.com
Here's my solution.
There will be shorter ways. I am new to perl, so this is how did it.
At the prompt do
$array-prob.pl <datafile>
#!/usr/bin/perl
#File name : array-prob.pl
my @arr;
while(<>)
{
chomp($_);
$field{$_}++;
print "$_ $field{$_}\n";
my $set = 0;
my $reset = 1;
foreach $element (@arr) {
if($_ ne $element) {
$set = 1;
}
else {
$reset = 0;
}
}
if($set == 1 || $arr == 0) {
if($reset != 0) {
push(@arr,$_);
}
}
}
print "\n array - is @arr";
print "\n Repeats are : \n";
foreach $element (@arr) {
print "$element was $field{$element} times\n";
}
c'ya
Aman
>From: "Allison Ogle" <aogle@ti.com>
>To: "a a" <beginners@perl.org>
>Subject: Array question
>Date: Mon, 1 Apr 2002 11:08:00 -0500
>
>Hi,
>
>I have a datafile with a list of names like
>
>Ana
>John
>Mike
>Tracy
>John
>Luke
>....
>etc.
>
>I don't know how long the list is and eventually in the list some of the
>names will repeat. I want to put these names in an array but I don't want
>to repeat any names in the array and I want to keep a count of how many
>times the name appears in the list. Does anyone have any ideas or
>suggestions? Thanks,
>
>Allison
>
>
>--
>To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>For additional commands, e-mail: beginners-help@perl.org
>
_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com
Thread Previous
|
Thread Next