Re: Square Brackets
- To: mathgroup at smc.vnet.net
- Subject: [mg92727] Re: Square Brackets
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 11 Oct 2008 06:46:43 -0400 (EDT)
On 10/10/08 at 4:36 AM, mariamsaleh.khan at gmail.com (mariam) wrote:
>i want to know that: 1)
>Do[{
>amplist[i1]= Read[StringJoin[jproc,"/",QCD[i1],"diagrams.amp"]],
>ileng[i1] = Length[amplist[i1]],
>Do[{
>Print["Processing amp[",i1,",",i2,"]"],
>If[i1===2,{amp[i1,i2] = amplist[i1][[i2]][[3]]}, {amp[i1,i2] =
>amplist[i1][[i2]][[2]]
>}],
>---------------------------------- and so on.
>What does amp[i1,i2] = amplist[i1] [[i2]] [[3]] mean? I mean any
>f[i.j]=g[i] [[j]] [[k]] I am confused about the double-brackets used
>[[----]].
Double square brackets are used to delimit indices into an array
or list or to refer to a part of an expression. Single square
brackets are used to delimit function arguments. For example:
f[1] = 8;
f[2] = 5;
f[3] = 7;
defines the values to be returned by the function f to be 8, 5
and 7 when evaluated at 1, 2 and 3 respectively. Often, it is
convenient to think of f as being an array or list rather than a
function. But writing
f[1.5] = 10;
is both valid and meaningful when f is viewed as a function.
Obviously, there can be no element 1.5 of an array of elements.
So,
amplist[i1][[i2]][[i3]] literally says evaluate the function
amplist at i1 then find part i2 of what amplist returns and then
return part i3 of that part.
>2) in another expression, itwo = StringPosition[jproc,"2"][[1,1]]
>what for [[ x, y]] is used?
StringPosition returns a list of start position, end position
pairs. That is
StringPosition["123","2"]
returns
{{2,2}}
The [[1,1]] part says take the pair at position 1 and return
element 1 of that pair which will be the starting position of
the sub-string to be found. Note, this could also have been
written as:
StringPosition["123","2"][[1]][[1]]