samy1212,
Perhaps your problem is with scoping. If that's the case, you should take a look at the Tutorials section, in particular:
  • Coping with Scoping off site
  • Variable Scoping in Perl: the basics

    When you declare a variable - you are not only declaring it's name (what follows the leading sigil), it's type (scalar, hash, array, typeglob, etc), you are also declaring it's scope. When you declare a variable with my, you are restricting its scope to the nearest enclosing curly braces.

    Probably what you want to do is change its current value and not actually declare it again:

    my @array = qw(1 2 3); @array = qw(one two three) if $op eq 'alpha';
    Conditionally declaring a variable is generally considered a bad idea and can lead to problems down the road. So your code would change to:
    my @xxx; @xxx = qw(one two three) if $op eq 'numbers'; @xxx = qw(four five) if $op eq 'alpha'; @xxx = qw(six ten) if $op eq 'alp';
    Finally, if you want to use the same variable name, but in different scopes - that is perfectly acceptable:
    #!/usr/bin/perl -w use strict; hello('world'); goodbye('world'); sub hello { my $greeting = shift; print "hello $greeting\n"; } sub goodbye { my $greeting = shift; print "goodbye $greeting\n"; }
    I hope this helps - L~R

    In reply to Re: declaring same variable by Limbic~Region
    in thread declaring same variable by samy1212

    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.