A few other issues: this line $key =~ /^(.)/; does nothing because you neither check for success of this match or use the successful result of $1. You can use a slightly different regex to do the job of getting the first letter and checking that it is lower case (I presume that you want to skip proper names like Bob?). That way writeFiles() doesn't have to do that job and it won't need a for loop.

Also $line should be passed as a parameter to writeFiles - try to minimize the number of global variables.

This code will work, but opening a file is "expensive". Further work could result in only opening each file one time.

Also if you had enabled warnings and strictures, you would see that this line
for ($i; $i<26; $i++); is not right. Also an indexed C style for loop is relatively rare in Perl. for my $letter (a..z){} would be more common in this situation - but as mentioned before, even that is not necessary.

#!/usr/bin/perl -w use strict; my %dictionary = ('dog' => 'pet', 'horse' =>'animal', 'doggie' => 'also a pet', 'Bob' => 'person'); sortDictionary(); sub sortDictionary { foreach my $key (sort keys %dictionary) { # process only keys with lower case first letter # e.g. 'Bob' => 'person' would be skipped my $first_letter; next unless (($first_letter) = $key =~ /^([a-z])/); my $line = $key . '/' . $dictionary{$key}; print "processing line: $line\n"; writeFiles( $first_letter, $line); } } sub writeFiles { my ($first_letter, $line) = @_; my $filename = $first_letter . "_words.txt"; print "file: $filename adding $line\n"; open FILE, '>>', $filename or die "can not open $filename $!"; print FILE "$line\n"; close FILE; return; } __END__ processing line: dog/pet file: d_words.txt adding dog/pet processing line: doggie/also a pet file: d_words.txt adding doggie/also a pet processing line: horse/animal file: h_words.txt adding horse/animal

In reply to Re: Function skips by Marshall
in thread Function skips by borovez

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.