The code at bottom works fine for converting all alpha characters in a text file to upper (or lower) case.   But having the two "$munged = " lines inside the "for(@in)" loop looks like ineficient if handling large files.

I tried these two tweaks to move that logic outside the for(@in) loop.   The first doesn't work because (correct me if I'm wrong) $_ is empty when $munged is declared.   The second fails with this compilation error: syntax error at uclc.pl line (push @munged, $munged();), near "$munged(" and I'm not clear just what's happening there.

# tweak one my @munged; my $munged = lc() if ($uclc eq 'lc'); $munged = uc() if ($uclc eq 'uc'); for(@in) { push @munged, $munged; } # tweak two my @munged; my $munged = lc if ($uclc eq 'lc'); $munged = uc if ($uclc eq 'uc'); for(@in) { push @munged, $munged(); }
So my questions for the wise bretheren and sisteren are:
  1. should I be concerned about potential ineficiency of this code for large input files?
  2. if so, how might it be improved?
    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")
#!/usr/bin/perl -w use strict; my $uclc = shift or Usage(); my $infile = shift or Usage(); my $outfile = shift or Usage(); Usage() unless ($uclc eq 'lc' or 'uc'); open (IN, "< $infile") or die "Error opening $infile for read: $!"; my @in = <IN>; close IN or die "Error closing $infile after read: $!"; my @munged; for(@in) { my $munged = lc() if ($uclc eq 'lc'); $munged = uc() if ($uclc eq 'uc'); push @munged, $munged; } open (OUT, "> $outfile") or die "Error opening $outfile for write: $!" +; print OUT for(@munged); close OUT or die "Error closing $outfile after write: $ +!"; ###################################################################### +#### sub Usage { die "\n Usage: uclc.pl (lc|uc) infile outfile\n"; } ###################################################################### +####

In reply to More efficient munging if infile very large by ybiC

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.