Message Boards Message Boards

0
|
10186 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Appending to a List passed into a Function - Why List Not Same Shape Error?

Posted 10 years ago

I am trying to append to a list which starts out empty and accumulates elements if some conditional logic in the function is met. The problem is that when trying to add the first element to the list I get a "List { } and { with my entry} are not the same shape. Here is an example:

checkForAs[aList_,element_,myAccList_]:= Module[{resultValue},
If[Length[StringCases[ToString[aList],element]]>0,resultValue=aList, resultValue={}];
resultValue;
myAccList = Append[myAccList,resultValue]
]

myAccList = {};
testList = {{b,b,b,b,c,b,b},{a,c,b,b,c,c,c},{b,b,b,b,b,c,c,a,b,b,b,c,c},{c,c,c,c,c,b,b},{a,b,b,b,b,c}};

checkForAs[testList[[1]],"a",myAccList]
checkForAs[testList[[2]],"a",myAccList]

Out[477]= {{}} Set::shape: Lists {} and {{}} are not the same shape. >> Out[478]= {{a, c, b, b, c, c, c}} Set::shape: Lists {} and {{a,c,b,b,c,c,c}} are not the same shape. >>

POSTED BY: Bob Stephens
2 Replies

The reason for your error message is that the myAccList argument of checkForAs is not held. So inside of the function the expression

myAccList = Append[myAccList,resultValue]

has a list of one length on the left hand side and a list of a different length on the right hand side. A question that I then have is to ask what you actually want this function to do. If you want it to revise myAccList then there are structural ways to make that happen by adding holding attributes to the function. But if you only want to output the result, then just modify the last line as in

checkForAsRevised[aList_, element_, myAccList_] := 
 Module[{resultValue},
  If[Length[StringCases[ToString[aList], element]] > 0,
   resultValue = aList, resultValue = {}];
  Append[myAccList, resultValue]]

note that I also removed the line that reads

resultValue;

as this has no effect in the original function.

POSTED BY: David Reiss
Posted 10 years ago

Thanks, that makes sense. Since this Append is the last line in the function I am not actually trying to re-assign a variable but rather accumulate elements in a new list by applying this function to testList.

Here is the desired output I was looking for using your modifications with a couple additions.

thanks for your help.

checkForAs[aList_,element_,myAccList_]:= Module[{resultValue},
If[Length[StringCases[ToString[aList],element]]>0,
       resultValue=aList, 
           resultValue={}];
 Append[myAccList,resultValue]
]

myAccList = {};
testList = {{b,b,b,b,c,b,b},{a,c,b,b,c,c,c},{b,b,b,b,b,c,c,a,b,b,b,c,c},{c,c,c,c,c,b,b},{a,b,b,b,b,c}};

desiredList = Flatten[checkForAs[#,"a",myAccList] & /@ testList,1]
Select[desiredList, UnsameQ[#, {}] &]

Output[574]= {{}, {a, c, b, b, c, c, c}, {b, b, b, b, b, c, c, a, b, b, b, c, c}, {}, {a, b, b, b, b, c}}

Output[575]= {{a, c, b, b, c, c, c}, {b, b, b, b, b, c, c, a, b, b, b, c, c}, {a, b, b, b, b, c}}

POSTED BY: Bob Stephens
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract