I have noticed that more and more people are trying to apply perl solutions to problems by regurgitating code that solves but doesn't completely apply to the problem trying to be solved. There are alot of people learning perl who have all the required books, but bite off more they can chew. If people would break the overall problem into tiny little ones, most of the syntax problems you could have are solved for you in one of the books. Then your big problem just becomes a matter of applying correct programming theory, and some streamlining.
Case in point:
I am still new enough to perl that I live with the perl books, and I was looking for a way to specifically sort a list, and keep duplicates out at the same time in a minimal number of passes. I don't know if the solution I eventually came up with was the most efficient one possible, but it beat the hell out of my first attempt. For demonstration purposes I modified my solution to make it a bit weirder then what I needed. But I Basically took my problem and broke it into three steps, 1) How does one efficiently sort on multiple criteria? 2) How does one efficiently handle duplicates? 3) How do I combine the previous steps into an applicable solution?
This little snippit comes from combining lesson 4.6 and 4.15 in the perl cookbook. It takes a list and sorts it by ascending length, descending character value, and then removes the dupes
#! /usr/bin/perl use strict; my @unsorted = ("A","B","C",9,"B","B","AAA","D",4,"EE","BBB",55,"BBBB","CCC",55,"BBDB +","BBB","JJ","ZZZZZZ","A blue flamingo doesn't have spots"); my %listed=(); my @sorted = grep { ! $listed{$_} ++} map { $_->[1] } sort { $a->[0] <=> $b->[0] || $b->[1] cmp $a->[1] } map { [ length $_, $_ ] } @unsorted; for(@sorted){ print "$_\n"; }

Replies are listed 'Best First'.
RE: thoughts on learning perl basics
by merlyn (Sage) on Oct 06, 2000 at 06:02 UTC
    If you want no duplicates, a hash comes to mind. And since you are creating a hash anyway, you might as well make the values useful:
    my @unsorted = ("A","B","C",9,"B","B","AAA","D",4,"EE","BBB",55,"BBBB","CCC",55,"BBDB +","BBB","JJ","ZZZZZZ","A blue flamingo doesn't have spots"); my %tmp; $tmp{$_} = length $_ for @unsorted; my @sorted = { $tmp{$a} <=> $tmp{$b} or $a cmp $b } keys %tmp;

    -- Randal L. Schwartz, Perl hacker

      Do you mean:

      -my @sorted = { $tmp{$a} <=> $tmp{$b} or $a cmp $b } keys %tmp; +my @sorted = sort { $tmp{$a} <=> $tmp{$b} or $a cmp $b } keys %tmp;

      After Compline,
      Zaxo

Basic Perl Learning.
by frankus (Priest) on Oct 06, 2000 at 17:11 UTC
    I advocate looking in Llama before hitting the Cookbook or others: I still do: It has a wealth of hints on the style of Perl.

    Your code shows a great deal of thought. However, much of Perl's strength is found by ignoring your expectations of a programming language.

    What you percieve as recycled code is more likely code maxims: shorthand for everyday Perl activities.

    ( Maxims aren't clichés: clichés should be avoided like the plague :-)

    --
    
    Brother Frankus.