# array variables

**URL:** <https://community.unix.com/t/array-variables/289762>\
**Category:** Shell Programming and Scripting\
**Created:** [June 14, 2011, 8:13am UTC](https://community.unix.com/t/array-variables/289762 "2011-06-14T08:13:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 14, 2011, 8:13am UTC](https://community.unix.com/t/array-variables/289762/1 "2011-06-14T08:13:48Z")

</div>

Hi,

There are total 100 files in the backup directory with the name format as below:

```nohighlight
prod_bkp_140611_13_30_05.txt
prod_bkp_140611_14_30_05.txt
prod_bkp_140611_15_30_05.txt
prod_bkp_140611_16_30_05.txt

```

How to use array variables (in perl) to get list of names of the above files ?

---

<div class="post-metadata">

**Author:** ![getmmg](https://community.unix.com/letter_avatar/getmmg/32/6_22734026db12803ebb3e059622ac3534.png) [@getmmg](https://community.unix.com/u/getmmg)\
**Post date:** [June 14, 2011, 8:14am UTC](https://community.unix.com/t/array-variables/289762/2 "2011-06-14T08:14:35Z")

</div>

Is this the output of ls command?

---

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 14, 2011, 8:19am UTC](https://community.unix.com/t/array-variables/289762/3 "2011-06-14T08:19:23Z")

</div>

yes this is the o/p of ls command.

I want to assign this o/p to the array variable for the further processing in code.

how to assign this values to array variable ?

---

<div class="post-metadata">

**Author:** ![getmmg](https://community.unix.com/letter_avatar/getmmg/32/6_22734026db12803ebb3e059622ac3534.png) [@getmmg](https://community.unix.com/u/getmmg)\
**Post date:** [June 14, 2011, 8:21am UTC](https://community.unix.com/t/array-variables/289762/4 "2011-06-14T08:21:47Z")

</div>

This should store the output of ls into the array.

```nohighlight
 
@arr = `ls`;

```

Use the code below if you want to harcode.

```nohighlight
 
@arr = qw(prod_bkp_140611_13_30_05.txt prod_bkp_140611_14_30_05.txt prod_bkp_140611_15_30_05.txt prod_bkp_140611_16_30_05.txt);

```

---

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 14, 2011, 8:32am UTC](https://community.unix.com/t/array-variables/289762/5 "2011-06-14T08:32:00Z")

</div>

Hi,

[oracle@andews:/oracle/backup] $ ls  
prod\_bkp\_140611\_13\_30\_05.txt  
prod\_bkp\_140611\_14\_30\_05.txt  
prod\_bkp\_140611\_15\_30\_05.txt  
prod\_bkp\_140611\_16\_30\_05.txt

[oracle@andews:/oracle/backup] $ @arr=`ls`  
-bash: @arr=prod\_bkp\_140611\_13\_30\_05.txt: command not found

[oracle@andews:/oracle/backup] $ @arr = `ls`  
-bash: @arr: command not found

[oracle@andews:/oracle/backup] $ @arr = `ls`;  
-bash: @arr: command not found

---

<div class="post-metadata">

**Author:** ![getmmg](https://community.unix.com/letter_avatar/getmmg/32/6_22734026db12803ebb3e059622ac3534.png) [@getmmg](https://community.unix.com/u/getmmg)\
**Post date:** [June 14, 2011, 8:37am UTC](https://community.unix.com/t/array-variables/289762/6 "2011-06-14T08:37:00Z")

</div>

Execute with perl.  
Some thing like below.

```nohighlight
 
perl -e ' @arr = `ls`;print @arr'

```

---

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 14, 2011, 9:13am UTC](https://community.unix.com/t/array-variables/289762/7 "2011-06-14T09:13:51Z")

</div>

Hi,

It works.

Now, I want the stored array values to be used as below in the further code :-

For example:

BackupFile:'prod\_bkp\_140611\_13\_30\_05.txt','prod\_bkp\_140611\_14\_30\_05.txt','prod\_bkp\_140611\_15\_30\_05.txt','prod\_bkp\_140611\_16\_30\_05.txt'

how it can be done ?

---

<div class="post-metadata">

**Author:** ![getmmg](https://community.unix.com/letter_avatar/getmmg/32/6_22734026db12803ebb3e059622ac3534.png) [@getmmg](https://community.unix.com/u/getmmg)\
**Post date:** [June 14, 2011, 10:35am UTC](https://community.unix.com/t/array-variables/289762/8 "2011-06-14T10:35:31Z")

</div>

This should do what you want.

```nohighlight
 
ls | perl -00 -lane 'BEGIN{$,=","}END{print map {qq|\x27$_\x27|} @F;}'

```

---

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 15, 2011, 6:52am UTC](https://community.unix.com/t/array-variables/289762/9 "2011-06-15T06:52:46Z")

</div>

Hi,

can u please explain the below line :

print map {qq|\x27$\_\x27|} @F;

---

<div class="post-metadata">

**Author:** ![getmmg](https://community.unix.com/letter_avatar/getmmg/32/6_22734026db12803ebb3e059622ac3534.png) [@getmmg](https://community.unix.com/u/getmmg)\
**Post date:** [June 15, 2011, 6:57am UTC](https://community.unix.com/t/array-variables/289762/10 "2011-06-15T06:57:12Z")

</div>

27 is the hexcode for ' (single quote) charecter. Since you wanted your output to be wrapped inside it, added that piece of code to map it.

---

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 15, 2011, 7:28am UTC](https://community.unix.com/t/array-variables/289762/11 "2011-06-15T07:28:02Z")

</div>

Hi,

xtremly sorry .. I am not getting the solution mention by you

but want to insert the file names as shown below in bold:

CREATE TABLE PROD\_BKP  
(  
Column1 NUMBER(20),  
Column2 VARCHAR2(25),  
Column3 NUMBER(1),  
Column4 NUMBER(10),  
Column5 NUMBER(10)  
)  
ORGANIZATION EXTERNAL  
( TYPE ORACLE\_LOADER  
DEFAULT DIRECTORY EXTRACT\_DIR  
ACCESS PARAMETERS  
( RECORDS DELIMITED BY NEWLINE  
FIELDS TERMINATED BY '|'  
MISSING FIELD VALUES ARE NULL  
)  
LOCATION (EXTRACT\_DIR:'prod\_bkp\_140611\_13\_30\_05.txt','prod\_bkp\_140611\_14\_30\_05.txt','prod\_bkp\_140611\_15\_30\_05.t xt','prod\_bkp\_140611\_16\_30\_05.txt')  
)  
REJECT LIMIT UNLIMITED  
PARALLEL ( DEGREE 5 INSTANCES 1 )  
NOMONITORING;

---

<div class="post-metadata">

**Author:** ![dharanivasan](https://community.unix.com/letter_avatar/dharanivasan/32/6_22734026db12803ebb3e059622ac3534.png) [@dharanivasan](https://community.unix.com/u/dharanivasan)\
**Post date:** [June 15, 2011, 8:14am UTC](https://community.unix.com/t/array-variables/289762/12 "2011-06-15T08:14:07Z")

</div>

my @Array = \<\*.txt\>;

print "EXTRACT\_DIR:'".join("','", @Array)."'";

This can be solve your problem.

---

<div class="post-metadata">

**Author:** ![getmmg](https://community.unix.com/letter_avatar/getmmg/32/6_22734026db12803ebb3e059622ac3534.png) [@getmmg](https://community.unix.com/u/getmmg)\
**Post date:** [June 15, 2011, 8:14am UTC](https://community.unix.com/t/array-variables/289762/13 "2011-06-15T08:14:34Z")

</div>

```nohighlight
ls | perl -00 -lane 'BEGIN{$,=","}END{print map {qq|\x27$_\x27|} @F;}'

```

Consider this as the output of your ls command

```nohighlight
 
File1
File2

```

This will get stored in @F array like below.

```nohighlight
 
$F[0] = File1
$F[1] = File2

```

Since you wanted the output separted by comma $, (output separtor in perl) is set as -.

But if you try to print the @F now you'll get an output like this without the single quotes

```nohighlight
File1,File2

```

Since you wanted every item of array @F to be wrapped in single quotes below statement is added before print.

```nohighlight
 
print map {qq|\x27$_\x27|} @F

```

This is same as writing

```nohighlight
 
print map {"\x27$_\x27"} @F

```

Map function wraps the array contents inside a single quote. (x27 is hex code equivalent of ' ).

---

<div class="post-metadata">

**Author:** ![milink](https://community.unix.com/letter_avatar/milink/32/6_22734026db12803ebb3e059622ac3534.png) [@milink](https://community.unix.com/u/milink)\
**Post date:** [June 16, 2011, 6:13am UTC](https://community.unix.com/t/array-variables/289762/14 "2011-06-16T06:13:31Z")

</div>

Hi,

Instead of 'print' I want the values of @F to be used/added as below in the command:

LOCATION (EXTRACT\_DIR:'prod\_bkp\_140611\_13\_30\_05.txt','prod\_bkp\_140611\_14\_30\_05.txt','prod\_bkp\_140611\_15\_30\_05.t xt','prod\_bkp\_140611\_16\_30\_05.txt')

I tried as below but not working:

LOCATION (EXTRACT\_DIR:map {"\x27$\_\x27"} @files)
