Message Boards Message Boards

[✓] Append a string to an array (list?) of numbers?

GROUPS:

Edit : Resolved: See below.

I have an array generated from the following:

array1=Range[10]
array1={1,2,3,4,5,6,7,8,9,10}

As expected. However, A bit later in my notebook I need to make a table out of this array, but I need the first element to be empty. I have tried the following:

Tableform[Transpose[{{Append[{" "},array1]},{array2}}]]

The issue seems to be that Append creates a 2d array instead of simply adding an element to array1. I would think that some data type conversion (integer to string) or something would be necessary, but this does not seem to be something that I can see.

Sorry for being new. Thanks!

POSTED BY: Sarah Chastain
Answer
13 days ago

Sarah,

Is this what you are trying to achieve?

array1 = Insert[array1, "", 1]

{, 1, 2, 3, 4, 5, 6, 7, 8, 
POSTED BY: Paul Cleary
Answer
13 days ago

Paul, This is exactly what I needed. I'll have to remember the Insert function. Thanks!

POSTED BY: Sarah Chastain
Answer
12 days ago

Append takes the list (or expression) as its first argument, and the thing to append as the second.

So when you showed Append[{" "},array1], I wonder if you meant Append[array1, " "].

Also, what format of table were you hoping to get? That wasn't clear from your original post, especially since you're starting from a 1-dimension list. I just wondered if you meant a a 2-row table where the first row is blank, and the second row is the contents of array1, or if you wanted to split up array1 into into 2 rows with 5 columns, or just what you wanted.

One way to ask this is to hard-code the final output that I wanted, and then ask what code could take me from the input to that output.

POSTED BY: Joel Klein
Answer
13 days ago

It looks like I misunderstood the Append command. I should have been using Insert like the above.

POSTED BY: Sarah Chastain
Answer
12 days ago

Group Abstract Group Abstract