Solved

FREQUENCY LIST OF NAMES

Posted on 2003-10-22
6
179 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now