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

Discussion in 'Ruby' started by RLMuller, Sep 14, 2003.

  1. RLMuller

    RLMuller Guest

    This is a multi-part message in MIME format.

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

    Hi All,

    "Programming Ruby" says the following for String#scan at =
    http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#Strin=
    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;
    charset="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; =
    charset=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">http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_s=
    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:eek:ffice:eek:ffice"=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, Sep 14, 2003
    #1
    1. Advertising

  2. RLMuller

    Hal Fulton Guest

    RLMuller wrote:
    > Hi All,
    >
    > "Programming Ruby" says the following for String#scan at
    > http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.scan:
    >
    >
    > |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, Sep 14, 2003
    #2
    1. Advertising

  3. RLMuller

    Richard Guest

    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, Sep 14, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vengala s reddy
    Replies:
    0
    Views:
    1,925
    vengala s reddy
    Mar 5, 2004
  2. John Timney \(ASP.NET MVP\)

    Re: Expression does not produce a value: Session error?

    John Timney \(ASP.NET MVP\), Sep 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    611
    John Timney \(ASP.NET MVP\)
    Sep 20, 2005
  3. Replies:
    5
    Views:
    462
    Luc The Perverse
    Oct 27, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
  5. Alex Allmont
    Replies:
    1
    Views:
    120
    Brian Candler
    Jul 30, 2009
Loading...

Share This Page