Hi. I'm a complete perl novice. I've knocked together some code to do what I want but I know there must be a more efficient way to do this. Can anyone improve on this code? Thanks in advance. Mint
#!/usr/bin/perl -w use strict; use warnings; # I want to parse a string containing bug fix identifiers such that # it will take a list of comma separated bugs, or single bugs prefixed # with "bug[s:]" ignoring whitespace, sort them and pick out only # unique identifiers and put them into an array. # The printing is just for testing each stage. my $bug_fix_msg = "this 33,44 is a fix for mybug44,99 real \ bug 99 again bug33r bug1722,3 bUg:99, \ bugs:456, 17, 24 bugs 42,58 bug: 50,50"; my @filtered = (); while ($bug_fix_msg =~ m/\bbugs?:?([\s\d,]*)\b/ig) { push @filtered, $1; print $1; print "\n"; } print "filtered: @filtered"; s/\s//g for(@filtered); my @bug_array = (); foreach my $bug_line(@filtered) { my @items = split(',', $bug_line); push @bug_array, @items; } my %hash = map { $_ => 1 } @bug_array; my @unique = keys %hash; my @sorted = sort {$a <=> $b} @unique; print "\nunsorted: @bug_array"; print "\n unique: @unique"; print "\n sorted: @sorted\n";
The output looks like this -
99 1722,3 99, 456, 17, 24 42,58 50,50 filtered: 99 1722,3 99, 456, 17, 24 42,58 50,50 unsorted: 99 1722 3 99 456 17 24 42 58 50 50 unique: 50 3 58 17 456 42 99 24 1722 sorted: 3 17 24 42 50 58 99 456 1722

In reply to Better way to do this, regex etc. by mintz

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.