editholla has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I have created the following code to create an html-based directory. I start by creating html documents for a group of directories and writing file names from each directory as hyperlinks into the documents. I then wanted to read each text file and chomp out all strings that are 5 characters long, begin with "4", and end with a letter. this code is effective until the section where I attempt to chomp the text files and store my desired strings as nothing prints nor are there any errors. I would like to know what I am doing in the last section that is not working. Thank you!#!/usr/bin/env perl use strict; use warnings; #Create the html page(s) for directories my $indexfile = "html file location"; unlink $indexfile or warn "Could not unlink $indexfile: $!"; unless(open FILE, '>', $indexfile){ die "\nUnable to create $indexfile\n"; } #Create an array of files listed in each directory my @array; my $path = "test"; opendir (my $DIR, $path); while ( my $entry = readdir $DIR ) { next unless -f $path . '/' . $entry; next if $entry eq '.' or $entry eq '..'; push @array, $entry; } #Denote data hash for collecting keys from text files my %data; #Fill in the files on newly created directory pages foreach (@array) { print FILE "<div>\n"; print FILE "<a href = \"/",$_.".html\"",">",$_;",</a>"; print FILE "</div>\n"; # Create pages for each file my $file = "html file location"; unlink $file or warn "Could not unlink $file: $!"; unless(open SUBFILE, '>', $file){ die "\nUnable to create $file\n"; } #Create an array of PNPs for each test rule #Open text file in the array my $pathtxt = $path."/".$_; open (FH , "+<" , $pathtxt) or die("Can't open $pathtxt: $!"); #Read each line while (my $line = <FH>){ #Find all instances of specific five character strings + #starts with 4, then 3 more numbers, then a letter) chomp $line; if ($line =~ /(4\d{4})/){ #and place in my previously declared data hash $data{$1}++; } } #Create scaler $k and use it to print my + #5-character strings to each file's html page my $k = (keys %data); print SUBFILE "<div>\n"; print SUBFILE "<a href = \"/",$k.".html\"",">",$k;",</a>"; print SUBFILE "<div>\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Chomping Lines, Storing as Hash, and Printing "keys" from Hash
by Athanasius (Archbishop) on Jan 06, 2016 at 15:45 UTC | |
|
Re: Chomping Lines, Storing as Hash, and Printing "keys" from Hash
by Corion (Patriarch) on Jan 06, 2016 at 15:36 UTC | |
by editholla (Initiate) on Jan 06, 2016 at 17:17 UTC | |
by poj (Abbot) on Jan 06, 2016 at 20:02 UTC | |
by Laurent_R (Canon) on Jan 06, 2016 at 18:50 UTC |