Solved

FREQUENCY LIST OF NAMES

Posted on 2003-10-22
6
186 Views
Last Modified: 2008-02-01
Dear Experts,

I would need your helpfull to build a script which would create a sorted frequency list
from a list of names, from a <textarea> to another <textarea>

For example:

FROM

<textarea>

Charles Letchev
Edmund Casparot
Ray Davies da Silva
Ronaldo Marcado
Noites e Dias da Silva
Maria Camilla Freindlich
Kenshin Himura
Satoro Sato
Jane Austin
Miguel da Silva
Edmund Casparot
Ronaldo marcado
Jane Austin
Charles Letchev
</textarea>

TO
<TEXTAREA>
Charles Letchev                  2
Edmund Casparot               2
Jane Austin                        2
Kenshin Himura                  1
Maria Camilla Freindlich      1
Miguel da Silva                   1
Noites e Dias da Silva          1
Ray Davies da Silva             1
Ronaldo Marcado                2
Satoro Sato                        1
</TEXTAREA>


Thank you very much.


soperl





</textarea>

0
Comment
Question by:soperl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:devic
ID: 9603103
hi soperl,

try this:
=========================================
<html>
<head>
      <title>Untitled</title>
      <META http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>

<body bgcolor=ffffff topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<script>
function runit()
{
      var mytext1=document.getElementById("t1");
      var mytext2=document.getElementById("t2");
      var mytextArr=mytext1.innerHTML.split("\n");
      mytextArr.sort();
      
      mytext2.innerHTML=mytextArr.join("\n");
}
</script>
<textarea id=t1 style=height:300px>
Charles Letchev
Edmund Casparot
Ray Davies da Silva
Ronaldo Marcado
Noites e Dias da Silva
Maria Camilla Freindlich
Kenshin Himura
Satoro Sato
Jane Austin
Miguel da Silva
Edmund Casparot
Ronaldo marcado
Jane Austin
Charles Letchev
</textarea>

<textarea id=t2 style=height:300px>
</textarea>

<button onclick=runit()>runit</button>
</body>
</html>
0
 
LVL 6

Expert Comment

by:ren_b
ID: 9603120
so what you want is to just put the number of times each name shows up in textarea 1, in textarea 2?

this is probably a bad way to do it, but it'd work:

you'd make textarea 1 a string...

var text1 = document.forms[0].elements[0]; //assuming its the first eleemnt in the form
var text2 = document.forms[0].elements[1]; //same deal

var text1_names = text1.value.split('\n');

for (var a=0;a<text1_names.length;a++){
  var names[a] = 0;
  for (var b=0;b<text1_names.length;b++){
    if (text1_names[a] == text1_names[b]){
      var name[b]=text1_name[b];
      names[b]++;
}}}
for (var c=0;c<name.length;c++)
  text2.value += names[c] + " " + name[c] + "\n";


i think that should work, though i haven't tested it
0
 

Author Comment

by:soperl
ID: 9605790

Dear devic,

Your function works fine, but I would need the frequencies side by side of names.

----------------------------------


Dear ren_b,

Yes, but deleting the lines in duplicate.

Please complete your function() to be most clear to me.

-----------------------------------

soperl
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
devic earned 260 total points
ID: 9615478

soperl, here is solution:
======================================
<html>
<head>
    <title>Untitled</title>
    <META http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>

<body bgcolor=ffffff topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<script>
function runit()
{
    var mytext1=document.getElementById("t1");
    var mytext2=document.getElementById("t2");
      var tmpspan=document.getElementById("tmpspan");
    var mytextArr=mytext1.innerHTML.replace(/\r/,"").split("\n");
      mytextArr.sort();
      tmpspan.innerHTML=countitems(mytextArr).join("<br>");
      mytext2.innerText=tmpspan.innerText;
}
function countitems(myarr)
{
      var origarr=myarr;
      var resarr=myarr;
      for(var i=0;i<myarr.length;i++)
      {
            var tmp=0;
            for(var j=0;j<origarr.length;j++)
            {
                  if(origarr[j].toLowerCase().indexOf(myarr[i].toLowerCase())!=-1)
                  {
                        tmp++;
                  }
            }
            resarr[i]=origarr[i]+" :"+tmp;
      }
      return removedublicate(myarr);
}
function removedublicate(myarr)
{
      var resarr=new Array();
      var j=0;
      for(var i=0;i<myarr.length;i++)
      {
            if(resarr.toString().toLowerCase().indexOf(myarr[i].toLowerCase())==-1)
            {
                  resarr[j]=myarr[i];
                  j++;
            }
      }
      return resarr;
}
</script>
<textarea id=t1 style=height:300px;width:400px;>
Charles Letchev
Edmund Casparot
Ray Davies da Silva
Ronaldo Marcado
Noites e Dias da Silva
Maria Camilla Freindlich
Kenshin Himura
Satoro Sato
Jane Austin
Miguel da Silva
Edmund Casparot
Ronaldo marcado
Jane Austin
Charles Letchev

</textarea>
<textarea id=t2 style=height:300px;width:400px;></textarea>
<div id=tmpspan style=display:none;></div><br>
<button onclick=runit()>runit</button>
</body>
</html>

======================================
if you want to be "Ronaldo marcado" not equal to "Ronaldo Marcado" (case-sensitive)
then remove .toLowerCase()
0
 
LVL 25

Expert Comment

by:devic
ID: 9626644
hi soperl,

did you solved your problem?
0
 

Author Comment

by:soperl
ID: 9628834
Dear devic,

No doubt! Solve my problem!

Thank you very much.

soperl,
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question