Solved

Manipulate arrays in C#

Posted on 2006-11-05
27
250 Views
Last Modified: 2010-04-16
Can some please help me? I need the following code

[
     [null,'dasdas','_self','sada',
          [null,'dasdas','_self','sada',
               [null,'dasdas','_self','sada'],
               [null,'dasdas','_self','sada',
                    [null,'dasdas','_self','sada'],
                    [null,'dasdas','_self','sada'],
                    [null,'dasdas','_self','sada']
               ],
               [null,'dasdas','_self','sada']
          ]
     ],
     [null,'dasdas','_self','sada',
          [null,'dasdas','_self','sada'],
          [null,'dasdas','_self','sada'],
          [null,'dasdas','_self','sada',
               [null,'dasdas','_self','sada'],
               [null,'dasdas','_self','sada'],
               [null,'dasdas','_self','sada']
          ]
     ],
     [null,'dasdas','_self','sada']
];

===============================================================================
Changed to the following code
===============================================================================

[
     [null,'1', '1','dasdas','_self','sada',
          [null,'2','1','dasdas','_self','sada',
          [null,'3','1','dasdas','_self','sada'],
               [null,'4','3','dasdas','_self','sada',
                    [null,'5','4','dasdas','_self','sada'],
                    [null,'6','4','dasdas','_self','sada'],
                    [null,'7','4','dasdas','_self','sada']
          ],
          [null,'8','1','dasdas','_self','sada']
          ]
     ],
     [null,'9','9','dasdas','_self','sada',
          [null,'10','9','dasdas','_self','sada'],
          [null,'11','9','dasdas','_self','sada'],
               [null,'12','11','dasdas','_self','sada',
                    [null,'13','12','dasdas','_self','sada'],
                    [null,'14','12','dasdas','_self','sada'],
                    [null,'15','12','dasdas','_self','sada']
          ]
     ],
     ["Page",'16','16','dasdas','_self','sada']
];

===============================================================================
The 2nd and 3rd elements off each array has a number similar to an ID, ParentID
===============================================================================

It should produce a ID/ParentID relationship.

As you can see, the 2nd element is incremented by 1, the 3rd is the ParentID. The code is something I just mocked up, an I prefer to have the ID's, ParentID to be the last items, rather then 2nd and 3rd.

I really do hope I've made sense here, and I would very much like to have your help in the problem.
Many thanks
Mousemat24
0
Comment
Question by:mousemat24
  • 16
  • 11
27 Comments
 
LVL 11

Expert Comment

by:Agarici
ID: 17879468
to insert in an array at index iPos with 0 <= iPos < Length, you should do:

Length = <something>;
iPos = <somethingElse>;
int[Length] arr = new int[Length];
// insert at iPos
int[] tmpArr = new  int[Length+1]
Array.Copy(arr, 0, tmpArr, 0, iPos-1 );
tmpArr[iPos] = <something>;
Array.Copy( arr, iPos, tmpArr, iPos+1, Length - iPos );
arr = tmpArr;

of course things would be a lot easier if instead of arrays you would use ArrayList or the generics from .net2.0 List(Type)
with lists you would only need to do a list.Insert()

hth,
A.



0
 

Author Comment

by:mousemat24
ID: 17879549
Hi Agarici

I'm new to C#, so please bear with me. The code you've written, does it sort out my problem? i.e. will it produce the following code. I would really be grateful if you can help me with this.

[
     [null,'1', '1','dasdas','_self','sada',
          [null,'2','1','dasdas','_self','sada',
          [null,'3','1','dasdas','_self','sada'],
               [null,'4','3','dasdas','_self','sada',
                    [null,'5','4','dasdas','_self','sada'],
                    [null,'6','4','dasdas','_self','sada'],
                    [null,'7','4','dasdas','_self','sada']
          ],
          [null,'8','1','dasdas','_self','sada']
          ]
     ],
     [null,'9','9','dasdas','_self','sada',
          [null,'10','9','dasdas','_self','sada'],
          [null,'11','9','dasdas','_self','sada'],
               [null,'12','11','dasdas','_self','sada',
                    [null,'13','12','dasdas','_self','sada'],
                    [null,'14','12','dasdas','_self','sada'],
                    [null,'15','12','dasdas','_self','sada']
          ]
     ],
     ["Page",'16','16','dasdas','_self','sada']
];

0
 

Author Comment

by:mousemat24
ID: 17879553
Agarici, I've also posted this as a Javascript question. Do you think you can help me out, I'm really panicing here, and I need this working ASAP.
Thanks man
0
Technology Partners: 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 11

Expert Comment

by:Agarici
ID: 17879592
do you use .net 2.0 or 1.1?
the data that you have posted, is it hold in an array or an arrayList?
do you have some sample code? How is the data structure declared?

the code i've posted will produce that output but it need to be adapted to your specific problem.
0
 

Author Comment

by:mousemat24
ID: 17879629
Agarici

How good is your JS knowledge? Because the acually array is written is this really cool menu builder program:

http://jscook.yuanheng.org/JSCookMenu/MenuBuilder.html

I'm using that to produce the array.

I'm using VS2005/C#/ASP.NET 2

In a nut shell, I'm taking the menu builder and converting it to .NET. I've got it to work, but I want to add the ID/ParentID. The reason why is that I can use VS treeview navigation. I've looked at this site

http://aspalliance.com/732

thats why I want a ID/ParentID.

I hope this makes sense?
Thanks Agarici, I really do hope you can help me out
0
 

Author Comment

by:mousemat24
ID: 17879639
Then once you've managed to get it to work, I can then use the following code to put it into a SQL db:

yourTextBox.Text    =   to the array



string allEntries = yourTextBox.Text;
int index = allEntries.IndexOf("[");  // detect start of data
if (index>=0)
{
    allEntries = allEntries.Remove(0,index+1);
    index = allEntries.IndexOf("]]"); // detect end of data
    if (index>=0)
    {
         allEntries.Remove(index+1);
         string[] records = allEntries.Split(",".ToCharArray());

         foreach(string record in records)
         {
              string values = record.Replace('[','(').Replace(']',')');
              string query = "INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description) VALUES"+values;
              // now you have the query string, use it!
         }
    } else
         // error, no end found, do something
}
else
    // There is not data in your textbox, do something
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17879963
well... i would suggest to you this: add the id/parentId values when you insert into the db.
meaning : you extract the data from your array like you already do. for each menu item (db row) generate an unique id. and use that id an the parent id into the insert query.

like the code bellow ;)

            Console.WriteLine(page);
            int itemId = 1;
            int parentId = 0;
            int crtPos = 0;

            string allEntries = page.Replace("\n", string.Empty);
            allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty);

            string[] values = allEntries.Split('[');
            string crtValue = string.Empty;
            string query = string.Empty;

            ArrayList parents = new ArrayList();

            for ( int i = 0 ; i < values.Length ; i++ )
            {
                crtValue = values[i];

                if ( crtValue.Length == 0 )
                    continue;

                itemId ++;
                parentId = itemId;

                crtPos = crtValue.IndexOf(']');
                if ( crtPos == -1 )
                {
                    if ( parents.Count > 0 )
                        parentId = (int)parents[parents.Count - 1];
                    parents.Add(itemId);
                }
                else
                {
                    crtValue = crtValue.Remove(crtPos, 1);
                    crtPos = crtValue.IndexOf(']');
                    if ( parents.Count > 0 )
                    {
                        parentId = (int)parents[parents.Count - 1];
                    }
                    while ( crtPos != -1 )
                    {
                        if ( parents.Count > 0 )
                        {
                            parents.RemoveAt(parents.Count - 1);
                        }
                        crtValue = crtValue.Remove(crtPos, 1);
                        crtPos = crtValue.IndexOf(']');
                    }
                }

                query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES ({0}{1},{2})", values, id, parentId);
               
                // execute query string formated above.


            }


hth,
A.
0
 

Author Comment

by:mousemat24
ID: 17885696
Hi Agarici

Thanks very much for helping me out, really am grateful for your help!!! As I said my C#/ASP.NET knowledge isnt that great, So please bear with me if I ask stupid questions.

The code you've written is great, but I cant test it as I get an error:

The name 'id' does not exist in the current context

I've created a string (hope thats ok?)

string page = "[[null,'dasdas','_self','sada',[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada']]";

Do you think its possible to have a variable that contains the revised array with the correct ID's/ParentID's? You're probably correct with what you've mentioned above, but I'm not that good with .NET. So can you please help me in getting the new array stored as a new variable?

Thanks Agarici, just wish my knowledge of .NET was as good as yours
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17887589
yes, my mistake ;)

instead of id in

query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES ({0}{1},{2})", values, id, parentId);

should be 'itemId'
meaning:

query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES ({0}{1},{2})", values, itemId, parentId);

yes, 'page' variable should contain the array as you posted it.

about creating a new string variable with the array containing the id/parentid... it is possible, but if your purpose is to store the data in the db, its not usefull, it only complicates the things.

but as i said it is posible:
          Console.WriteLine(page);
            int itemId = 1;
            int parentId = 0;
            int crtPos = 0;

            string allEntries = page.Replace("\n", string.Empty);
            allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty);

            string[] values = allEntries.Split('[');
            string crtValue = string.Empty;
            string query = string.Empty;
           
            string initialCrtValue = string.Empty;
            string paveWithIdAndParentId = string.Empty;

            ArrayList parents = new ArrayList();

            for ( int i = 0 ; i < values.Length ; i++ )
            {
                crtValue = values[i];

                if ( crtValue.Length == 0 )
                    continue;
               
                initialCrtValue = crtValue;

                itemId ++;
                parentId = itemId;

                crtPos = crtValue.IndexOf(']');
                if ( crtPos == -1 )
                {
                    if ( parents.Count > 0 )
                        parentId = (int)parents[parents.Count - 1];
                    parents.Add(itemId);
                }
                else
                {
                    crtValue = crtValue.Remove(crtPos, 1);
                    crtPos = crtValue.IndexOf(']');
                    if ( parents.Count > 0 )
                    {
                        parentId = (int)parents[parents.Count - 1];
                    }
                    while ( crtPos != -1 )
                    {
                        if ( parents.Count > 0 )
                        {
                            parents.RemoveAt(parents.Count - 1);
                        }
                        crtValue = crtValue.Remove(crtPos, 1);
                        crtPos = crtValue.IndexOf(']');
                    }
                }

                query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES ({0}{1},{2})", values, id, parentId);
               
                // execute query string formated above.
                // ...

               int firstComaPos = initialCrtValue.IndexOf( ',' );
               if( firstComaPos == -1 ) continue;

               initialCrtValue = initialCrtValue.Insert( firstComaPos+1, string.Fromat("{0},{1},", itemId, parentId);
               pageWithIdAndParentId += ( "[" + initialCrtValue );

            }

hth,
A.
0
 

Author Comment

by:mousemat24
ID: 17893660
Thanks Agarici for coming back to me: I've put the following code at the end

initialCrtValue = initialCrtValue.Insert(firstComaPos + 1, string.Format("{0},{1},", itemId, parentId));

string pageWithIdAndParentId = string.Empty;
pageWithIdAndParentId += pageWithIdAndParentId + ("[" + initialCrtValue);
lit1.Text = initialCrtValue;

the results what appear in lit1 is:

null,17,17,'dasdas','_self','sada']]

am I doing anything wrong?

Thanks for helping me out again
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17896153
well... the '[' are removed when doing allEntries.Split('[');
so you will have to put them back by changing
initialCrtValue = crtValue;
to initialCrtValue = "[" + crtValue;

also take care of the case

if ( crtValue.Length == 0 )
        continue;

you should do there also a += "[" :

if ( crtValue.Length == 0 )
{
pageWithIdAndParentId += "[";
 continue;
 }

see if it works better after those changes

hth,
A.
0
 

Author Comment

by:mousemat24
ID: 17901381
Hi Agarici

Thanks for the response, really am grateful for your time. I've changed the code as per your comments, the following is the results from your code above:

[[[[null,2,2,'dasdas','_self','sada',[[null,3,2,'dasdas','_self','sada',[[null,4,3,'dasdas','_self','sada'],[[null,5,3,'dasdas','_self','sada',[[null,6,5,'dasdas','_self','sada'],[[null,7,5,'dasdas','_self','sada'],[[null,8,5,'dasdas','_self','sada']],[[null,9,3,'dasdas','_self','sada']]],[[null,10,10,'dasdas','_self','sada',[[null,11,10,'dasdas','_self','sada'],[[null,12,10,'dasdas','_self','sada'],[[null,13,10,'dasdas','_self','sada',[[null,14,13,'dasdas','_self','sada'],[[null,15,13,'dasdas','_self','sada'],[[null,16,13,'dasdas','_self','sada']]],[[null,17,17,'dasdas','_self','sada']];


But this is what I'm expecting, I think we have to many "[", "]", and also the numbering isnt right ID/parentID:

[[null,'1', '1','dasdas','_self','sada', [null,'2','1','dasdas','_self','sada', [null,'3','1','dasdas','_self','sada'], [null,'4','3','dasdas','_self','sada', [null,'5','4','dasdas','_self','sada'], [null,'6','4','dasdas','_self','sada'], [null,'7','4','dasdas','_self','sada'] ], [null,'8','1','dasdas','_self','sada'] ] ], [null,'9','9','dasdas','_self','sada', [null,'10','9','dasdas','_self','sada'], [null,'11','9','dasdas','_self','sada'], [null,'12','11','dasdas','_self','sada', [null,'13','12','dasdas','_self','sada'], [null,'14','12','dasdas','_self','sada'], [null,'15','12','dasdas','_self','sada'] ] ], ["Page",'16','16','dasdas','_self','sada']];

====================================================================
Do you think you can help me in getting this to work? the code I'm using is as follows Agarici
====================================================================

string page = "[[null,'dasdas','_self','sada',[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada']]";
            int itemId = 1;
            int parentId = 0;
            int crtPos = 0;
            string allEntries = page.Replace("\n", string.Empty);
            allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty);

            string[] values = allEntries.Split('[');
            string crtValue = string.Empty;
            string query = string.Empty;            
            string initialCrtValue = string.Empty;
            string paveWithIdAndParentId = string.Empty;
            string pageWithIdAndParentId = "";
            ArrayList parents = new ArrayList();

            for ( int i = 0 ; i < values.Length ; i++ )
            {
                crtValue = values[i];

                if (crtValue.Length == 0)
                {
                    pageWithIdAndParentId += "[";
                    continue;
                }
                initialCrtValue = "[" + crtValue;
                itemId ++;
                parentId = itemId;
                crtPos = crtValue.IndexOf(']');
                if ( crtPos == -1 )
                {
                    if ( parents.Count > 0 )
                        parentId = (int)parents[parents.Count - 1];
                    parents.Add(itemId);
                }
                else
                {
                    crtValue = crtValue.Remove(crtPos, 1);
                    crtPos = crtValue.IndexOf(']');
                    if ( parents.Count > 0 )
                    {
                        parentId = (int)parents[parents.Count - 1];
                    }
                    while ( crtPos != -1 )
                    {
                        if ( parents.Count > 0 )
                        {
                            parents.RemoveAt(parents.Count - 1);
                        }
                        crtValue = crtValue.Remove(crtPos, 1);
                        crtPos = crtValue.IndexOf(']');
                    }
                }
               int firstComaPos = initialCrtValue.IndexOf( ',' );
               if( firstComaPos == -1 ) continue;
               initialCrtValue = initialCrtValue.Insert( firstComaPos+1, string.Format("{0},{1},", itemId, parentId));                
               pageWithIdAndParentId += ( "[" + initialCrtValue );
               lit1.Text = pageWithIdAndParentId;
            }


As I said, I'm am new to .NET and I really need some professional help in getting this sorted out. Thanks Agarici
0
 

Author Comment

by:mousemat24
ID: 17906068
sorry to bother you Agarici, just wondering if you've come up with a solution?

Thanks man
0
 

Author Comment

by:mousemat24
ID: 17925044
Hi Agarici, did you have a look at problem?

Thank you
Mousemat24
0
 
LVL 11

Accepted Solution

by:
Agarici earned 500 total points
ID: 17928420
           Console.WriteLine(page);
            int itemId = 1;
            int parentId = 0;
            int crtPos = 0;

            string allEntries = page.Replace("\n", string.Empty);
            allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty);

            string[] values = allEntries.Split('[');
            string crtValue = string.Empty;
            string query = string.Empty;
            string initialCrtValue = string.Empty;
            string paveWithIdAndParentId = string.Empty;
            string pageWithIdAndParentId = "[";

            ArrayList parents = new ArrayList();

            for ( int i = 0 ; i < values.Length ; i++ )
            {
                crtValue = values[i];

                if ( crtValue.Length == 0 )
                {
                    continue;
                }
                initialCrtValue = "[" + crtValue;

                itemId ++;
                parentId = itemId;

                crtPos = crtValue.IndexOf(']');
                if ( crtPos == -1 )
                {
                    if ( parents.Count > 0 )
                        parentId = (int)parents[parents.Count - 1];
                    parents.Add(itemId);
                }
                else
                {
                    crtValue = crtValue.Remove(crtPos, 1);
                    crtPos = crtValue.IndexOf(']');
                    if ( parents.Count > 0 )
                    {
                        parentId = (int)parents[parents.Count - 1];
                    }
                    while ( crtPos != -1 )
                    {
                        if ( parents.Count > 0 )
                        {
                            parents.RemoveAt(parents.Count - 1);
                        }
                        crtValue = crtValue.Remove(crtPos, 1);
                        crtPos = crtValue.IndexOf(']');
                    }
                }

                //query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES (%s%d,%d)", values, id, parentId);
                query = string.Format("({0}{1},{2})", crtValue, itemId, parentId);
                Console.WriteLine(query);
                // execute query string formated above.
                int firstComaPos = initialCrtValue.IndexOf(',');
                if ( firstComaPos == -1 ) continue;
                initialCrtValue = initialCrtValue.Insert(firstComaPos + 1, string.Format("{0},{1},", itemId, parentId));
                pageWithIdAndParentId += (initialCrtValue);
0
 

Author Comment

by:mousemat24
ID: 17928975
Agarici, thanks for replying to me!! :-)

Just copied your code, I thnk it may be wrong, sorry, but here is the output:

This example has [null,3,2  [null,4,2   [null,8,2   [null,'12','9'

[[null,1,1,'dasdas','_self','sada',[null,2,1,'dasdas','_self','sada',[null,3,2,'dasdas','_self','sada'],[null,4,2,'dasdas','_self','sada',[null,5,4,'dasdas','_self','sada'],[null,6,4,'dasdas','_self','sada'],[null,7,4,'dasdas','_self','sada']],[null,8,2,'dasdas','_self','sada']]],[null,9,9,'dasdas','_self','sada',[null,10,9,'dasdas','_self','sada'],[null,11,9,'dasdas','_self','sada'],[null,12,9,'dasdas','_self','sada',[null,13,12,'dasdas','_self','sada'],[null,14,12,'dasdas','_self','sada'],[null,15,12,'dasdas','_self','sada']]],[null,16,16,'dasdas','_self','sada']]

The output I'm expecting is:

This example has [null,3,1  [null,4,3   [null,8,1   [null,'12','11'

[ [null,'1', '1','dasdas','_self','sada', [null,'2','1','dasdas','_self','sada', [null,'3','1','dasdas','_self','sada'], [null,'4','3','dasdas','_self','sada', [null,'5','4','dasdas','_self','sada'], [null,'6','4','dasdas','_self','sada'], [null,'7','4','dasdas','_self','sada'] ], [null,'8','1','dasdas','_self','sada'] ] ], [null,'9','9','dasdas','_self','sada', [null,'10','9','dasdas','_self','sada'], [null,'11','9','dasdas','_self','sada'], [null,'12','11','dasdas','_self','sada', [null,'13','12','dasdas','_self','sada'], [null,'14','12','dasdas','_self','sada'], [null,'15','12','dasdas','_self','sada'] ] ], ["Page",'16','16','dasdas','_self','sada'] ];

Hope this makes sense Agarici?
Thanks for replying to my cry.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17928987
try mooving the line with 'itemId ++;' at the end of the for loop (before the closing bracket)
see if it works better
0
 

Author Comment

by:mousemat24
ID: 17929034
Hi Agarici

Tried that, but the numbers are still not matching up.
Thanks
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17929056
using the code i have posted, i get this results.
please tell me exactly what is wrong

[
     [null,'M1','_self','sada',
          [null,'M1_1   ','_self','sada',
               [null,'M1_1_1  ','_self','sada'],
               [null,'M1_1_2  ','_self','sada',
                    [null,'M1_1_2_1','_self','sada'],
                    [null,'M1_1_2_2','_self','sada'],
                    [null,'M1_1_2_3','_self','sada']
               ],
               [null,'M1_1_3','_self','sada']
          ]
     ],
     [null,'M3','_self','sada',
          [null,'M3_1','_self','sada'],
          [null,'M3_2','_self','sada'],
          [null,'M3_3','_self','sada',
               [null,'M3_3_1','_self','sada'],
               [null,'M3_3_2','_self','sada'],
               [null,'M3_3_3','_self','sada']
          ]
     ],
     [null,'M4','_self','sada']
];

(null,'M1','_self','sada',1,1)
(null,'M1_1','_self','sada',2,1)
(null,'M1_1_1','_self','sada',3,2)
(null,'M1_1_2','_self','sada',4,2)
(null,'M1_1_2_1','_self','sada',5,4)
(null,'M1_1_2_2','_self','sada',6,4)
(null,'M1_1_2_3','_self','sada',7,4)
(null,'M1_1_3','_self','sada',8,2)
(null,'M3','_self','sada',9,9)
(null,'M3_1','_self','sada',10,9)
(null,'M3_2','_self','sada',11,9)
(null,'M3_3','_self','sada',12,9)
(null,'M3_3_1','_self','sada',13,12)
(null,'M3_3_2','_self','sada',14,12)
(null,'M3_3_3','_self','sada',15,12)
(null,'M4','_self','sada';16,16)

[[null,1,1,'M1','_self','sada',[null,2,1,'M1_1','_self','sada',[null,3,2,'M1_1_1
','_self','sada'],[null,4,2,'M1_1_2','_self','sada',[null,5,4,'M1_1_2_1','_self'
,'sada'],[null,6,4,'M1_1_2_2','_self','sada'],[null,7,4,'M1_1_2_3','_self','sada
']],[null,8,2,'M1_1_3','_self','sada']]],[null,9,9,'M3','_self','sada',[null,10,
9,'M3_1','_self','sada'],[null,11,9,'M3_2','_self','sada'],[null,12,9,'M3_3','_s
elf','sada',[null,13,12,'M3_3_1','_self','sada'],[null,14,12,'M3_3_2','_self','s
ada'],[null,15,12,'M3_3_3','_self','sada']]],[null,16,16,'M4','_self','sada']];
0
 

Author Comment

by:mousemat24
ID: 17929149
Hi Agarici
I'm using this string for the page variable:

string page = "[[null,'dasdas','_self','sada',[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada']]";

I've changed int itemId = 1; to int itemId = 0; as I want it to start from 1. I'm using a WEB form, so I'm outputing the value to a Literal control. The results are as follows:

[[null,1,1,'dasdas','_self','sada',[null,2,1,'dasdas','_self','sada',[null,3,2,'dasdas','_self','sada'],[null,4,2,'dasdas','_self','sada',[null,5,4,'dasdas','_self','sada'],[null,6,4,'dasdas','_self','sada'],[null,7,4,'dasdas','_self','sada']],[null,8,2,'dasdas','_self','sada']]],[null,9,9,'dasdas','_self','sada',[null,10,9,'dasdas','_self','sada'],[null,11,9,'dasdas','_self','sada'],[null,12,9,'dasdas','_self','sada',[null,13,12,'dasdas','_self','sada'],[null,14,12,'dasdas','_self','sada'],[null,15,12,'dasdas','_self','sada']]],[null,16,16,'dasdas','_self','sada']]

--------------------------------------------------------------------------------------------------------------------------
The output I'm expecting is as follows, noticed there are some slight number changes:

[ [null,'1', '1','dasdas','_self','sada', [null,'2','1','dasdas','_self','sada', [null,'3','1','dasdas','_self','sada'], [null,'4','3','dasdas','_self','sada', [null,'5','4','dasdas','_self','sada'], [null,'6','4','dasdas','_self','sada'], [null,'7','4','dasdas','_self','sada'] ], [null,'8','1','dasdas','_self','sada'] ] ], [null,'9','9','dasdas','_self','sada', [null,'10','9','dasdas','_self','sada'], [null,'11','9','dasdas','_self','sada'], [null,'12','11','dasdas','_self','sada', [null,'13','12','dasdas','_self','sada'], [null,'14','12','dasdas','_self','sada'], [null,'15','12','dasdas','_self','sada'] ] ], ["Page",'16','16','dasdas','_self','sada'] ];


I hope I've explained myself? If not, please let me know.
Thank you
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17929224
i think you are making a mistake as to the desired output
please examine what you have originaly posted and notice the output differs as tree structure from the imput.
i think (correct me if i'm wrong ) you made a mistake when establishing what the output should look like.


[
     [null,'dasdas','_self','sada',
          [null,'dasdas','_self','sada',
               [null,'dasdas','_self','sada'],
               [null,'dasdas','_self','sada',
                    [null,'dasdas','_self','sada'],
                    [null,'dasdas','_self','sada'],
                    [null,'dasdas','_self','sada']
               ],
               [null,'dasdas','_self','sada']
          ]
     ],
     [null,'dasdas','_self','sada',
          [null,'dasdas','_self','sada'],
          [null,'dasdas','_self','sada'],
          [null,'dasdas','_self','sada',
               [null,'dasdas','_self','sada'],
               [null,'dasdas','_self','sada'],
               [null,'dasdas','_self','sada']
          ]
     ],
     [null,'dasdas','_self','sada']
];

// this should be the output
[
     [null,'1', '1','dasdas','_self','sada',
          [null,'2','1','dasdas','_self','sada',
               [null,'3','2','dasdas','_self','sada'],
               [null,'4','2','dasdas','_self','sada',
                     [null,'5','4','dasdas','_self','sada'],
                     [null,'6','4','dasdas','_self','sada'],
                     [null,'7','4','dasdas','_self','sada']
          ],
          [null,'8','1','dasdas','_self','sada']
          ]
     ],
     [null,'9','9','dasdas','_self','sada',
          [null,'10','9','dasdas','_self','sada'],
          [null,'11','9','dasdas','_self','sada'],
               [null,'12','11','dasdas','_self','sada',
                    [null,'13','12','dasdas','_self','sada'],
                    [null,'14','12','dasdas','_self','sada'],
                    [null,'15','12','dasdas','_self','sada']
          ]
     ],
     ["Page",'16','16','dasdas','_self','sada']
];
 
as opposed to what you posted:
[
     [null,'1', '1','dasdas','_self','sada',
          [null,'2','1','dasdas','_self','sada',
          [null,'3','1','dasdas','_self','sada'],
               [null,'4','3','dasdas','_self','sada',
                    [null,'5','4','dasdas','_self','sada'],
                    [null,'6','4','dasdas','_self','sada'],
                    [null,'7','4','dasdas','_self','sada']
          ],
          [null,'8','1','dasdas','_self','sada']
          ]
     ],
     [null,'9','9','dasdas','_self','sada',
          [null,'10','9','dasdas','_self','sada'],
          [null,'11','9','dasdas','_self','sada'],
               [null,'12','11','dasdas','_self','sada',
                    [null,'13','12','dasdas','_self','sada'],
                    [null,'14','12','dasdas','_self','sada'],
                    [null,'15','12','dasdas','_self','sada']
          ]
     ],
     ["Page",'16','16','dasdas','_self','sada']
];
0
 

Author Comment

by:mousemat24
ID: 17929540
Agarici

I think your cracked it, you were right, it was my output that was messed up!!! Ohh man, you cant beleive how happy I am do get someone help me with this problem!!!

Agarici, do you think you can place some comments? I really would like to know how you built this program.

One final question, do you think you can help me out with a slight change to this function? I can place another question. Basiclly, all the top level nodes i.e. '1', '1' ... '9','9' and '16','16' etc, can they change to

'1','top' ... '9','top' and '16','top' etc

I've noticed in http://aspalliance.com/732, he places a null (top) at the top level nodes. I've just placed top, just to make it different. As I said I can post another question. Really would appriciate your help in this.

Thanks Agarici for all your help!!! sorry if I pestered you, but I really needed this ASAP.
Thank you Thank you Thank you Thank you
Mousemat24
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17929864
replace
              query = string.Format("({0}{1},{2})", crtValue, itemId, parentId);
with
              query = string.Format("({0}{1},{2})", crtValue, itemId, parentId == itemId ? "top or whatever" : parentId.ToString() );
0
 

Author Comment

by:mousemat24
ID: 17930394
Agarici

Thanks for helping out, I hope you didnt find me a pest or anything!! hope you liked this problem?

Mousemat24
0
 

Author Comment

by:mousemat24
ID: 17942797
Hi Agarici

Do you think you can help me with getting another problem I have, its pretty much the same as the stuff you've done, but I want to capture all the value and place it in the query string (insert)

So if I had the following

['home.gif', 'Homepage', 'http://www.google.com', 'main','this is a test', '1', '2'

will get converted to:

query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES (%a,%b,%c,%d,%e,%f,%g)", icon, title, url, target, desc, itemid, parentid);


Dont know if that made sense? I can place this as a seperate question? Really would like you to help me out.
Thanks Agarici
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17945164
i think you should have opened an other question for that

anyway:
string someString = @"['home.gif', 'Homepage', 'http://www.google.com', 'main','this is a test', '1', '2'";
string[] values = someString.Split(',');
if( values.Length != 7 )
    // see how to handle this case

query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES ({0},{1},{2},{3},{4},{5},{6})", values[0],values[1],values[2],values[3],values[4],values[5],values[6]);

A.
0
 

Author Comment

by:mousemat24
ID: 17945400
ok, I've just created another question Agarici, title Manipulate arrays, and placing the resualts to SQL

Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

713 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