Sadly there are a few issues with that code. Some are transcription errors, some are coding convention related, some are bugs and one is at odds with a PerlMonks' convention.

First the PerlMonks' convention: runnable stand alone code. By removing the file handling and using DATA instead it's easy to make your code stand alone.

Coding conventions: always use strictures (use strict; use warnings;). Use a consistent indentation style (Perl tends toward K&R with 4 character indents). Use the three parameter version of open and test for errors (open ... or die "... $!\n" by convention). Don't slurp (my @arr = <DATA>;). Use blank lines to break your code up into "paragraphs". Comment tricky stuff (your use of $flag and the appended 1 for example).

Bugs: it doesn't work! I get two one row clusters then everything else in one cluster. With strictures on there are "Use of uninitialized value" warnings.


Perl's payment curve coincides with its learning curve.

In reply to Re^5: clustering pairs by GrandFather
in thread clustering pairs by sugar

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.