Sponsored Content
Full Discussion: Array question
Top Forums UNIX for Dummies Questions & Answers Array question Post 302753973 by bakunin on Wednesday 9th of January 2013 08:51:21 PM
Old 01-09-2013
The problem is the behavior of the "for"-loop in connection with an obvious misunderstanding about what "${arr[*]}" means.

The subscript "*" in an array denotes ALL array elements and it is expanded before the for-loop is executed. Therefore this is what the shell "sees":

Code:
for i in ${arr[*]}            # original line, begin of parsing process
for i in hello my name is     # after expanding the variables

Now, "$i" is assigned one word each pass of the for-loop because this is how this kind of loop behaves. There are two possibilities to correct this:

1. Use a while-loop instead:

Code:
echo ${arr[*]} | while read a ; do
     echo $a
done

2. Instead of using this faulty way of expanding the array count elements:

Code:
i=1
for i in {1..${#arr[*]}} ; do
     echo ${arr[$i]}
done

"${#arrayname[*]}" expands to the number of elements in your array (instead of the whole array itself), in your case: 2.

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

Storage array question

We just purchased a MOD30 disk array strage system. We have 15 drives and 2 hot spares. We're running a database app with 8 data sets. I'm trying to get the best i/o speed out of my disk configuration. Right now I have 3 raid5 arrays setup. This seems to offer the same performance as having the... (1 Reply)
Discussion started by: ncmurf00
1 Replies

2. UNIX for Dummies Questions & Answers

Array question

When setting a variable, how would I go about making each result a new line? A very simple example would be: theFolders=`(ls -l /)` echo $theFolders This gives me all the folders as one variable and I need to be able to use each as its own variable. I'm sure I have to make this into an... (2 Replies)
Discussion started by: TheCrunge
2 Replies

3. Shell Programming and Scripting

Array question

Hi all, I have a question does anyone know if it is possible to push or pop an array in the ksh environment? Could anyone give me a hint, because I am trying to merge 2 server files together and there are some names in the server is not proper anymore. Thank you in advance. (4 Replies)
Discussion started by: ahtat99
4 Replies

4. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

5. Shell Programming and Scripting

Variable Sized Array Length Question

I need to implement the following logic and need some expert help from UNIX community. These are the steps in my Shell script. 1. Analyze a file. 2. Extract all the ID's in that file. 3. Use the ID's from #2 to run another filter on the file. I've implemented # 1 and 2 using... (3 Replies)
Discussion started by: katwala
3 Replies

6. Shell Programming and Scripting

Help! Yet another check element in array Question

Greetings, DISCLAIMER: My shell scripting is rusty so my question may be borderline stupid. You've been warned. I need to create a script that a) lists the content of zip files in a directory and b) sends out an `exception` report. My ZIP files contain a control file (for load check). I want... (2 Replies)
Discussion started by: alan
2 Replies

7. Programming

array question

Im new to C programming and am having trouble understanding the output of this code int array={4,5,8,9,8,1,0,1,9,3}; int *array_ptr; int main() { array_ptr=array; while((*array_ptr) != 0) array_ptr++;; printf("%d\n", array_ptr - array); return(0); } the output is 6 but I... (2 Replies)
Discussion started by: sacat
2 Replies

8. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Zsh array -a vs. -A question

Inside a zsh function, I create a local array with local -a arrayname and a local associative array with local -A arrayname. I also can create an array using set, like this: set -A arrayname value1 value2 value3In this form, I can not explicitly declare that an array is associative or... (2 Replies)
Discussion started by: rovf
2 Replies

10. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies
ARRAY_MAP(3)								 1							      ARRAY_MAP(3)

array_map - Applies the callback to the elements of the given arrays

SYNOPSIS
array array_map (callable $callback, array $array1, [array $...]) DESCRIPTION
array_map(3) returns an array containing all the elements of $array1 after applying the $callback function to each one. The number of parameters that the $callback function accepts should match the number of arrays passed to the array_map(3) PARAMETERS
o $callback - Callback function to run for each element in each array. o $array1 - An array to run through the $callback function. o $... - Variable list of array arguments to run through the $callback function. RETURN VALUES
Returns an array containing all the elements of $array1 after applying the $callback function to each one. EXAMPLES
Example #1 array_map(3) example <?php function cube($n) { return($n * $n * $n); } $a = array(1, 2, 3, 4, 5); $b = array_map("cube", $a); print_r($b); ?> This makes $b have: Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) Example #2 array_map(3) using a lambda function (as of PHP 5.3.0) <?php $func = function($value) { return $value * 2; }; print_r(array_map($func, range(1, 5))); ?> Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 ) Example #3 array_map(3) - using more arrays <?php function show_Spanish($n, $m) { return("The number $n is called $m in Spanish"); } function map_Spanish($n, $m) { return(array($n => $m)); } $a = array(1, 2, 3, 4, 5); $b = array("uno", "dos", "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a, $b); print_r($c); $d = array_map("map_Spanish", $a , $b); print_r($d); ?> The above example will output: // printout of $c Array ( [0] => The number 1 is called uno in Spanish [1] => The number 2 is called dos in Spanish [2] => The number 3 is called tres in Spanish [3] => The number 4 is called cuatro in Spanish [4] => The number 5 is called cinco in Spanish ) // printout of $d Array ( [0] => Array ( [1] => uno ) [1] => Array ( [2] => dos ) [2] => Array ( [3] => tres ) [3] => Array ( [4] => cuatro ) [4] => Array ( [5] => cinco ) ) Usually when using two or more arrays, they should be of equal length because the callback function is applied in parallel to the corre- sponding elements. If the arrays are of unequal length, shorter ones will be extended with empty elements to match the length of the long- est. An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function Example #4 Creating an array of arrays <?php $a = array(1, 2, 3, 4, 5); $b = array("one", "two", "three", "four", "five"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); ?> The above example will output: Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) ) If the array argument contains string keys then the returned array will contain string keys if and only if exactly one array is passed. If more than one argument is passed then the returned array always has integer keys. Example #5 array_map(3) - with string keys <?php $arr = array("stringkey" => "value"); function cb1($a) { return array ($a); } function cb2($a, $b) { return array ($a, $b); } var_dump(array_map("cb1", $arr)); var_dump(array_map("cb2", $arr, $arr)); var_dump(array_map(null, $arr)); var_dump(array_map(null, $arr, $arr)); ?> The above example will output: array(1) { ["stringkey"]=> array(1) { [0]=> string(5) "value" } } array(1) { [0]=> array(2) { [0]=> string(5) "value" [1]=> string(5) "value" } } array(1) { ["stringkey"]=> string(5) "value" } array(1) { [0]=> array(2) { [0]=> string(5) "value" [1]=> string(5) "value" } } SEE ALSO
array_filter(3), array_reduce(3), array_walk(3), information about the callback type. PHP Documentation Group ARRAY_MAP(3)
All times are GMT -4. The time now is 12:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy