Copy/Pasting a Grid of Numbers from Project Euler

On Project Euler Problem 11 (link: https://projecteuler.net/problem=11), there is a 20x20 grid of numbers. However, when I pasted it into Mathematica, I got this strange result where the times symbol 'x' is interlaced between the numbers.

Here is an image that shows this (again, this grid is directly copy/pasted from the website): http://i.gyazo.com/6336b0c44a1cb6afef7ee3f962c06522.png

Are there any in-line commands/functions you can use on that mass of numbers so that you can enter everything once and it will return a nicely formatted list/matrix? The only way I could think of, which did not make use of in-line commands, was very tedious: Ctrl+F --> Replace all "x" with "," and then manually entering curly brackets to turn the grid into a list/matrix.

Ideally, I want to turn it into something like {{1,2,3},{4,5,6},{7,8,9}}, but of course with the appropriate elements.

Edit: From this StackExchange post (Cut and paste data from a spreadsheet), I found that

data = ImportString["(copy/paste grid here)", "TSV"]


works exactly as desired :)

Edit 2: Just kidding, the above doesn't work. It doesn't add commas.

3 Answers

Here is one way to import the grid into Mathematica

Partition[
StringCases[
Import["http://projecteuler.net/problem=11", "Plaintext"],
DigitCharacter ..][[10 ;; -8]],
20] // ToExpression

• Thank you for this :) Can you explain what the ". . " are for? I looked in the documentation for DigitCharacter, and that seems to be paired with the function, kind of like how pure functions involve the pairing of # and &. Jun 29, 2015 at 3:35
• @AisforAmbition It is a String Pattern. DigitCharacter .. means one or more digits. Jun 29, 2015 at 3:39
• A problem I ran into was that since this treats every digit as a string and not a standard number, the inside elements cannot be operated upon by the Times function. What can I do if I want to multiply the matrix's first diagonal, for example? Jun 29, 2015 at 3:46
• Ah, thank you very much :) Jun 29, 2015 at 3:50
• @Karsten7., output is the same w/o "Plaintext". Jun 29, 2015 at 17:38

Szabolcs's Past Tabular Data palette still works just like it should. This palette is so useful it is one of only two I always open on start-up.

If for some reason you are looking for a one-off solution here is mine:

Block[{Times = List}, (*paste here*) ] ~Partition~ 20


Reference: Convert head Times to List

• Very elegant method, Mr. Wizard. Once again, you've been a tremendous help :) Thank you! Jun 29, 2015 at 9:05
• Nice. With and Module also work. Jun 29, 2015 at 17:43

Highlight the list and paste into a text file and then

t=Import["put path of text file in here","Table"]
t//MatrixForm


works for me. Does it work for anyone else?

• No, I get HTML, eventually containing ... {32, 98, 81, 28, 64, 23, 67, 10, "<span", \ "style=\"color:#ff0000;\"><b>26</b></span>", 38, 40, ... Jun 29, 2015 at 17:50
• It certainly ought to. This is the basic method without direct copy-and-paste, but it's the one I used for years before the introduction of ImportString. Jun 29, 2015 at 17:51
• @alancalvitti Is your browser really copying HTML code? Which do you use? Jun 29, 2015 at 17:51
• @Mr.Wizard Possible confusion: bobbym wrote "put path..." so I used Import["http://projecteuler.net/problem=11","Table"] Jun 29, 2015 at 17:52
• @alancalvitti but first Highlight the list and paste into a text file and then Jun 29, 2015 at 17:58