当前位置: 首页 > 知识库问答 >
问题:

如何使用Perl 6类表示嵌套数据结构?

郭翰翮
2023-03-14

上次我不得不处理这样的数据时,我使用了类似于散列数组的东西,其中每个散列都可以有散列值等。在循环遍历不同的索引/键时,很难不丢失,所以我想应该有更好的解决方案。由于我没有OOP经验,我不知道如何开始...

假设在我们的城市里,有一个图书馆(其内容已被数字化为txt文件),有几个房间:1\u red\u room,2\u blue\u room,和3\u white\u room。在每个房间里,都有许多书,每本书都有:作者的名字、标题和文本(从txt文件读取)分为几页(带数字)。

给定一个$字,对于每个房间,程序应列出:

room_name, with the overall number of `$word` contexts in all its books
  list of authors, who use this word, with number of contexts
    for every author, list of books, with number of contexts
      for every book, list of pages, with number of contexts

输出示例:

Word: cucumber
TOTAL: 654
1_red_room: 234
  author: John Smith: 70
    title: "In the wild": 3
      page_50:  1
      page_150: 2
    title: "Hello world": 10
      page_1: 2
      page_5: 1
      page_7: 3
...
...
2_blue_room: 114
  author: Wendy Brown
    title: "In the dark": 43
      page_8: 7
...

那么,有没有一种方法可以借助用户定义的类(或者可能使用其他一些工具)来处理这些数据?


共有1个答案

能远
2023-03-14

这是我如何开始的。我将创建一个Book类。然后我将为每个房间创建一个书籍%book的哈希值:

my $total-count = 0;
my @room-info;
for @rooms -> $room {
    my @room-authors;
    my %room-authors;
    my $room-count = 0;
    for @(%books{$room}) -> $book {
        my $count = $book.contains-word( $word );
        if $count > 0 {
            $total-count += $count;
            $room-count += $count;
            my $author = $book.author;
            if %room-authors{$author}:exists {
                $(%room-authors{$author}).update($book, $word, $count);
            }
            else {
                %room-authors{$author} = Room-Author.new(
                    book => $book, word => $word, count => $count
                );
                @room-authors.push( $author );
            }
        }
    }
    if @room-authors.elems > 0 {
        @room-info.push(
            Room-Info.new(
                room => $room, room-count => $room-count,
                order => @room-authors, hash => %room-authors
            )
        );
    }
}
say "Word: $word";
say "TOTAL: $total-count";

for @room-info -> $room {
    my @room-authors = $room.order;
    my %room-authors = $room.hash;
    say $room.room ~ " : " ~ $room.room-count;
    for @room-authors -> $author-str {
        my $author = %room-authors{$author-str};
        say "  author: " ~ $author.name ~ " : " ~ $author.count;
        for @($author.titles) -> $title {
            say "    title: " ~ $title.title ~ " : " ~ $title.count;
            for @($title.pages) -> $page {
                say "      page_" ~ $page.page ~ ": " ~ $page.count;
            }
        }
    }
}

在这里,类PageTitleRoom-InfoBookRoom-作者可以看起来像(注意:更多细节必须在真实代码中填写):

class Page {
    has Int $.page;
    has Int $.count;
}

class Title {
    has Str $.title;
    has Page @.pages;
    has Int $.count;
}

class Room-Info {
    has $.room;
    has $.room-count;
    has @.order;
    has %.hash;
}

class Book {
    has Str $.author;
    has Str $.title;
    has Str $.text;

    # checks how many times a word occurs in the book
    method contains-word ( $word, --> Int ) {
        return 2;  # Stub, insert more code here..
    }

    method get-page-matches( $word ) {
        return [Page.new(page => 50, count => 1),
                Page.new(page => 150, count => 2)]; # Stub, insert more code..
    }
}

class Room-Author {
    has Title @.titles;
    has Bool %!titles;
    has $.name;
    has $.count;

    submethod BUILD(:$book, :$word, :$!count) {
        my $title = $book.title;
        $!name = $book.author;
        %!titles{$title} = True;
        @!titles.push(
            Title.new(title => $title,
                      pages => $book.get-page-matches( $word ),
                      count => $!count,
                     )
        );
    }
    method update( $book, $word, $count ) {
        my $title = $book.title;
        $!count += $count;
        my $author = $book.author; # should be the same as $.name.. 
        if %!titles{$title}:exists {
            die "Unexpected: Duplicate title '$title' for $author";
        }
        else {
            %!titles{$title} = True;
            my Page @pages = $book.get-page-matches( $word );
            @!titles.push(
                Title.new(title => $title,
                          pages => $book.get-page-matches( $word ),
                          count => $count,
                         ) );
        }
    }
}
 类似资料:
  • 问题内容: 这是我的模型: User.java 我想建立一个这样的用户朋友表: users.jsf 由于用户很多,因此无法一次性转储用户表。 在这种情况下,数据表组件是理想的,因为它具有内置的分页支持。也是理想的,因为可以对列进行排序… 不幸的是,我无法通过Primefaces示例找到改变用户列行距的方法。 如何建立该数据表? 问题答案: 基于@Kerem的答案,这是我想出的解决方案: 为了使嵌套

  • 从另一个角度看这3行: 编辑:注意表有2000列,是否可以动态创建一个类(或向类添加属性),例如在Scala中从外部文件加载字段名和类型?我知道case类仅限于22个字段 Edit2:还要注意,任何属性都可以有多行(rowkey除外),即orderid、name、amount、supplier、account和1995+其他列,所以为所有这些属性创建单独的“singleline”类是不可行的,我正

  • 本文向大家介绍C++ 嵌套类/结构,包括了C++ 嵌套类/结构的使用技巧和注意事项,需要的朋友参考一下 示例 甲class或struct还可以包含另一个class/struct内部本身的定义,这被称为“嵌套类”; 在这种情况下,包含类称为“封闭类”。嵌套类定义被认为是封闭类的成员,但在其他方面则是单独的。 从封闭类的外部,可以使用范围运算符访问嵌套类。但是,在封闭类的内部,可以使用没有限定符的嵌套

  • 问题内容: 使用AJAX发出POST请求后,我得到以下JSON响应: 我正在使用数据表来显示数据..但是使用此嵌套的JSON,我无法直接获取数据。我正在使用此https://datatables.net/examples/server_side/post.html https://datatables.net/reference/option/ajax.dataSrc作为参考。 问题答案: 您必须

  • 使用JSF 2.0,我需要显示一个表,其中每一行都包含一个打开弹出窗口的链接。我有两种型号:

  • 像这样的形式: 我如何使用perl6自动获取表单数据?lkie this: 结果是这样的: perl6中有shome模块吗?请