# Question about variable scope inside function

**URL:** <https://forum.kx.com/t/question-about-variable-scope-inside-function/6989>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [February 14, 2011, 7:24am UTC](https://forum.kx.com/t/question-about-variable-scope-inside-function/6989 "2011-02-14T07:24:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bovine](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@bovine](https://forum.kx.com/u/bovine)\
**Post date:** [February 14, 2011, 7:24am UTC](https://forum.kx.com/t/question-about-variable-scope-inside-function/6989/1 "2011-02-14T07:24:00Z")

</div>

I’ve written the following code to load quote data to a partitionedtable (follow the example from kdb+ for mortals):ldchunk:{.Q.en[`:/db;] select code,id,time,datatype,val from flip`code`id`d`time`datatype`val!("IIDTSF";6 10 9 10 3 16) 0: x wheredatatype within `CA`CB}ldata1:{[fn] d:"D"$ssr[fn;":/HKSE/BidAsk/ba_mb_";""]; / d is the date parsedfrom the filename h1:` sv `:`db,(`$string d),`quote1`; /h1 is the file handler of thepartition of that date .Q.fs[{0N!.[h1;();,;ldchunk x]}] `$fn}however, I got a error:{0N!.[h1;();,;ldchunk x]}'h1it seems in .Q.fs[{…}] function call, it cannot recognize ldata1’slocal variable in the embedded function definition.In this case, could anyone tell me how to make this piece of codework?Thanks!

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 14, 2011, 8:06am UTC](https://forum.kx.com/t/question-about-variable-scope-inside-function/6989/2 "2011-02-14T08:06:00Z")

</div>

&nbsp;.Q.fs[{0N!.[x;();,;ldchunk y]}h1] `$fn

---

<div class="post-metadata">

**Author:** ![bovine](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@bovine](https://forum.kx.com/u/bovine)\
**Post date:** [February 14, 2011, 8:24am UTC](https://forum.kx.com/t/question-about-variable-scope-inside-function/6989/3 "2011-02-14T08:24:00Z")

</div>

Thanks! It works for my case.On Feb 14, 4:06?pm, Charles Skelton \<char…\> wrote:\> ?.Q.fs[{0N!.[x;();,;ldchunk y]}h1] `$fn&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; On Mon, Feb 14, 2011 at 8:24 AM, bovine <chenying...> wrote:&gt; &gt; I've written the following code to load quote data to a partitioned&gt; &gt; table (follow the example from kdb+ for mortals):&gt;&gt; &gt; ldchunk:{.Q.en[`:/db;] select code,id,time,datatype,val from flip\> \> `code`id`d`time`datatype`val!(“IIDTSF”;6 10 9 10 3 16) 0: x where\> \> datatype within `CA`CB}\>\> \> ldata1:{[fn]\> \> ?d:“D”$ssr[fn;“:/HKSE/BidAsk/ba\_mb\_”;“”]; ?/ d is the date parsed\> \> from the filename\> \> ?h1:`sv`:`db,(`$string d),`quote1`; ?/h1 is the file handler of the\> \> partition of that date\> \> ?.Q.fs[{0N!.[h1;();,;ldchunk x]}] `$fn\> \> }\>\> \> however, I got a error:\> \> {0N!.[h1;();,;ldchunk x]}\> \> 'h1\>\> \> it seems in .Q.fs[{…}] function call, it cannot recognize ldata1’s\> \> local variable in the embedded function definition.\>\> \> In this case, could anyone tell me how to make this piece of code\> \> work?\>\> \> Thanks!\>\> \> –\> \>

Submitted via Google Groups\</chenying…\>\</char…\>
