it is a mess, but it works, makes one file with the books in pages, makes a second with a year's worth of pages at random.
todo: check no repeats, clean up the code.
This commit is contained in:
parent
6f42818518
commit
d5361e391e
77
books.pl
77
books.pl
@ -6,6 +6,9 @@ use strict;
|
||||
use warnings;
|
||||
use File::Slurp; # https://metacpan.org/pod/File::Slurp
|
||||
use JSON;
|
||||
use Time::Piece;
|
||||
|
||||
use DateTime;
|
||||
|
||||
###################################
|
||||
#
|
||||
@ -27,6 +30,9 @@ use JSON;
|
||||
|
||||
my $json = new JSON;
|
||||
|
||||
# my $date = localtime->strftime('%Y-%m-%d');
|
||||
# print $date;
|
||||
|
||||
my @books = (
|
||||
{
|
||||
title => "Wuthering Heights",
|
||||
@ -48,13 +54,15 @@ my @books = (
|
||||
}
|
||||
); # books hash
|
||||
|
||||
print($books[1]{'file'});
|
||||
# print($books[1]{'file'});
|
||||
|
||||
# print(int(rand($for)));
|
||||
|
||||
my $k = 0;
|
||||
|
||||
foreach (@books) {
|
||||
|
||||
print $books[$k]{'title'};
|
||||
# print $books[$k]{'title'};
|
||||
|
||||
|
||||
|
||||
@ -63,7 +71,7 @@ my $book = read_file($books[$k]{'file'});
|
||||
|
||||
my @chapters = split('\nCHAPTER', $book);
|
||||
my $test = @chapters;
|
||||
print($test);
|
||||
# print($test);
|
||||
|
||||
# number of lines in a page
|
||||
my $number_of_lines = 31;
|
||||
@ -141,7 +149,70 @@ print FH $js_text;
|
||||
close(FH);
|
||||
|
||||
|
||||
# make a list of 365 pages
|
||||
my $for = @books;
|
||||
my @page_list;
|
||||
my %page_list2;
|
||||
my $dt = DateTime->now();
|
||||
# print $dt->ymd . "\n";
|
||||
for ( my $l; $l < 365; $l++){
|
||||
# print $l;
|
||||
# $page_list[$l] = $l;
|
||||
my $book_list = int(rand($for));
|
||||
# print $book_list;
|
||||
|
||||
# my %book1 = $books[$book_list];
|
||||
# print $books[$book_list]{'title'};
|
||||
my $chapter_list = @{$books[$book_list]{'pages'}};
|
||||
# print $chapter_list;
|
||||
my $chapter_select = int(rand($chapter_list));
|
||||
# print $chapter_select;
|
||||
my $page_list = @{$books[$book_list]{'pages'}[$chapter_select]};
|
||||
# print $page_list;
|
||||
my $page_select = int(rand($page_list));
|
||||
my $page_rand = $books[$book_list]{'pages'}[$chapter_select][$page_select];
|
||||
# print $page_rand;
|
||||
# my $page_select = int(rand($page_list));
|
||||
$page_list[$l] = {
|
||||
title => $books[$book_list]{'title'},
|
||||
author => $books[$book_list]{'author'},
|
||||
page => $page_rand
|
||||
};
|
||||
|
||||
# my $new_test = $dt->add(days => 1)->ymd;
|
||||
# print $new_test . "\n";
|
||||
|
||||
$page_list2{$dt->add(days => 1)->ymd} = {
|
||||
title => $books[$book_list]{'title'},
|
||||
author => $books[$book_list]{'author'},
|
||||
page => $page_rand
|
||||
};
|
||||
|
||||
|
||||
# print $dt->add(days => 1)->ymd;
|
||||
# my $new_date = $dt->add(days => $l);
|
||||
# print $new_date->ymd;
|
||||
# for ( my $m; $m < $for; $m++) {
|
||||
#
|
||||
#
|
||||
# } # for m
|
||||
#
|
||||
} # for l
|
||||
|
||||
# print scalar keys %page_list2;
|
||||
|
||||
# print $page_list[55]{'title'};
|
||||
#
|
||||
|
||||
# my $json_text = encode_json(\%books);
|
||||
my $json_text2 = $json->pretty->encode( \%page_list2 );
|
||||
|
||||
my $js_text2 = "let book = " . $json_text2;
|
||||
|
||||
# write the file
|
||||
open(FH, '>', "test5.js") or die $!;
|
||||
print FH $js_text2;
|
||||
close(FH);
|
||||
|
||||
|
||||
# $books{'pages'} = \@page_text;
|
||||
|
Loading…
x
Reference in New Issue
Block a user