My example solution:

#!/usr/bin/perl use strict; my @certs; while(<DATA>) { push @certs, /\'([-.\w]+)\'/ if(/added/); } print join("\n", @certs), "\n"; __DATA__ Sep 12 17:30:02 nt-ca-na CA(Domain-CA): Certificate name 'jsmith.2048' + added to database. Sep 12 20:09:37 nt-ca-na CA(Domain-CA): Certificate name 'ksmith.2048' + added to database. Sep 12 21:25:30 nt-ca-na CA(Domain-CA): Certificate name 'ssmith.2048' + added to database.

Uses the default $_ instead of the problematic $var=<HANDLE> in a while ( which has a few though rare gotcha cases ). This avoids all the line =~ stuff which confuses what is going on.

uses push to add elements to the end of the array and the () matching in my RE to get exactly the text I wanted into the array ( the name without quotes ). Popping the quotes into the ()'s will add them to the text that is saved off if that is desired..

Used DATA handle so the example text could be in a functional example program. You would want to use your real filehandle on an open real file of course.

Used a statement modifier ( the if(/added/) ) since we want people to see that we are appending to an array inside a while loop, making the rule to append secondary to what is happening.

At the end @certs has the names added one per element in the array. There is a print to show that that is the case.


In reply to Re: Array Problem by dga
in thread Array Problem by dru145

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.