I will try to explain in detail. When I post on PerlMonks, I try to be a bit more verbose than when I'm writing my own code, but I also try to show brief shortcuts...

# don't leave home without these ;) use warnings; use strict; # this could be from a file or whatever my @lines = ( "myserver.it.ca. XXXX XXX XXX XXXXXXXXXXX", "myserver.it.org. XXXX XXX XXX XXXXXXXXXXX", "myserver.it.com. XXXX XXX XXX XXXXXXXXXXX", ); # in the next line, we take every array element of the above @lines, # and work on them one at a time. $line contains each # of the array elements, in order for my $line (@lines){ # the below split() is a bit of trickery. we split the line on # whitespace (\s) (the + is superfluous in this case, it means # "one or more" (you had a tab), so we split on ANY whitespace). # # because I wrapped the split() within parens (()), that forces # it into list context, so I treat it as an array (of sorts), # and after the split() executes, I can immediately take # the first element of it ([0]), and assign it to $server. # # that isn't anything special... it's just avoiding splitting # into a named array, then having to take the first element # from that and putting it into the variable... two-stage example: # # my @servers = split /\s/, $line; # my $server = $servers[0]; my $server = (split /\s+/, $line)[0]; # the following does a substitution... it replaces a '.' # (dot, period) character at the very end of $server with nothing # (ie. it removes it) in your OP, you had s/.$//. In Perl # (and in sed), a '.' represents *any* character (perl doesn't # recognize a '.' as a newline by default, I don't know about sed) # I changed it so it looks for a dot explicitly by escaping it # with a backslash (\). After the substitution, we print... $server =~ s/\.$//; print "$server\n"; }

In reply to Re^5: perl to run awk and sed by stevieb
in thread perl to run awk and sed by deelinux

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.