Ruby에서 1-D Array를 2-D Array로 전환 할려면.

yuni의 이미지

루비 초보자입니다(실은 프로그랭 전반에 걸쳐서
...).
처음 루비책을 보았을때는 조금만 배우면 이걸로 못하는 것이 없겠다 싶어서(구한 책에 Tk, CGI 까지 소개를 했더군요.) 시작을 했는데, 친숙하지 않아서 그런지 상당히 괴롭네요.

루비에서 이런 경우는 어떻게 하나요?

[yuni@tp PointSES]$ cat tmp.inp 
-278.9000       0.0000  -0.6400 
-278.9000       1.3820  0.0000 
-271.2000       0.0000  -0.6400 
-271.2000       1.3820  0.0000 
-263.5000       0.0000  -15.1200 

이런 자료를읽어서 배열로 하고 싶습니다.

[yuni@tp PointSES]$ cat arr.rb
#!/usr/bin/ruby
oriData = IO.readlines("tmp.inp")
oriData.length.times do |i|
   puts oriData[i] 
end
[yuni@tp PointSES]$ ./arr
-278.9000       0.0000  -0.6400 
-278.9000       1.3820  0.0000 
-271.2000       0.0000  -0.6400 
-271.2000       1.3820  0.0000 
-263.5000       0.0000  -15.1200 

보시는것 처럼 oriData[0] 에는 -278.9000 0.0000 -0.6400 이렇게문자열이 들어 가 있습니다. 이것을 data[0][0] = -278.9000 data[0][1] = 0.0000, data[0][2] = -0.6400 이런식의 2차원 배열로 처음 부터 불러오는 수는없을까요? 아니면 다른 방법이라도...

그리고 루비관련 자료도 한번 부탁 드려봅니다.
지금 제가 보고 있는 책은 Making use of Ruby( Suresh Mahadevan 저, 2002년) 인데, 주소록 설명만 열심히 하고 있답니다. 온라인에서도 이자료 저자료 뒤져 보고는 있는데 좀 갑갑하네요. :oops:

7339989b62a014c4ce6e31b3540bc7b5f06455024f22753f6235c935e8e5의 이미지

제가 멋대로 고쳐보았습니다. :)

lines = IO.readlines("tmp.inp")
data = []
lines.each do |line|
  data << line.split
end 

파일을 읽어와서 줄을 나눈 뒤 data 배열에 추가하는 코드입니다. 원래 코드와 다른 점은 each를 사용하였다는 것입니다. :)

그리고 http://ruby-doc.org/ 를 이용하시면 훨씬 수월하실 겁니다.

yuni의 이미지

이렇게 빨리 많은 것을 알게 해 주셔서 감사합니다.
저의 코드와는 비교가 안될 만큼 직관적이고 명확합니다.

==========================
부양가족은 많은데, 시절은 왜 이리 꿀꿀할까요?
=====================
"지금하는 일을 꼭 완수하자."

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.