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"; }

In reply to Chomping Lines, Storing as Hash, and Printing "keys" from Hash by editholla

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.