Solved

FREQUENCY LIST OF NAMES

Posted on 2003-10-22
6
182 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 62
How to resize a div in html 3 35
CORS Error from Chrome, but FireFox is OK 1 77
Character counter breaks after adding EmojiOne Area 4 19
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

791 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