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

    Hello editholla,

    I haven’t studied your code in detail, but this section leaps to the eye:

    #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})/){

    That regex will match any sequence of 5 digits beginning with a 4. However, the comment suggests you need this:

    /(4\d{3}[a-z]/i

    but that will also match a substring as part of a longer string. Is that what you intended? If not, add word boundary assertions:

    /\b(4\d{3}[a-z])\b/i

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Chomping Lines, Storing as Hash, and Printing "keys" from Hash
by Corion (Patriarch) on Jan 06, 2016 at 15:36 UTC

    What steps have you taken to isolate the problem?

    Is the recursive parsing of the directory and the writing to FILE relevant to the problem?

    Have you printed out the progress of your program and inspected whether it processes all the subfiles you want it to process?

    Have you printed out the progress of your program and inspected whether it processes all the 4-digit sequences?

    Have you inspected how your program outputs the data it has collected? Does the program output the correct information if you hardcode data into %data instead of collecting it from files?

    If you work through these steps, either you find a solution or you get your program to a state where you can ask better questions by describing how your program behaves and what you expect and where the differences are between the expected behaviour and the observed behaviour.

      Corion,

      My bad, Corion. I am in my second week of Perl (and mostly programming as a whole) and I am struggling. Let me try to explain in more detail.

      **UPDATE**:

      I figured it all out. Thank you, Corian. After you reminded me to step through my process, I ended up figuring out the solution on my own.

        Take a look at here-doc in Quote and Quote like Operators

        #!perl use strict; my %data = ( 11111=>1,22222=>2 ); open FILEPNP,'>','zzzzz.tmp' or die "$!"; for my $k (keys %data){ print FILEPNP <<EOF; <div> <a href="/$k.html">$k</a> </div> EOF }
        poj
        After you reminded me to step through my process, I ended up figuring out the solution on my own.
        Very good, you've just learned something that will be very useful to you many many times, perhaps even during an entire life of a programmer. :)