Posts

Showing posts with the label Foreach

Bash: Git Submodule Foreach?

Answer : The script passwd to git submodule is run with it's working directory set to the top of the given submodule...so you can simply look at pwd to see if you're in you're in the particular submodule. However, if you take some time to read the git submodule documentation, it turns out to be even easier: foreach Evaluates an arbitrary shell command in each checked out submodule. The command has access to the variables $name, $path, $sha1 and $toplevel: $name is the name of the relevant submodule section in .gitmodules, $path is the name of the submodule directory relative to the superproject, $sha1 is the commit as recorded in the superproject, and $toplevel is the absolute path to the top-level of the superproject. So you can do something like this: git submodule foreach '[ "$path" = "Libraries/JSONKit" ] \ && branch=experimental \ || branch=master; git co $branch'

Change $key Of Associative Array In A Foreach Loop In Php

Answer : unset it first in case it is already in the proper format, otherwise you will remove what you just defined: foreach($array as $key => $value) { unset($array[$key]); $array[ucfirst($key)] = $value; } You can't modify the keys in a foreach , so you would need to unset the old one and create a new one. Here is another way: $array = array_combine(array_map('ucfirst', array_keys($array)), $array); Get the keys using array_keys Apply ucfirst to the keys using array_map Combine the new keys with the values using array_combine The answers here are dangerous, in the event that the key isn't changed, the element is actually deleted from the array. Also, you could unknowingly overwrite an element that was already there. You'll want to do some checks first: foreach($array as $key => $value) { $newKey = ucfirst($key); // does this key already exist in the array? if(isset($array[$newKey])){ // yes, sk...

Bash Foreach Loop

Answer : Something like this would do: xargs cat <filenames.txt The xargs program reads its standard input, and for each line of input runs the cat program with the input lines as argument(s). If you really want to do this in a loop, you can: for fn in `cat filenames.txt`; do echo "the next file is $fn" cat $fn done "foreach" is not the name for bash. It is simply "for". You can do things in one line only like: for fn in `cat filenames.txt`; do cat "$fn"; done Reference: http://www.cyberciti.biz/faq/linux-unix-bash-for-loop-one-line-command/ Here is a while loop: while read filename do echo "Printing: $filename" cat "$filename" done < filenames.txt