Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

(code) Re: (2) More efficient munging... (improved lc() uc() snippet)

by ybiC (Prior)
on Jul 26, 2001 at 18:16 UTC ( #99983=note: print w/ replies, xml ) Need Help??

in reply to Re: More efficient munging if infile very large
in thread More efficient munging if infile very large

Thanks, synapse0 (and others who replied also) for the suggestions.   8^)

I'm not familiar with the eq 'lc' ? lc() : uc(); syntax from print OUT $uclc eq 'lc' ? lc() : uc();.   Would you be so kind as to explain how that works?

Updated: 2001-07-29 07:40 CDT

Here's what I ended up using.   Thanks again, and ++ to all who offered suggestions in nodes and CB!
    striving toward Perl Adept
    (it's pronounced "why-bick")

#!/usr/bin/perl -w use strict; my $uclc = shift or Usage(); Usage() unless ($uclc eq 'uc' or 'lc'); while(<>) { print $uclc eq 'uc' ? uc() : lc(); } sub Usage { print "\n Usage: (uc|lc) < infile > outfile\n\n"; exit; } =head1 NAME =head1 SYNOPSIS (uc|lc) < infile > outfile" Convert alpha characters in text file to uppercase (or lowercase). =head1 UPDATED 2001-07-26 16:30 CDT Simplify code for (in|out)put of STD(IN|OUT). Remove unnecessary "or Usage()" from first two input lines. Replace if/else with ternary "?:". my $munged; if ($uclc eq 'uc') { $munged = uc(); } else {$munged = lc(); } print OUT $munged; While instead of for so file read line-by-line not slurp entire fil +e. Post efficiency SoPW to PerlMonks 2001-07-25 Initial working code. =head1 TODOS None that I know of. =head1 TESTED ActivePerl 5.61 on Win2kPro =head1 AUTHOR ybiC =head1 CREDITS Thanks to synapse0, OeufMayo, crazyinsomniac, Masem, MZSanford, virtualsue, Hoffmater, tilly, clemburg, and ichimunki for suggestions. And to davorg for "Data Munging with Perl". Oh yeah, and to some guy named vroom. =cut

Comment on (code) Re: (2) More efficient munging... (improved lc() uc() snippet)
Select or Download Code
Replies are listed 'Best First'.
Re3: More efficient munging if infile very large (explain particular syntax?)
by Hofmator (Curate) on Jul 26, 2001 at 18:35 UTC

    from perlop:

    Conditional Operator Ternary "?:" is the conditional operator, just as in C. It works mu +ch like an if-then-else. If the argument before the ? is true, the arg +ument before the : is returned, otherwise the argument after the : is returned. For example: printf "I have %d dog%s.\n", $n, ($n == 1) ? '' : "s";
    for the gory details read on there ...

    -- Hofmator

(ichimunki) re x 4: More efficient munging if infile very large (explain particular syntax?)
by ichimunki (Priest) on Jul 26, 2001 at 18:31 UTC
    see perlman:perlop and look for the section on Conditional Operator. It's basically an in-place if-then type of construct that evaluates the EXPR $uclc eq 'lc' and returns lc() if the EXPR is true or uc() if the EXPR is false.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://99983]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2015-10-07 02:30 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (170 votes), past polls