Changing an array value to upper case?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike S. Nowostawsky

    #1

    Changing an array value to upper case?

    I tried using the "toUpperCas e()" property to change the value of an array
    entity to uppercase BUT it tells me that the property is invalid. It seems
    that an array is not considered an object when it is assigned a text
    literal?? HOW can I change the array value to upper case then? What other
    method exists for arrays?

    Ex:
    var GridArrayName1 = new Array();
    GridArrayName1[0] = new Array ('test-value');
    GridArrayName1[0] = GridArrayName1[0] .toUpperCase();

    The above won't work. I even tried assigning it to an object then using the
    object's "toUpperCase()" , but after the assignment the object now longer has
    that property either??

    'Sup?

    thx,

    --
    =============== =============== =============== ====
    Mike S. Nowostawsky:
    Email: mikenowo@sympat ico.ca, mikenowo@yahoo. ca
    Home Page: http://www3.sympatico.ca/mikenowo/
    Lachine (Montreal), Quebec, Canada


  • to heave chunks

    #2
    Re: Changing an array value to upper case?

    >It seems that an array is not considered an object when it is assigned a text[color=blue]
    >literal?? HOW can I change the array value to upper case then? What other
    >method exists for arrays?
    >
    >Ex:
    > var GridArrayName1 = new Array();
    > GridArrayName1[0] = new Array ('test-value');
    > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
    >
    >The above won't work. I even tried assigning it to an object then using the
    >object's "toUpperCase()" , but after the assignment the object now longer has
    >that property either??[/color]

    The Array object does not contain the .toUpperCase() method.

    In line two, you created a two-dimensional array when you
    assigned new Array( 'test-value') to GridArrayName1[0]. You
    attempted to use the .toUpperCase() method on this Array object.
    You need to take this second dimension array into account..

    var GridArrayName1 = new Array();
    GridArrayName1[0] = new Array( "test-value");
    GridArrayName1[0][0] = GridArrayName1[0][0].toUpperCase();

    Peace, Vm
    Yaz

    Providing complicated solutions to simple problems since 1997.

    Comment

    • Lee

      #3
      Re: Changing an array value to upper case?

      Mike S. Nowostawsky said:[color=blue]
      >
      >I tried using the "toUpperCas e()" property to change the value of an array
      >entity to uppercase BUT it tells me that the property is invalid. It seems
      >that an array is not considered an object when it is assigned a text
      >literal?? HOW can I change the array value to upper case then? What other
      >method exists for arrays?
      >
      >Ex:
      > var GridArrayName1 = new Array();
      > GridArrayName1[0] = new Array ('test-value');
      > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
      >
      >The above won't work. I even tried assigning it to an object then using the
      >object's "toUpperCase()" , but after the assignment the object now longer has
      >that property either??[/color]

      You seem to be confused about the difference between an Array and
      an element of an Array. You can't assign a text literal to an Array.
      You assign values to array elements. Array elements can be any type.

      Your second assignment creates a new Array containing one string
      element and makes that array the first element of GridArrayName1.
      You would access that string element as GridArrayName1[0][0].

      If you want to assign the string literal value to the first
      element of GridArrayName1, you should use:
      GridArrayName1[0] = "test-value";

      Comment

      • Dr John Stockton

        #4
        Re: Changing an array value to upper case?

        JRS: In article <XLGHb.795$Vl6. 275961@news20.b ellglobal.com>, seen in
        news:comp.lang. javascript, Mike S. Nowostawsky <mikenowo@sympa tico.ca>
        posted at Sun, 28 Dec 2003 15:08:45 :-[color=blue]
        >I tried using the "toUpperCas e()" property to change the value of an array
        >entity to uppercase BUT it tells me that the property is invalid. It seems
        >that an array is not considered an object when it is assigned a text
        >literal?? HOW can I change the array value to upper case then? What other
        >method exists for arrays?
        >
        >Ex:
        > var GridArrayName1 = new Array();
        > GridArrayName1[0] = new Array ('test-value');
        > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
        >
        >The above won't work. I even tried assigning it to an object then using the
        >object's "toUpperCase()" , but after the assignment the object now longer has
        >that property either??[/color]


        By using the second new Array, you put an Array in the first Array. It
        is then GridArrayName1[0][0] that holds 'test-value', as shown by

        var GridArrayName1 = new Array();
        GridArrayName1[0] = new Array ('test-value');
        GridArrayName1[0] = GridArrayName1[0][0] .toUpperCase();

        But what you should be writing is

        var GridArrayName1 = new Array();
        GridArrayName1[0] = 'test-value'
        GridArrayName1[0] = GridArrayName1[0].toUpperCase();

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Mike S. Nowostawsky

          #5
          Re: Changing an array value to upper case?

          Thanks very much all 3 of you for that explanation. I now understand what
          the problem was. I'm used to creating multiple dimensional arrays in a
          different fashion. In this case I only want a single dimension, so your
          suggestion of "GridArrayN ame1[0] = 'test-value';" does the trick. See what
          happens when you try to modify other peoples' code without actually knowing
          their initial intentions! <chuckle>

          Thx again!

          --
          =============== =============== =============== ====
          Mike S. Nowostawsky:
          Email: mikenowo@sympat ico.ca, mikenowo@yahoo. ca
          Home Page: http://www3.sympatico.ca/mikenowo/
          Lachine (Montreal), Quebec, Canada
          "Mike S. Nowostawsky" <mikenowo@sympa tico.ca> wrote in message
          news:XLGHb.795$ Vl6.275961@news 20.bellglobal.c om...[color=blue]
          > I tried using the "toUpperCas e()" property to change the value of an array
          > entity to uppercase BUT it tells me that the property is invalid. It seems
          > that an array is not considered an object when it is assigned a text
          > literal?? HOW can I change the array value to upper case then? What other
          > method exists for arrays?
          >
          > Ex:
          > var GridArrayName1 = new Array();
          > GridArrayName1[0] = new Array ('test-value');
          > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
          >
          > The above won't work. I even tried assigning it to an object then using[/color]
          the[color=blue]
          > object's "toUpperCase()" , but after the assignment the object now longer[/color]
          has[color=blue]
          > that property either??
          >
          > 'Sup?
          >
          > thx,
          >
          > --
          > =============== =============== =============== ====
          > Mike S. Nowostawsky:
          > Email: mikenowo@sympat ico.ca, mikenowo@yahoo. ca
          > Home Page: http://www3.sympatico.ca/mikenowo/
          > Lachine (Montreal), Quebec, Canada
          >
          >[/color]


          Comment

          • Mike S. Nowostawsky

            #6
            Re: Changing an array value to upper case?

            Now, my last step was to read data values from a text file instead of
            imbedding them in the html itself (i.e. read each line and assign it to the
            next value in the array). Can this be done in javascript? I've done it with
            VB and countless other languages, but don't know if javascript is that
            versatile.

            Thx,
            --
            =============== =============== =============== ====
            Mike S. Nowostawsky:
            Email: mikenowo@sympat ico.ca, mikenowo@yahoo. ca
            Home Page: http://www3.sympatico.ca/mikenowo/
            Lachine (Montreal), Quebec, Canada
            "Mike S. Nowostawsky" <mikenowo@sympa tico.ca> wrote in message
            news:XLGHb.795$ Vl6.275961@news 20.bellglobal.c om...[color=blue]
            > I tried using the "toUpperCas e()" property to change the value of an array
            > entity to uppercase BUT it tells me that the property is invalid. It seems
            > that an array is not considered an object when it is assigned a text
            > literal?? HOW can I change the array value to upper case then? What other
            > method exists for arrays?
            >
            > Ex:
            > var GridArrayName1 = new Array();
            > GridArrayName1[0] = new Array ('test-value');
            > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
            >
            > The above won't work. I even tried assigning it to an object then using[/color]
            the[color=blue]
            > object's "toUpperCase()" , but after the assignment the object now longer[/color]
            has[color=blue]
            > that property either??
            >
            > 'Sup?
            >
            > thx,
            >
            > --
            > =============== =============== =============== ====
            > Mike S. Nowostawsky:
            > Email: mikenowo@sympat ico.ca, mikenowo@yahoo. ca
            > Home Page: http://www3.sympatico.ca/mikenowo/
            > Lachine (Montreal), Quebec, Canada
            >
            >[/color]


            Comment

            • Kien

              #7
              Re: Changing an array value to upper case?

              Yes, you can use server-side javascript (like ASP) to read the
              textfile line by line and assign the cintent to your array.
              But this is server-side only.
              Do you have access?

              Kien


              "Mike S. Nowostawsky" <mikenowo@sympa tico.ca> wrote in message news:<pC_Hb.319 1$Vl6.846689@ne ws20.bellglobal .com>...[color=blue]
              > Now, my last step was to read data values from a text file instead of
              > imbedding them in the html itself (i.e. read each line and assign it to the
              > next value in the array). Can this be done in javascript? I've done it with
              > VB and countless other languages, but don't know if javascript is that
              > versatile.
              >
              > Thx,
              > --[/color]

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Changing an array value to upper case?

                Mike S. Nowostawsky wrote:
                [color=blue]
                > I tried using the "toUpperCas e()" property to change the value of an array
                > entity to uppercase BUT it tells me that the property is invalid. It seems
                > that an array is not considered an object when it is assigned a text
                > literal??[/color]

                An array cannot be assigned a "text literal" (better: string literal)[1]
                for it has no real value.[2] What you have done by using the
                Array(...) constructor is assigning a value to an *element* of the array
                (the first one, to be precise). A special context is required to
                convert the array's "value" (see above) to a String object. Such
                contexts are for example the alert(...) method call or the (String)
                concatenation operation with "+". But note that the toString() method
                is implemented different in different hosts, so do not count on that it
                returns a comma-separated list of the elements' values or so.
                [color=blue]
                > OW can I change the array value to upper case then?[/color]

                Again, an array (here: Array object) does not have a value (at least
                none that you can actually *write* to). I presume you meant: How can
                I change the string value of all array elements to uppercase?

                One is to convert the Array object to a String object (e.g. using
                the Array.join(...) method), uppercase that String (e.g. using its
                toUpperCase(... ) method) and split it into array elements again
                (e.g. using its split(...) method). Another is to iterate the array
                elements and uppercase each element. While the former is presumably
                faster, the latter is more reliable as it does not split substrings
                separated by delimiters in element values to different array elements.
                [color=blue]
                > What other method exists for arrays?[/color]

                RTFM:


                [color=blue]
                > Ex:
                > var GridArrayName1 = new Array();
                > GridArrayName1[0] = new Array ('test-value');
                > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
                >
                > The above won't work.[/color]

                BAD. Borken as designed. Maybe you did not want a
                *two-dimensional* array (because that is what you did)
                but only

                var GridArrayName1 = new Array();
                GridArrayName1[0] = 'test-value';
                GridArrayName1[0] = GridArrayName1[0].toUpperCase();

                which could be shortened to either

                var GridArrayName1 = new Array('test-value');
                GridArrayName1[0] = GridArrayName1[0].toUpperCase();

                as well as to

                var GridArrayName1 = ['test-value'];
                GridArrayName1[0] = GridArrayName1[0].toUpperCase();

                or

                var GridArrayName1 = new Array();
                GridArrayName1[0] = 'test-value'.toUpperC ase();

                as well as to

                var GridArrayName1 = [];
                GridArrayName1[0] = 'test-value'.toUpperC ase();
                [color=blue]
                > I even tried assigning it to an object then using the
                > object's "toUpperCase()" , but after the assignment the
                > object now longer has that property either??[/color]

                toUpperCase() is a native method of
                String objects, not of Array objects.

                However, if you need it, you can add a method
                with that identifier to the Array prototype:

                function array_toUpperCa se(/** @optional Array */ a)
                /**
                * Takes input array <code>a</code> or the Array object
                * it is applied to as method and returns a new Array
                * object with all elements in uppercase. Elements that
                * were previously not string values are automagically
                * converted to String.
                *
                * @author
                * (C) 2004 Thomas Lahn &lt;array.js@Po intedEars.de&gt ;
                * @partof
                * http://pointedears.de.vu/scripts/array.js
                * @requires
                * types#isArray()
                * @param a
                * Array which elements should be converted.
                * Is used instead of the Array object the
                * function is applied to.
                * @returns
                * A copy of <code>a</code> or the Array object with its
                * elements' value in uppercase. If <code>a</code> has no
                * elements, an empty array is returned.
                * @see
                * http://pointedears.de.vu/scripts/JSDoc/
                */
                {
                if (!a && isArray(this))
                {
                a = this;
                }

                if (isArray(a))
                {
                for (var i = 0; i < a.length; i++)
                {
                a[i] = String(a[i]).toUpperCase() ;
                }
                return a;
                }
                else
                {
                return new Array();
                }
                }
                Array.prototype .toUpperCase = array_toUpperCa se;
                [color=blue]
                > 'Sup?[/color]

                ?


                PointedEars
                ___________
                [1] A *reference* to an Array object can of course be assigned a String
                literal, but then it will not reference that Array object anymore.

                [2] What its valueOf(...) method yields is only the result of its
                toString(...) method. When assigning another value, including
                another Array object/literal to its reference, the previous Array
                object is preserved (until it is garbage-collected) but becomes
                unavailable if there is no other reference to it.

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Changing an array value to upper case?

                  Mike S. Nowostawsky wrote:
                  [color=blue]
                  > I tried using the "toUpperCas e()" property to change the value of an array
                  > entity to uppercase BUT it tells me that the property is invalid. It seems
                  > that an array is not considered an object when it is assigned a text
                  > literal??[/color]

                  An array cannot be assigned a "text literal" (better: string literal)[1]
                  for it has no real value.[2] What you have done by using the
                  Array(...) constructor is assigning a value to an *element* of the array
                  (the first one, to be precise). A special context is required to
                  convert the array's "value" (see above) to a String object. Such
                  contexts are for example the alert(...) method call or the (String)
                  concatenation operation with "+". But note that the toString() method
                  is implemented different in different hosts, so do not count on that it
                  returns a comma-separated list of the elements' values or so.
                  [color=blue]
                  > OW can I change the array value to upper case then?[/color]

                  Again, an array (here: Array object) does not have a value (at least
                  none that you can actually *write* to). I presume you meant: How can
                  I change the string value of all array elements to uppercase?

                  One is to convert the Array object to a String object (e.g. using
                  the Array.join(...) method), uppercase that String (e.g. using its
                  toUpperCase(... ) method) and split it into array elements again
                  (e.g. using its split(...) method). Another is to iterate the array
                  elements and uppercase each element. While the former is presumably
                  faster, the latter is more reliable as it does not split substrings
                  separated by delimiters in element values to separate array elements.
                  [color=blue]
                  > What other method exists for arrays?[/color]

                  RTFM:


                  [color=blue]
                  > Ex:
                  > var GridArrayName1 = new Array();
                  > GridArrayName1[0] = new Array ('test-value');
                  > GridArrayName1[0] = GridArrayName1[0] .toUpperCase();
                  >
                  > The above won't work.[/color]

                  BAD. Borken as designed. Maybe you did not want a
                  *two-dimensional* array (because that is what you did)
                  but only

                  var GridArrayName1 = new Array();
                  GridArrayName1[0] = 'test-value';
                  GridArrayName1[0] = GridArrayName1[0].toUpperCase();

                  which could be shortened to either

                  var GridArrayName1 = new Array('test-value');
                  GridArrayName1[0] = GridArrayName1[0].toUpperCase();

                  as well as to

                  var GridArrayName1 = ['test-value'];
                  GridArrayName1[0] = GridArrayName1[0].toUpperCase();

                  or

                  var GridArrayName1 = new Array();
                  GridArrayName1[0] = 'test-value'.toUpperC ase();

                  as well as to

                  var GridArrayName1 = [];
                  GridArrayName1[0] = 'test-value'.toUpperC ase();
                  [color=blue]
                  > I even tried assigning it to an object then using the
                  > object's "toUpperCase()" , but after the assignment the
                  > object now longer has that property either??[/color]

                  toUpperCase() is a native method of
                  String objects, not of Array objects.

                  However, if you require it, you could add a method with that identifier
                  to the Array prototype:

                  function array_toUpperCa se(/** @optional Array */ a)
                  /**
                  * Takes input array <code>a</code> or the Array object
                  * it is applied to as method and returns a new Array
                  * object with all elements in uppercase. Elements that
                  * were previously no string values are automagically
                  * converted to String.
                  *
                  * @author
                  * (C) 2004 Thomas Lahn &lt;array.js@Po intedEars.de&gt ;
                  * @partof
                  * http://pointedears.de.vu/scripts/array.js
                  * @requires
                  * types#isArray()
                  * @param a
                  * Array which elements should be converted.
                  * Is used instead of the Array object the
                  * function is applied to.
                  * @returns
                  * A copy of <code>a</code> or the Array object with its
                  * elements' value in uppercase. If <code>a</code> has no
                  * elements, an empty array is returned.
                  */
                  {
                  if (!a && isArray(this))
                  {
                  a = this;
                  }

                  if (isArray(a))
                  {
                  for (var i = 0; i < a.length; i++)
                  {
                  a[i] = String(a[i]).toUpperCase() ;
                  }
                  return a;
                  }
                  else
                  {
                  return new Array();
                  }
                  }
                  Array.prototype .toUpperCase = array_toUpperCa se;
                  [color=blue]
                  > 'Sup?[/color]

                  ?


                  PointedEars
                  ___________
                  [1] A *reference* to an Array object can of course be assigned a String
                  literal, but then it will not reference that Array object anymore.

                  [2] What its valueOf(...) method yields is only the result of its
                  toString(...) method. When assigning another value, including
                  another Array object/literal to its reference, the previous Array
                  object is preserved (until it is garbage-collected) but becomes
                  unavailable.

                  Comment

                  Working...