Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FREQUENCY LIST OF NAMES

Posted on 2003-10-22
6
Medium Priority
?
188 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Accepted Solution

by:
devic earned 1040 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

618 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