How to Convert text list into 2d array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew V. Romero

    #1

    How to Convert text list into 2d array

    At work we have an excel file that contains the list of medications and
    their corresponding strengths. I would like to save the excel file as a
    text list and paste this list into a javascript function and have JS put
    this into an array. Then JS would use this array to create a selection
    list which displays only the names of the drugs. When the user
    selections one of the drugs, another selection list will be loaded with
    the avaiable strengths of that drug. The biggest problem I see if how
    to get the list into a javascript array. The list will look something
    like (only a lot longer):

    Acetaminophen tablets
    * 80mg chewable
    * 325mg caplet
    * 500mg caplet
    Acetaminophen supp.
    "* 80mg, 120mg, 325mg, 650mg"
    Acetaminophen liquid
    * 650mg/20.3ml
    Acetazolomide tablets
    * 250mg scored
    Acyclovir capsules
    "* 200mg, 800mg"
    Adeks tablets
    Allbee + C capsules
    Aspirin tablets
    * 40.5mg chewable
    * 81mg chewable
    * 81mg enteric coated
    * 325mg enteric coated
    * 325mg buffered

    I am not aware of a way in JS to simply paste this data as is into a
    function and have the function read it and put it into an array. Is
    there a way I could put the whole list in "" and treat it as a string
    and then do an split on the string at each line break? Seeing as the
    list is really long, I don't really want to have to type
    array.push(valu e) on each and every line.

    Even if this works, I will still likely need a way to turn the data into
    a 2d array so that for example:
    list[0][0] = acetaminophen tablets
    list [0][1] = 80mg chewable, 325 caplet, 500mg caplet
    list [1][0] = acetaminophen supp.
    etc.

    I imagine you could probably do this with some regular expression work
    (which I am not very good at, but this would be a good chance to learn).
    I just wanted to throw this idea out there and see if anyone has any
    ideas on the best way to go about trying to do this. If nothing else, I
    could always just not make the web page very exciting and use excel to
    save as web page and have people press control+f to find a drug, but
    that is just so boring.

    Thanks for your thoughts,
    Andrew V. Romero

    PS: I know a server side langauge would be much better at doing this,
    but I have no server side langauge avaiable on the company web server
    (not my department).

  • Dan Brussee

    #2
    Re: How to Convert text list into 2d array

    In article <3F21D69F.90801 07@icqmail.com> , rrstudio2@icqma il.com says...[color=blue]
    > At work we have an excel file that contains the list of medications and
    > their corresponding strengths. I would like to save the excel file as a
    > text list and paste this list into a javascript function and have JS put
    > this into an array. Then JS would use this array to create a selection
    > list which displays only the names of the drugs. When the user
    > selections one of the drugs, another selection list will be loaded with
    > the avaiable strengths of that drug. The biggest problem I see if how
    > to get the list into a javascript array. The list will look something
    > like (only a lot longer):
    >
    > Acetaminophen tablets
    > * 80mg chewable
    > * 325mg caplet
    > * 500mg caplet
    > Acetaminophen supp.
    > "* 80mg, 120mg, 325mg, 650mg"
    > Acetaminophen liquid
    > * 650mg/20.3ml
    > Acetazolomide tablets
    > * 250mg scored
    > Acyclovir capsules
    > "* 200mg, 800mg"
    > Adeks tablets
    > Allbee + C capsules
    > Aspirin tablets
    > * 40.5mg chewable
    > * 81mg chewable
    > * 81mg enteric coated
    > * 325mg enteric coated
    > * 325mg buffered
    >
    > I am not aware of a way in JS to simply paste this data as is into a
    > function and have the function read it and put it into an array. Is
    > there a way I could put the whole list in "" and treat it as a string
    > and then do an split on the string at each line break? Seeing as the
    > list is really long, I don't really want to have to type
    > array.push(valu e) on each and every line.
    >
    > Even if this works, I will still likely need a way to turn the data into
    > a 2d array so that for example:
    > list[0][0] = acetaminophen tablets
    > list [0][1] = 80mg chewable, 325 caplet, 500mg caplet
    > list [1][0] = acetaminophen supp.
    > etc.
    >
    > I imagine you could probably do this with some regular expression work
    > (which I am not very good at, but this would be a good chance to learn).
    > I just wanted to throw this idea out there and see if anyone has any
    > ideas on the best way to go about trying to do this. If nothing else, I
    > could always just not make the web page very exciting and use excel to[/color]

    Consider using an XML data island. In HTML you can have a tag of:
    <xml id=xmlMeds>
    <meds>
    <med type="Acetamino phen tablets">
    <dose>80mg chewable</dose>
    <dose>325 caplet</dose>
    <dose>500mg caplet</dose>
    </med>
    <med type="Another type">
    <dose>blah</dose>
    </med>
    </meds>
    </xml>

    From Javascript, you can do something like..
    <script type="text/javascript">
    function getdoses(medtyp e) {
    var nodes = xmlMeds.selectN odes("/meds/med [" + medtype + "]/dose");
    for (var x = 0; x < nodes.length; x++) {
    // do what you need to do
    }
    }
    </script>

    Warning: Air Code!!!


    --

    Remove NOT from email address to reply. AntiSpam in action.

    Comment

    • Fox

      #3
      Re: How to Convert text list into 2d array

      It's a two step process -- convert the excel file (Save As...) to a CSV
      or TSV (tab separated values, which the following script uses) text
      document -- open it up in a text editor and select all and copy.

      Run the following script and paste the excel data into the textarea --
      click the convert button (when it's done, copy and paste the entire
      contents into an html or js file) -- your excel data will be converted
      easily into javascript format.

      [* this script should handle several hundred lines easily (I converted
      an excel file with 730 lines with 13 fields each very easily with this
      script (less than a couple of seconds) -- If you have to, you can break
      up the excel tsv/csv file into chunks and assemble the final JS array by pieces]

      [** using TSV removes the problem of commas that might be used within
      the data, as in addresses or business names (e.g.: business, inc. etc...)]

      [*** any "empty" field becomes an empty string]

      <xmp><!-- remove this line to run -->

      <title>Spreadsh eet to JS Converter</title>
      <script language = javascript>

      function
      formatData(e)
      {
      // Excel sometimes adds quotes where none were entered
      // so clear those
      e=e.replace(/"/g,"");

      // this script uses tab delimited data
      // or use /,/g as the regex for comma separated values (csv)
      e = e.replace(/\t/g, '","');

      // this just handles each new line and does a little formatting
      // so it's "neat" when you paste it into your html/js file
      e = e.replace(/[\n\r]/g, '"],\n\t\t\t["');

      return "var spreadsheetData = [\n\t\t\t[\"" + e + "\"]\n];";

      }

      function
      handler(f)
      {
      f.data.value = formatData(f.da ta.value);
      return false;

      }


      </script>

      <body>
      <center>
      <h1>Spreadshe et to JS Converter</h1>
      <form name=exchanger onsubmit = "return handler(this)">
      <textarea name=data rows = 24 cols = 100></textarea>
      <br><br>
      <input type=submit value = Convert onclick = "return handler(this.fo rm)">
      </form>
      </center>
      </body>

      </xmp><!-- remove this line to run -->

      "Andrew V. Romero" wrote:[color=blue]
      >
      > At work we have an excel file that contains the list of medications and
      > their corresponding strengths. I would like to save the excel file as a
      > text list and paste this list into a javascript function and have JS put
      > this into an array. Then JS would use this array to create a selection
      > list which displays only the names of the drugs. When the user
      > selections one of the drugs, another selection list will be loaded with
      > the avaiable strengths of that drug. The biggest problem I see if how
      > to get the list into a javascript array. The list will look something
      > like (only a lot longer):
      >
      > Acetaminophen tablets
      > * 80mg chewable
      > * 325mg caplet
      > * 500mg caplet
      > Acetaminophen supp.
      > "* 80mg, 120mg, 325mg, 650mg"
      > Acetaminophen liquid
      > * 650mg/20.3ml
      > Acetazolomide tablets
      > * 250mg scored
      > Acyclovir capsules
      > "* 200mg, 800mg"
      > Adeks tablets
      > Allbee + C capsules
      > Aspirin tablets
      > * 40.5mg chewable
      > * 81mg chewable
      > * 81mg enteric coated
      > * 325mg enteric coated
      > * 325mg buffered
      >
      > I am not aware of a way in JS to simply paste this data as is into a
      > function and have the function read it and put it into an array. Is
      > there a way I could put the whole list in "" and treat it as a string
      > and then do an split on the string at each line break? Seeing as the
      > list is really long, I don't really want to have to type
      > array.push(valu e) on each and every line.
      >
      > Even if this works, I will still likely need a way to turn the data into
      > a 2d array so that for example:
      > list[0][0] = acetaminophen tablets
      > list [0][1] = 80mg chewable, 325 caplet, 500mg caplet
      > list [1][0] = acetaminophen supp.
      > etc.
      >
      > I imagine you could probably do this with some regular expression work
      > (which I am not very good at, but this would be a good chance to learn).
      > I just wanted to throw this idea out there and see if anyone has any
      > ideas on the best way to go about trying to do this. If nothing else, I
      > could always just not make the web page very exciting and use excel to
      > save as web page and have people press control+f to find a drug, but
      > that is just so boring.
      >
      > Thanks for your thoughts,
      > Andrew V. Romero
      >
      > PS: I know a server side langauge would be much better at doing this,
      > but I have no server side langauge avaiable on the company web server
      > (not my department).[/color]

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: How to Convert text list into 2d array

        "Mosley Jones III" <me@info.gov.ma rs> writes:
        [color=blue]
        > I make 2d or 3d arrays in JS like this.
        >
        >
        > I make up a fake tag with attribuits, and put them in the head section
        >
        > <fakeTag att1=value1 att2=name1 att3=address1 ></fakeTag>
        > <fakeTag att1=value2 att2=name2 att3=address2 ></fakeTag>
        > <fakeTag att1=value3 att2=name3 att3=address3 ></fakeTag>[/color]
        ....[color=blue]
        > var dis = getElementByTag Name('fakeTag')[/color]

        var dis = document.getEle mentsByTagName( 'fakeTag');
        [color=blue]
        >
        > so dis(0).att1 would = value1[/color]

        Actually not in all browsers, but
        dis[0].getAttribute(" att1")
        would give "value1".
        In Mozilla, the result of documents.getEl ementsByTagName is not a function.
        In neither Mozilla nor Opera is "att1" a property of the fake elements,
        so you must use "getAttribu te".


        If you are going to invent your own HTML extension (which obviously
        won't allow the page to validate), then you might as well use XML.

        Still, neither are very good ideas if you want all browsers to
        understand it. If you embed the arrays in javascript, then all
        browsers that understand Javascript will also be able to access the
        table.

        All modern browsers seem to accept your fakeTag method, but ofcourse
        NS4 doesn't. It would understand a Javascript literal as the one
        Douglas Crockford gave as example.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Mosley Jones III

          #5
          Re: How to Convert text list into 2d array


          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:r84da6f0.f sf@hotpop.com.. .[color=blue]
          > "Mosley Jones III" <me@info.gov.ma rs> writes:
          >[color=green]
          > > I make 2d or 3d arrays in JS like this.
          > >
          > >
          > > I make up a fake tag with attribuits, and put them in the head section
          > >
          > > <fakeTag att1=value1 att2=name1 att3=address1 ></fakeTag>
          > > <fakeTag att1=value2 att2=name2 att3=address2 ></fakeTag>
          > > <fakeTag att1=value3 att2=name3 att3=address3 ></fakeTag>[/color]
          > ...[color=green]
          > > var dis = getElementByTag Name('fakeTag')[/color]
          >
          > var dis = document.getEle mentsByTagName( 'fakeTag');
          >[/color]

          thank you

          [color=blue][color=green]
          > >
          > > so dis(0).att1 would = value1[/color]
          >
          > Actually not in all browsers, but
          > dis[0].getAttribute(" att1")
          > would give "value1".
          > In Mozilla, the result of documents.getEl ementsByTagName is not a[/color]
          function.[color=blue]
          > In neither Mozilla nor Opera is "att1" a property of the fake elements,
          > so you must use "getAttribu te".
          >
          >
          > If you are going to invent your own HTML extension (which obviously
          > won't allow the page to validate), then you might as well use XML.
          >
          > Still, neither are very good ideas if you want all browsers to
          > understand it. If you embed the arrays in javascript, then all
          > browsers that understand Javascript will also be able to access the
          > table.
          >
          > All modern browsers seem to accept your fakeTag method, but ofcourse
          > NS4 doesn't. It would understand a Javascript literal as the one
          > Douglas Crockford gave as example.[/color]

          it should if you use the NN4 DOM

          [color=blue]
          >
          > /L
          > --
          > Lasse Reichstein Nielsen - lrn@hotpop.com
          > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          > 'Faith without judgement merely degrades the spirit divine.'[/color]


          Comment

          • Mosley Jones III

            #6
            Re: How to Convert text list into 2d array


            "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
            news:3cgs8d2r.f sf@hotpop.com.. .[color=blue]
            > "Mosley Jones III" <me@info.gov.ma rs> writes:
            >[color=green]
            > > "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
            > > news:r84da6f0.f sf@hotpop.com.. .[color=darkred]
            > > > "Mosley Jones III" <me@info.gov.ma rs> writes:
            > > >
            > > > > I make 2d or 3d arrays in JS like this.
            > > > >
            > > > >
            > > > > I make up a fake tag with attribuits, and put them in the head[/color][/color][/color]
            section[color=blue][color=green][color=darkred]
            > > > >
            > > > > <fakeTag att1=value1 att2=name1 att3=address1 ></fakeTag>
            > > > > <fakeTag att1=value2 att2=name2 att3=address2 ></fakeTag>
            > > > > <fakeTag att1=value3 att2=name3 att3=address3 ></fakeTag>
            > > > ...
            > > > > var dis = getElementByTag Name('fakeTag')
            > > >
            > > > var dis = document.getEle mentsByTagName( 'fakeTag');
            > > >[/color]
            > >
            > > thank you
            > >
            > >[color=darkred]
            > > > >
            > > > > so dis(0).att1 would = value1
            > > >
            > > > Actually not in all browsers, but
            > > > dis[0].getAttribute(" att1")
            > > > would give "value1".
            > > > In Mozilla, the result of documents.getEl ementsByTagName is not a[/color]
            > > function.[color=darkred]
            > > > In neither Mozilla nor Opera is "att1" a property of the fake[/color][/color][/color]
            elements,[color=blue][color=green][color=darkred]
            > > > so you must use "getAttribu te".
            > > >
            > > >
            > > > If you are going to invent your own HTML extension (which obviously
            > > > won't allow the page to validate), then you might as well use XML.
            > > >
            > > > Still, neither are very good ideas if you want all browsers to
            > > > understand it. If you embed the arrays in javascript, then all
            > > > browsers that understand Javascript will also be able to access the
            > > > table.
            > > >
            > > > All modern browsers seem to accept your fakeTag method, but ofcourse
            > > > NS4 doesn't. It would understand a Javascript literal as the one
            > > > Douglas Crockford gave as example.[/color]
            > >
            > > it should if you use the NN4 DOM[/color]
            >
            > That is the problem. There is no way in the NN4 DOM to access
            > arbitrary elements. In order to access elements through the
            > document.layers collection, they must be absolutely positioned,
            > which makes little sense for elements in the head element.[/color]

            yes it does, it would make no sence to put them in the body


            There[color=blue]
            > is no equivalent to getElementsByTa gName.
            >
            > /L
            > --
            > Lasse Reichstein Nielsen - lrn@hotpop.com
            > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            > 'Faith without judgement merely degrades the spirit divine.'[/color]


            Comment

            Working...