0

I am going to make a Hash Table using C# and store a number of Html tags in each sell of this hash table. Then, I want to write an algorithm to read the code from the Hash table and demonstrate them semantically and correctly in the output.

For example: if I have in my Hash table something like this:

<html>|<title>|</head>|</html>|</title>|<head>|...
-----------------------------------------------------

The final output would be like the correct one which is:

<html>
<head>
<title>

</title>
</head>
</html>
...

In this way, I have 3 main questions which are:

  1. how to store the codes in the hash table?
  2. how to read the code from the hash table semantically?
  3. Is there any better way or easier way to do it?

I will be thankful, if you let me your ideas and if there is some information missing in your point of view, please let me know and I will provide those info for you to help me in a better way.

Thanks in advance

mazName
  • 145
  • 9
  • See this [link](https://msdn.microsoft.com/en-us/library/system.collections.hashtable(v=vs.110).aspx). – Han Nov 19 '16 at 20:01
  • @HandokoChen The Hashtable class is not reccomended for new development, you should use the strongly type [Dictionary](https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx) class instead – Scott Chamberlain Nov 19 '16 at 20:36
  • @ScottChamberlain I know. But he asked about Hashtable. – Han Nov 19 '16 at 20:36
  • Maziar why do you feel that a hash table would be appropriate, from your description it looks like you just want a normal `List`, @HandokoChen I don't think he knows what he is asking for, he knows the concept of a hash table, but he is not nesssarly looking for the `Hashtable` class. A `Dictionary` is also a hashtable but it is just a strongly typed hashtable. – Scott Chamberlain Nov 19 '16 at 20:38
  • He asked how to store something in a hashtable, so I gave the link. The Dictionary works similar to Hashtable. It's even better because it's strongly type. But I'm curious with the 3 semantic tags. I'm not sure what he meant with `semantically`. – Han Nov 19 '16 at 20:43
  • @ScottChamberlain, hi dude. To be honest to you, I know what is the differences between hashtable, dictionary, list and so on. but I gave that just as an example. So, the actual output will be bigger and more complicated than what I wrote in my question. I mentioned a hashtable because I didn't want to go to far first and talk about a database or something like that. – mazName Nov 20 '16 at 11:09
  • @HandokoChen, hello. At the bottom of this question, I explained in easy way to Zoran Horvat that why I said semantically. I mentioned semantically because the output will be shown based on a question which is asked. So, the output is not just to read from the hashtable, dictionary ,... and print them in the output. – mazName Nov 20 '16 at 11:09

1 Answers1

0

If I understood your question right, it seems like you are dealing with tags out of order, and then you want to order them correctly. Doing so will not be easy at all...

Let me try to construct a system which is doing precisely that and then you will see all the complexity that is hiding behind.

Each tag would have to be represented by a key. Tags are either opening (e.g. <html>), closing (e.g. </html>), or empty (e.g. <html />). The first two forms must come in pairs, so it doesn't seem to make sense to remember both of them - missing closing tag is an error anyway and you may decide to auto-correct such errors anyway.

So you may describe a tag with a structure (Tag, RequiresClose) and then construct a hashtable or dictionary which maps string -> bool, where Boolean value indicates whether the tag requires a corresponding closing tag:

<html>|<title>|</head>|</html>|</title>|<head>

These tags would populate the dictionary like:

html -> true
title -> true
head -> true

And then you need to construct an interpreter for this dictionary, which would construct the output sequence of tags. Below is the pseudocode of the interpreter.

Interpret()
    InterpretHtml()

InterpretHtml()
    if html not present in dictionary then quit
    if dictionary['html'] = false then print <html /> and quit
    print <html>
    InterpretHead()
    InterpretBody()
    print </html>
    remove html from dictionary

InterpretHead()
    if head not present in dictionary then quit
    if dictionary['head'] = false then print <head /> and quit
    print <head>
    InterpretTitle()
    print </head>
    remove head from dictionary

InterpretTitle()
    if title not present in dictionary then quit
    if dictionary['title'] = false then print <title /> and quit
    print <title>
    // somehow find the title and print it
    print </title>
    remove title from dictionary

This approach is tedious and it doesn't produce predictable output.

Therefore, I would recommend you to thoroughly reconsider your task and do it in a completely different manner. Since HTML is producing hierarchical documents, you might want to consider using a stack and then force correct order of tags on the input.

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43
  • First of all, I should thank you for your explanation. – mazName Nov 20 '16 at 10:43
  • I have a question regarding to your last sentence which is using stack. Imagine, if we have many input and there is someone who can add and remove the tags from the hash table, dictionary of stack. Then, the stack is not the correct way of storing the inputs as the person can come and add after a while that we don't know where to put it. Am I right? Apart from that, the output is not to show all the input once and then finish. I mentioned in my question "demonstrate semantically" because I don't know that how many times will be shown in the out put – mazName Nov 20 '16 at 10:54
  • the output will be based on a set of input. For example, the questions is "a html page with 5 paragraphs" and then my code needs to fetch the data from the dictionary semantically and print them in the output based on the question. – mazName Nov 20 '16 at 10:58