?
Solved

How do arrays work?

Posted on 2003-03-20
8
Medium Priority
?
2,911 Views
Last Modified: 2012-06-21
Hello,

I'm having big problems with arrays in asp. What I would like to know is how to assign an array to another array? In my example both arrays are of the same dimension and I still get an error! Could anyone tell me what I am doing wrong here?

<%
 Dim A(1,1)
 Dim B(1,1)
 
 B(0,0) = "A"
 B(0,1) = "B"
 B(1,0) = "C"
 B(1,1) = "D"
     Response.Write("B(0,0) = " & B(0,0) & "<br>")
     Response.Write("B(0,1) = " & B(0,1) & "<br>")
     Response.Write("B(1,0) = " & B(1,0) & "<br>")
     Response.Write("B(1,1) = " & B(1,1) & "<br>")
 A(0,0) = "D"
 A(0,1) = "C"
 A(1,0) = "B"
 A(1,1) = "A"
     Response.Write("A(0,0) = " & A(0,0) & "<br>")
     Response.Write("A(0,1) = " & A(0,1) & "<br>")
     Response.Write("A(1,0) = " & A(1,0) & "<br>")
     Response.Write("A(1,1) = " & A(1,1) & "<br>")
 
A=B
%>

What I get in IE 6 is:

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/test/test.asp, line 55 ---> this is line where I assign B to A (A=B)

thanks and best regards,
U.
0
Comment
Question by:uipavec
[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
8 Comments
 
LVL 11

Expert Comment

by:ASPGuru
ID: 8174693
you can't assign anything to an variable which was dimed as an array...

you need to copy every value "by hand" ...

or use this sub (arrays need to have the same size, or the size of the first Array needs to be smaller in every dimension)

Array2DCopy A, B

sub Array2DCopy(ByRef A1, ByRef A2)
  dim i,j
  for i = 0 to ubound(A1,1)
    for j = 0 to ubound(A2,2)
      A1(i,j) = A2(i,j)
    next
  next
end sub


ASPGuru
0
 
LVL 7

Expert Comment

by:chisholmd
ID: 8174789
You can assign to another variable:

Dim A(1,1)
B(0,0) = "A"
B(0,1) = "B"
B(1,0) = "C"
B(1,1) = "D"
A=B

should work:

Also you might be able to do this
Dim A(1,1)
Dim B(1,1)
B(0,0) = "A"
B(0,1) = "B"
B(1,0) = "C"
B(1,1) = "D"
A(0,0) = "D"
A(0,1) = "C"
A(1,0) = "B"
A(1,1) = "A"

A=""
A=B

dave

0
 
LVL 11

Expert Comment

by:ASPGuru
ID: 8175007
might doesn't mean it does... ;-)

ASPGuru
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:chisholmd
ID: 8175139
Ya :)  bit sloppy eh?

Pretty sure that I have done that in one project but can't remember which.  However I think I do this:

dim someArr(1)
someArr(0) = "blah"
someArr(0) = "blahblah"

newArray = someArray
newArray = ""
newArray = someOtherArray

Which is not exactly the same because I never dim the newArray.
0
 
LVL 11

Expert Comment

by:ASPGuru
ID: 8175192
that's right... this works...
because you never dim...

but this doesn't copy anything...
this just sets a pointer to the old array... if you change something in newArray it will also change in someArr...


regards,

ASPGuru
0
 
LVL 5

Accepted Solution

by:
dgorin earned 200 total points
ID: 8175421
This will work to copy array A to array B.

<%@ Language=VBScript %>
<%
Dim A(1,1)
Dim B

A(0,0) = "D"
A(0,1) = "C"
A(1,0) = "B"
A(1,1) = "A"
    Response.Write("A(0,0) = " & A(0,0) & "<br>")
    Response.Write("A(0,1) = " & A(0,1) & "<br>")
    Response.Write("A(1,0) = " & A(1,0) & "<br>")
    Response.Write("A(1,1) = " & A(1,1) & "<br>")

B=A

    Response.Write("B(0,0) = " & B(0,0) & "<br>")
    Response.Write("B(0,1) = " & B(0,1) & "<br>")
    Response.Write("B(1,0) = " & B(1,0) & "<br>")
    Response.Write("B(1,1) = " & B(1,1) & "<br>")
%>
0
 
LVL 11

Expert Comment

by:ASPGuru
ID: 8175476
yes, it does indeed...
i mixed up different languages here...

regards,

ASPGuru
0
 

Author Comment

by:uipavec
ID: 8175837
I would like to thank you all for your effort. However dgorin had provided the most clear and elegant solution to my problem.

I can't believe that I didn't try that myself:-)...among all my tentatives...

FYI: the code I posted was just an example. What I want to do in practice is call a function wich splits a big string that includes data separated with comma delimiter, put the results into a table, sort them and return them as a two dimensional array.

Thanks again,
U.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 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