Drkcore

21 10 2008 perl Tweet

肥大化するリスト

こんな感じで肥大化するリストをつくると(実際にやりたいことは実行途中のあるタイミングでリストの先頭に戻ってループさせながらリストを大きくしたい)

my @l = (0);

for my $n (@l) {
  push @l,$n+1;
  print $n,"\n";
}

数百万件のデータを処理したいので、これをDBIx::Classを使ってやろうと。

で、こんな風に書きました。

#!/usr/bin/perl

#-- tables
#
#create table compounds (
#    id         integer PRIMARY KEY not null,
#    num        integer not null
#);
#insert into compounds values(null,1);

package TestSchema;
use strict;
use base qw/DBIx::Class::Schema::Loader/;

__PACKAGE__->loader_options(
    debug => 1,
    exclude => qr/^sqlite_/,
);  

package Main;

my $schema = TestSchema->connect('dbi:SQLite:dbname=test.db');
my $rs = $schema->resultset('Compounds')->search;

while (my $row = $rs->next) {
  my $n = $row->num;
  $schema->resultset('Compounds')->create({num => $n+1});
  printf "* %s\n", $n;
}

この場合、データの行数が一つだとうまくいかない(next一回で終了してしまう。2個以上データが入っていればリストの時のコードの様に動く)。でも、初期データは一つしかないので困る。ダミーでなんか入れとけばいいのだけど、BFSっぽく繰り返したい場合ダミーのデータは邪魔だし、なんか無駄なことやっている気がする。

うまいやり方ないもんか。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021