Professional Web Applications Themes

Does String#scan(/(..)(..)/) produce an array of arrays? - Ruby

This is a multi-part message in MIME format. ------=_NextPart_000_002A_01C37A79.49BBECF0 Content-Type: text/plain; cht="Windows-1252" Content-Transfer-Encoding: quoted-printable Hi All, "Programming Ruby" says the following for String#scan at = [url]http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#Strin=[/url] g.scan: a =3D "cruel world"=20 ... a.scan(/(..)(..)/) =BB [["cr", "ue"], ["l ", "wo"]] I wrote the following to test whether scan actually produced an array or = arrays: a =3D "cruel world"=20 ar =3D a.scan(/(..)(..)/) puts "Type of ar =3D %s of size %d" % [ar.class, ar.size] ix=3D0 ar.each {|x| puts "Type of ar[#{ix}] is %s" % [ix, ar[ix].class]; = ix+=3D1} That resulted in: Type of ar =3D Array of size 2 Type of ...

  1. #1

    Default Does String#scan(/(..)(..)/) produce an array of arrays?

    This is a multi-part message in MIME format.

    ------=_NextPart_000_002A_01C37A79.49BBECF0
    Content-Type: text/plain;
    cht="Windows-1252"
    Content-Transfer-Encoding: quoted-printable

    Hi All,

    "Programming Ruby" says the following for String#scan at =
    [url]http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#Strin=[/url]
    g.scan:

    a =3D "cruel world"=20

    ...

    a.scan(/(..)(..)/) =BB [["cr", "ue"], ["l ", "wo"]]



    I wrote the following to test whether scan actually produced an array or =
    arrays:



    a =3D "cruel world"=20
    ar =3D a.scan(/(..)(..)/)
    puts "Type of ar =3D %s of size %d" % [ar.class, ar.size]
    ix=3D0
    ar.each {|x| puts "Type of ar[#{ix}] is %s" % [ix, ar[ix].class]; =
    ix+=3D1}


    That resulted in:



    Type of ar =3D Array of size 2
    Type of ar[0] is 0
    Type of ar[1] is 1



    So it seem like we don't have an array of arrays. So what do we have? =
    Or am I all wet?



    Regards,
    Richard

    A programmer is a device for turning coffee into code.
    Jeff Prosise (with an assist from Paul Erdos)

    ------=_NextPart_000_002A_01C37A79.49BBECF0
    Content-Type: text/html;
    cht="Windows-1252"
    Content-Transfer-Encoding: quoted-printable

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=3DContent-Type content=3D"text/html; =
    cht=3Dwindows-1252">
    <META content=3D"MSHTML 6.00.2800.1226" name=3DGENERATOR>
    <STYLE></STYLE>
    </HEAD>
    <BODY bgColor=3D#ffffff>
    <DIV><FONT size=3D2>Hi All,</FONT></DIV>
    <DIV><FONT size=3D2></FONT>&nbsp;</DIV>
    <DIV><FONT size=3D2>"Programming Ruby" says the following for =
    String#scan at <A=20
    href=3D"http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.ht=
    ml#String.scan">[url]http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_s=[/url]
    tring.html#String.scan</A>:</FONT></DIV>
    <DIV><FONT size=3D2></FONT>&nbsp;</DIV>
    <DIV>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'">a&nbsp;=3D&nbsp;"cruel&nbsp;world"=20
    <?xml:namespace prefix =3D o ns =3D =
    "urn:schemas-microsoft-com:office:office"=20
    /><o:p></o:p></SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'">...<o:p></o:p></SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">a.scan(/(..)(..)/) =

    </SPAN></CODE><FONT face=3D"Times New Roman">=BB&nbsp; =
    </FONT><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'">[["cr",&nbsp;"ue"],&nbsp;["l&nbsp;",&nbsp;"wo"]]</SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'"></SPAN></CODE>&nbsp;</P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">I wrote the =
    following to=20
    test whether scan actually produced an array or =
    arrays:</SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'"></SPAN></CODE>&nbsp;</P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">a =3D "cruel =
    world" <BR>ar =3D=20
    a.scan(/(..)(..)/)<BR>puts "Type of ar =3D %s of size %d" % [ar.class,=20
    ar.size]<BR>ix=3D0<BR>ar.each {|x| puts "Type of ar[#{ix}] is %s" % [ix, =

    ar[ix].class]; ix+=3D1}<BR></SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">That resulted=20
    in:</SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'"></SPAN></CODE>&nbsp;</P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><FONT =
    face=3DArial>Type of ar=20
    =3D Array of size 2<BR>Type of ar[0] is 0<BR>Type of ar[1] is=20
    1</FONT></P></SPAN></CODE>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'"></SPAN></CODE>&nbsp;</P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">So it seem like we =
    don't=20
    have an array of arrays.&nbsp; So what do we have? Or am I all=20
    wet?</SPAN></CODE></P>
    <P class=3DMsoNormal style=3D"MARGIN: 0in 0in 0pt"><CODE><SPAN=20
    style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier =
    New'"></SPAN></CODE>&nbsp;</P></DIV>
    <DIV><FONT size=3D2>Regards,<BR>Richard</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT size=3D2>A programmer is a device for turning coffee into =
    code.<BR>Jeff=20
    Prosise (with an assist from Paul Erdos)<BR></FONT></DIV></BODY></HTML>

    ------=_NextPart_000_002A_01C37A79.49BBECF0--


    RLMuller Guest

  2. #2

    Default Re: Does String#scan(/(..)(..)/) produce an array of arrays?

    RLMuller wrote:
    > Hi All,
    >
    > "Programming Ruby" says the following for String#scan at
    > [url]http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.scan:[/url]
    >
    >
    > |a = "cruel world" |
    >
    > |...|
    >
    > |a.scan(/(..)(..)/) | |[["cr", "ue"], ["l ", "wo"]]|
    >
    > ||
    >
    > |I wrote the following to test whether scan actually produced an array
    > or arrays:|
    >
    > ||
    >
    > |a = "cruel world"
    > ar = a.scan(/(..)(..)/)
    > puts "Type of ar = %s of size %d" % [ar.class, ar.size]
    > ix=0
    > ar.each {|x| puts "Type of ar[#{ix}] is %s" % [ix, ar[ix].class]; ix+=1}
    > |
    >
    > |That resulted in:|
    >
    > ||
    >
    > |Type of ar = Array of size 2
    > Type of ar[0] is 0
    > Type of ar[1] is 1|
    >
    > ||
    >
    > |So it seem like we don't have an array of arrays. So what do we have?
    > Or am I all wet?|
    You're all wet. ;)

    Seriously, you've just made a typo or two. Your last format string does
    an interpolation instead of using a format specifier. You're printing
    the value of ix as the class.

    I suggest each_with_index and consistent formatting, like

    ar.each_with_index {|x,ix| puts "Type of ar[#{ix}] is #{ar[ix].class}" }

    If you don't like variable interpolation, you can use a real printf:

    printf "Type of ar[%d] is %s\n",ix,ar[ix].class

    Anyhow, you do get real live arrays here, just as the Book says.

    Hal


    Hal Fulton Guest

  3. #3

    Default Re: Does String#scan(/(..)(..)/) produce an array of arrays?

    Many thanks, Hal,

    As I told you before, I've got your book, but digesting everything in
    a book of 579+ pages takes time, as does finding the answer to a
    specific question, so thanks for hanging out on this newsgroup.

    And thanks for 'drying me out'; I certainly was 'all wet' in my
    amateurish mistake. Better still, I'm thankful for your truly
    Rubyesque improvement. I learned one more thing, thereby.

    Regards,
    Richard
    Richard Guest

Similar Threads

  1. array of arrays
    By Christiane Nerz in forum PERL Beginners
    Replies: 5
    Last Post: October 22nd, 06:26 PM
  2. Antwort: array of arrays
    By Manfred Beilfuss in forum PERL Beginners
    Replies: 0
    Last Post: October 20th, 10:54 AM
  3. Replies: 2
    Last Post: August 16th, 10:26 AM
  4. += vs << when appending arrays to an array
    By Doug Beaver in forum Ruby
    Replies: 1
    Last Post: July 16th, 08:57 AM
  5. can I use arrays in forms? with a string index?
    By lawrence in forum PHP Development
    Replies: 5
    Last Post: July 13th, 06:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139