dousui4577 2016-10-28 12:46
浏览 49
已采纳

对于文件中的每个单词,查找当前单词是否多次出现

I'm very new to Golang and I'm having some issues on trying to find and print all the lines in a file which contain a certain same value.

My file is structured like the following:

index text
index text
     .
     .
     .
index text

Where index is ALWAYS 6 digits long and text is ALWAYS 16 digits long.

I need to find and print all the lines which contain the same text value.

That's what I tried so far:

func main() {

    //Array to contain common texts
    found := make([]string, 6)

    r, _ := os.Open("store.txt")
    scanner := bufio.NewScanner(r)
    //Splits in words
    scanner.Split(bufio.ScanWords)
    //Loop over all Words in the file
    for scanner.Scan() {
        line := scanner.Text()
        //If the current line is 16 digits long
        if(utf8.RuneCountInString(line) == 16){
           currLine := line
            //Search in the same files all the 16 digits long texts and
            for scanner.Scan(){
                searchLine := scanner.Text()
                //If a same text is found
                if(utf8.RuneCountInString(searchLine) == 16){
                    //Append it to found array
                    if(currLine == searchLine){
                        found = append(found, currLine)
                    }
                }
            }
        }
    }
    //Print found Array
    fmt.Println(found)
    //Close File
    r.Close()
}

Then, I would like to use found to print all the lines which match the current found[i-element].

The code above works only for the very first step. For instance, If in my file, at the very first line it gets 1234567890123456 (e.g. from index 1) then it checks and appends only one time, it does not loop for all the lines (for the remaining n-1 words).

  • How can I fix the first issue?

  • Do you think adding the duplicate texts in an Array and then print the matching lines based on it is a bad idea?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-10-28 13:14
    关注

    The first issue is caused because you are using the same stream to read the file and check duplicate so when the inner for reach the bottom of the file finish, then the outer for check if there is something more to scan but it find the EOF and exit.
    The easiest way to solve your problem is creating an array where you put all the text that you find for the first time and when the text value are already present just print the duplicate. Something like this:

    duplicates := make([]string,0)
    for scanner.Scan() {
        line := scanner.Text()
        text := line[6:]
        //Do your check
        //if all your control are ok
        if ! contains(duplicates, text) {
            duplicates = append(duplicates, text)
        } else {
            //Print the duplicates
        }
    

    And here there is the contains implementation

    func contains(s []string, e string) bool {
        for _, a := range s {
            if a == e {
                return true
            }
        }
        return false
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:这个六个方程输入程序可以得出角度角速度角加速度
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油