Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I generally try to avoid operating on @_ and $_ wherever possible, to avoid confusion and pitfalls like what you may be experiencing.
@_ and $_ are idiomatic of Perl. The former is much a Perl5 thing, but the latter will also be there in Perl6, more powerful than ever! Their use can be confusing only to non-perl programmers trying to hack at Perl. Also, while you can avoid "operating" on $_ (but only if you want to make your life more complicated than necessary!), @_ is the only means to pass parameters to subs, so you can't avoid to "operate" on it. Or do you prefer to clobber your main namespace of global variables?
Here's some code I whipped up that I think does the operation you desire but in a slightly different way.
#!/usr/bin/perl use strict; use warnings;
Good!
my $var = ''; my @array = ();
No need for the initializations.
foreach my $line (<DATA>) {
While this does work, it is generally advised against as an extremely bad habit as it will slurp in the whole file at a time. Sometimes it doesn't make much harm, sometimes it is necessary, but it is not here and it would be recommendable not to spread bad programming habits amongst newbies.
exit;
No need whatsoever for this!

In reply to Re^2: Yet another array problem by blazar
in thread Yet another array problem by Angharad

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2020-06-01 06:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?