Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

see how many times a string is matched within a paragraph...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i have an array of sorts:

$listarray='4857|4/8/2009,9568|4/9/2009,2368|4/10/2009,3746|4/12/2009,4987|4/15/2009,7283|4/16/2009,7428|4/18/2009,4993|4/19/2009,9968|4/19/2009,9968|4/19/2009,9968|4/19/2009';

i'm trying to see if a certain element is repeated in $listarray, as i'm cycling through each element in the array.

i have this:

Code:
dupe=0;
@lagroup=split(/,/,$listarray);
foreach $lag (@lagroup) {
 if ($listarray =~ /$lag/) {$dupe++;}
}

this works if it's repeated, but since i want to know how many times it appears, since it will always be true because it i counting itself.

for instance, the string '9968|4/19/2009' appears 3 times, and i need to know that it appears 2 extra times.

any ideas?

thanks!

- g
 
Code:
$listarray='4857|4/8/2009,9568|4/9/2009,2368|4/10/2009,3746|4/12/2009,4987|4/15/2009,7283|4/16/2009,7428|4/18/2009,4993|4/19/2009,9968|4/19/2009,9968|4/19/2009,9968|4/19/2009';
@lagroup = split(/,/, $listarray);
$seen{$_}++ for @lagroup;

foreach( keys %seen ) {
  printf "$_ = $seen{$_}\n" if $seen{$_}>1;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top