Hello again alokranjan,

Now I would like to print the name of file itself (means country name) by passing the name of city to a subroutine. But value is not changing. (Emphasis added.)

I notice that you use strict but not warnings. When I run your code with perl -wc I get:

23:34 >perl -wc 1342_SoPW.pl Variable "$name" will not stay shared at 1342_SoPW.pl line 39. 1342_SoPW.pl syntax OK 23:35 >

And with diagnostics:

23:35 >perl -Mdiagnostics -wc 1342_SoPW.pl Variable "$name" will not stay shared at 1342_SoPW.pl line 39 (#1) (W closure) An inner (nested) named subroutine is referencing a lexical variable defined in an outer named subroutine. When the inner subroutine is called, it will see the value of the outer subroutine's variable as it was before and during the *f +irst* call to the outer subroutine; in this case, after the first call t +o the outer subroutine is complete, the inner and outer subroutines will + no longer share a common value for the variable. In other words, the variable will no longer be shared. This problem can usually be solved by making the inner subroutine anonymous, using the sub {} syntax. When inner anonymous subs tha +t reference variables in outer subroutines are created, they are automatically rebound to the current values of such variables. 1342_SoPW.pl syntax OK 23:36 >

This explains why the value is not changing: on the first call to getCountry the variable $name is set to “NY” in both getCountry and txtsearch. But on the second call, $name becomes “London” in getCountry but remains “NY” within txtsearch. This is easily confirmed by adding print statements at appropriate points within these subs.

Moral: always use warnings !

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Grep a word and print the filename by Athanasius
in thread Grep a word and print the filename by alokranjan

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.