我从用户那里获取元素的输入并将其显示在文件中。但是,我需要再做一项任务。我需要用点(.
)替换每个数组元素的第一个字符,遗憾的是这不适合我。
请在下面找到我的代码:
while read line
do
my_array=("${my_array[@]}" $line)
done
echo ${my_array[@]/my_array[@][0]/.}
非常感谢任何帮助,谢谢。
答案 0 :(得分:2)
使用GNU bash:
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
/**
* Created by IDEA on 2/26/16.
*/
class Tree[T:ClassTag](val value: T, val children: List[Tree[T]]) {
def paths = {
val pathsBuffer = new ArrayBuffer[Array[T]]()
val pathBuffer = new ArrayBuffer[T]()
def helper(tree: Tree[T]): Unit = {
if (tree != null) {
pathBuffer.append(tree.value)
if (!tree.children.isEmpty) {
tree.children.foreach {
t => helper(t)
}
} else {
pathsBuffer.append(pathBuffer.toArray)
}
pathBuffer.remove(pathBuffer.length - 1)
}
}
helper(this)
pathsBuffer.toArray
}
}
object Tree {
def makeTree: Tree[Int] = {
new Tree[Int](
1,
List(
new Tree[Int](
2,
List.empty[Tree[Int]]
),
new Tree[Int](
3,
List(
new Tree[Int](
5,
List.empty
),
new Tree[Int](
6,
List.empty
)
)
),
new Tree[Int](
4,
List(
new Tree[Int](
7,
List.empty
),
new Tree[Int](
8,
List.empty
)
)
)
)
)
}
}
object TestTree extends App {
val t = Tree.makeTree
println(t.paths.map(_.toList).toList)
}
输出:
.oo .ar .bc
答案 1 :(得分:1)
您不需要先填充数组,然后再进行后期处理。在填充数组本身时,您可以这样做:
my_array=()
while read -r line; do
my_array+=( ".${line:1}" )
done
".${line:1}"
会将DOT放在第一位,然后从下一个位置开始放置$line
的子串。my_array+=( ".${line:1}" )