I think the core problem here, is you misunderstand what 'my' and 'our' does.

'my' creates a new variable, valid within the current scope, and as a new variable - it's undefined.

'our' is used to import variables from other packages. You probably don't want to use it.

Perldoc on 'our'

Perldoc on 'my'

Normally, 'my' is what you'll want to use, but when using it in a loop, if it's _inside_ the loop, you'll get a new variable each iteration.

So:

while (my $modelselect ne '10')

Is creating a new '$modelselect' each time you run that loop, which is almost certainly not what you want to do, because that way your loop says:

while (undef ne '10')

Likewise:

push (@switches, my $portcount);

Is doing the same thing. $portcount is being defined right there (and if you'd already declared it, would spit out an error) - you do actually have two separate 'my $portcounts' but they're in different subroutines, which means they're completely separate instances of the same variable.


In reply to Re: Strange undefined scalars in array by Preceptor
in thread Strange undefined scalars in array by GeorgMN

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.