adds loop to go through multiple books

This commit is contained in:
Jacob Haddon 2024-03-13 20:05:34 -04:00
parent 973dc8e8f5
commit 6f42818518

View File

@ -27,18 +27,39 @@ use JSON;
my $json = new JSON; my $json = new JSON;
my %books = ( my @books = (
title => "Wuthering Heights", {
author => "Emily Bronte", title => "Wuthering Heights",
url => "https://www.gutenberg.org/ebooks/768", author => "Emily Bronte",
file => "books/wutheringheights.txt" url => "https://www.gutenberg.org/ebooks/768",
file => "books/wutheringheights.txt"
},
{
title => "Jane Eyre",
author => "Charlotte Bronte",
url => "https://www.gutenberg.org/ebooks/1260",
file => "books/janeeyre.txt"
},
{
title => "Agnes Grey",
author => "Anne Bronte",
url => "https://www.gutenberg.org/ebooks/767",
file => "books/agnesgrey.txt"
}
); # books hash ); # books hash
# print($books{'file'}); print($books[1]{'file'});
# read in a whole file into a scalar my $k = 0;
my $book = read_file($books{'file'}); foreach (@books) {
print $books[$k]{'title'};
# read in a whole file into a scalar
my $book = read_file($books[$k]{'file'});
my @chapters = split('\nCHAPTER', $book); my @chapters = split('\nCHAPTER', $book);
my $test = @chapters; my $test = @chapters;
@ -96,22 +117,48 @@ foreach (@chapters) {
} # foreach chapters } # foreach chapters
$books[$k]{'pages'} = \@page_text;
$books{'pages'} = \@page_text;
$k++;
} # foreach books
my @tst_page = @{$books{'pages'} }; # my @tst_page = @{$books[$k]{'pages'} };
# print($tst_page[12][0]); # print($tst_page[12][0]);
# my $json_text = encode_json(\%books); # my $json_text = encode_json(\%books);
my $json_text = $json->pretty->encode( \%books ); my $json_text = $json->pretty->encode( \@books );
my $js_text = "let book = " . $json_text; my $js_text = "let book = " . $json_text;
# write the file # write the file
open(FH, '>', "test3.js") or die $!; open(FH, '>', "test4.js") or die $!;
print FH $js_text; print FH $js_text;
close(FH); close(FH);
# $books{'pages'} = \@page_text;
#
# my @tst_page = @{$books{'pages'} };
#
# # print($tst_page[12][0]);
#
# # my $json_text = encode_json(\%books);
# my $json_text = $json->pretty->encode( \%books );
#
# my $js_text = "let book = " . $json_text;
#
# # write the file
# open(FH, '>', "test3.js") or die $!;
# print FH $js_text;
# close(FH);
# print($page_text[12][0]); # print($page_text[12][0]);