Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
welcome AnonymousMonk,

First of all it is a good thing you want to learn experiencing but, BUT! trying to do something without using the simpler, acclaimed way (an hash for uniqueness) maybe is not so good idea, worst if you are beginning. The satandard way is a FAQ infact (infaq?)

Anyway it is possible for example using the smart match operator (it is sperimental) which syntax is ~~ see Smartmatch-Operator and act, in the form $scalar ~~ @array like if array contains scalar

my @arr = (0,1,2,3,1,2); my @uniq; foreach (sort{$a<=>$b} @arr){ unless($_ ~~ @uniq){push @uniq, $_} } print join ' ', @uniq;

This is very similar to another existing builtin tool any from List::Util (update: fixed from Utils to Util) core module:

use List::Util qw(any); my @arr = (0,1,2,3,1,2); my @uniq; foreach my $it (sort{$a<=>$b} @arr){ unless(any{$_ == $it}@uniq){push @uniq, $it} } print join ' ', @uniq;

I say this because it is very important and crucial to know which Perl tool to use for a task: this can save hours of your precious time: get the habit to read across perldoc to know everyday more: functions tutorials, core modules.. perldoc save your day!

Then, instead of start spending several hours trying to get uniqness without using an hash it will be by far better to read some good manual like ModernPerl.pdf the free book, or to get a copy if the even old PerlCookbook (a must have to get a wide panorama of solutions and fields to play with).

I say this because me too i'm somehow a Perl-just-for-fun (I use at work too but when conceiled and i just show perl result and benefits when i've done).

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Trudging along the learning perl path. -- wrong path? :=) by Discipulus
in thread Trudging along the learning perl path. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2024-04-19 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found