Perl vs Python vs Ruby.... vs Tcl? - TCL

This is a discussion on Perl vs Python vs Ruby.... vs Tcl? - TCL ; Hello. I found this page interesting http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/ Would someone care to show me how this would be done in Tcl please? Thanks....

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Perl vs Python vs Ruby.... vs Tcl?

  1. Default Perl vs Python vs Ruby.... vs Tcl?


    Hello. I found this page interesting

    http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/

    Would someone care to show me how this would be done in Tcl please?
    Thanks.


  2. Default Re: Perl vs Python vs Ruby.... vs Tcl?


    fro... wrote:
    > Hello. I found this page interesting
    >
    > http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/
    >
    > Would someone care to show me how this would be done in Tcl please?
    > Thanks.


    Probably by using the tcllib csv module and adding some lsearch/lsort -
    index trickery for Tcl 8.5 or simple iteration with foreach.

    Michael


  3. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    On 11 sep, 16:31, schl...@uni-oldenburg.de wrote:
    > fro... wrote:
    > > Hello. I found this page interesting

    >
    > >http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/

    >
    > > Would someone care to show me how this would be done in Tcl please?
    > > Thanks.

    >
    > Probably by using the tcllib csv module and adding some lsearch/lsort -
    > index trickery for Tcl 8.5 or simple iteration with foreach.
    >
    > Michael


    I do not think the CSV module is needed: from the description
    it does not look like there will be embedded tabs or quotation
    marks. So:

    set fields [split [string map {\" " "} $line] \t]

    ought to take care of the parsing bit. The rest is
    easy as well, I think.

    Regards,

    Arjen


  4. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    In article <1189518704.418087.48480@50g2000hsm.googlegroups.com>,
    <frolib> wrote:
    >
    >Hello. I found this page interesting
    >
    >http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/
    >
    >Would someone care to show me how this would be done in Tcl please?
    >Thanks.
    >


    Yes.

    'Might be a bit of delay, though ...

    Incidentally, not all his Python, Ruby, and Perl are current;
    modern Pythoneers would be likelier to use (another) list
    comprehension than a filter()ed lambda. Also, I'm uneasy with
    his occasional conflation of list and array ...

  5. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    frolib wrote:
    > Hello. I found this page interesting
    >
    > http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/
    >
    > Would someone care to show me how this would be done in Tcl please?
    > Thanks.


    It's pretty straight-forward. Here's a version that is roughly
    equivalent to the other languages:

    set records[list]
    while {[gets stdin line] >= 0} {
    lappend records [map {trim \"} [split $line \t]]
    }

    set EMAIL 17
    set CONTACTME 27
    set SKUTITLE 34

    set contactRecords[list]
    foreach r $records {
    lappend contactRecords [lselect $r $SKUTITLE $CONTACTME $EMAIL]
    }

    set contactRecords [lsort -index 0 $contactRecords]

    foreach r $contactRecords {
    if {[lindex $r 1] == 1} {
    puts [join $r \t]
    }
    }

    In order to make this work, you will need the following helper
    procedures. Tcl has a more minimal "standard" library than many other
    languages (keeps it nice and small), but you can find equivalents to
    these commands in the libraries shipped with most distributions (e.g.,
    tcllib contains "map"):

    # Just reverse the arguments to [string trim]
    proc trim {chars str} { string trim $str $chars }
    # Classic map function
    proc map {f xs} {
    set r[list]
    foreach x $xs { lappend r [invoke $f $x] }
    return $r
    }
    proc invoke {f x} { uplevel #0 $f $x }
    # Get multiple indices from a list at once
    proc lselect {xs args} {
    set ret[list]
    foreach idx $args { lappend ret [lindex $xs $idx] }
    return $ret
    }

    Really, though, I'd say put all this data into a relational database. A
    single SQL query will be much cleaner and more efficient than any of
    these solutions. If you don't want a full blown RDBMS then there are
    lighter-weight solutions, such as SQLite or Metakit, that work extremely
    well.

    -- Neil

  6. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    Neil Madden wrote:

    > frolib wrote:
    >> Hello. I found this page interesting
    >>
    >> http://mjtsai.com/blog/2002/11/25/pe...ython_vs_ruby/
    >>
    >> Would someone care to show me how this would be done in Tcl please?
    >> Thanks.

    >
    > It's pretty straight-forward. Here's a version that is roughly
    > equivalent to the other languages:


    [snip - nice example in straight Tcl]

    >
    > Really, though, I'd say put all this data into a relational database. A
    > single SQL query will be much cleaner and more efficient than any of
    > these solutions. If you don't want a full blown RDBMS then there are
    > lighter-weight solutions, such as SQLite or Metakit, that work extremely
    > well.
    >
    > -- Neil


    In the realm of killing gnats with a sledgehammer and if you don't mind
    bringing in a binary extension, TclRAL (http://tclral.sourceforge.net) can
    make quick work of these tabular sorts of problems.

    =========================================================================

    package require ral
    namespace import ::ral::*

    # A variable to hold the relation of input fields that we are interested in.
    # We set EMAIL as the identifier so each record will be checked for
    # unique values of the EMAIL attribute.
    relvar create records {
    Relation {
    EMAIL string
    CONTACTME boolean
    SKUTITLE string
    } {
    EMAIL
    }
    }

    # Gather up the input.
    # Here we just strip off all the quote marks and split by tabs.
    # Hard to believe this would work on "real world" input, but this
    # is just an example.
    while {[gets stdin line] >= 0} {
    set fields [split [string map {\" {}} $line] \t]
    # Insert the interesting fields into the relvar
    relvar insert records[list\
    EMAIL [lindex $fields 17]\
    CONTACTME [lindex $fields 27]\
    SKUTITLE [lindex $fields 34]\
    ]
    }

    # Uncomment this to see all the input in a nice tabular form.
    #puts [relformat $records]

    # Now for the "relational" part.
    # Find those records where we are to contact them.
    set contactrecords [relation restrictwith $records {$CONTACTME == 1}]

    # Output the fields in ascending SKUTITLE order.
    relation foreach cr $contactrecords -ascending SKUTITLE {
    relation assign $cr
    puts "$SKUTITLE\t$CONTACTME\t$EMAIL"
    }

    ===================================================================
    --
    Andrew Mangogna

  7. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    schlenk@uni-oldenburg.de wrote:
    > fro... wrote:
    >> Would someone care to show me how this would be done in Tcl please?
    >> Thanks.

    >
    > Probably by using the tcllib csv module and adding some lsearch/lsort -
    > index trickery for Tcl 8.5 or simple iteration with foreach.


    Once I stopped trying to write it like Python or other such foolishness,
    I got this:

    set EMAIL 17; set CONTACTME 27; set SKUTITLE 34
    foreach line [split [read stdin] \n] {
    set r [split [string map {\" {}} $line] \t]
    lappend contactRecords[list \
    [lindex $r $SKUTITLE] [lindex $r $CONTACTME] [lindex $r $EMAIL]]
    }
    foreach r [lsort -index 0 [lsearch -all -inline -index 1
    $contactRecords 1]] {
    puts [join $r \t]
    }

    Anyone asking why not use a DB instead, you're right! It's simpler and
    (probably) clearer...

    package require sqlite3
    sqlite3 db contacts.db
    set tab \t
    puts [join [db eval {
    SELECT SKUTITLE || $tab || CONTACTME || $tab || EMAIL
    FROM myTable
    WHERE CONTACTME = 1
    ORDER BY SKUTITLE
    }] \n]

    Donal.

  8. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    On Sep 11, 2:16 pm, "Donal K. Fellows"
    <donal.k.fell...@manchester.ac.uk> wrote:
    > Anyone asking why not use a DB instead, you're right! It's simpler and
    > (probably) clearer...


    We could just provide an example. Hey, there is a specific list of 8
    steps, nothing about csv, specific assumptions, etc. I'll post my
    script here, followed by a set of reference data (I've changed the
    indexes since they are not specified in the 8 steps.):

    This is tsv.tcl:

    #!/web/nsd45/bin/tclsh8.4
    # Do comparison Algorithm

    # 1. Read stdin, split by \t
    # 2. String trim each field of '"' (quotation marks)
    # 3. Sore these fields in list called record
    # 4. lappend 'record' to another list called records
    # 5. create new record list named contactRecords with only three
    fields:
    # SKUTITLE, CONTACTME, EMAIL
    # 6. Sort contactRecords by SKUTITLE
    # 7. Delete from contactRecords any records where CONTACTME != 1
    # 8. Print contactRecords with tabs between fields and newline between
    records

    set EMAIL 2
    set CONTACTME 0
    set SKUTITLE 1

    set records[list]
    set contactRecords[list]

    while {![eof stdin]} {
    # 1.
    set line [split [gets stdin] "\t"]
    # 2-3
    set record [string map {"\"" ""} $line]
    # 4.
    lappend records $record
    # 5.
    lappend contactRecords[list \
    [lindex $record $SKUTITLE]\
    [lindex $record $CONTACTME]\
    [lindex $record $EMAIL]];
    }

    # 6.
    set contactRecords [lsort $contactRecords]
    # 7.
    set contactRecords [lsearch -all -inline -not $contactRecords {* 1 *}]
    # 8.
    foreach contactRecord $contactRecords {
    puts stdout [join $contactRecord "\t"]
    }

    # Following is the data.tsv file (tab separated values):

    $cat data.tsv
    "1" "rz" "t@b"
    "0" "uq" "r@y"
    "1" "rn" "e@u"
    "0" "mn" "q@a"
    "0" "tt" "o@y"
    "1" "ew" "i@x"
    "0" "gh" "v@z"

    Then do this:

    $ cat data.tsv | ./tsv.tcl

    Results:
    gh 0 v@z
    mn 0 q@a
    tt 0 o@y
    uq 0 r@y

    Although you could claim to do this faster or with less code, you have
    to create the variables specified in the steps, replace the value of
    certain variables, etc. These are significant to comparing languages.
    Tcl can do each step in the order asked, creating only the variables
    needed. Very nice is the removal of quotes from list elements.


  9. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    On Sep 11, 5:19 pm, "tom.rmadilo" <tom.rmad...> wrote:
    > We could just provide an example.


    The comments on the reference page are very interesting. Most
    scripting languages support nearly impossible to understand code.

    Also, several are incorrect, as is the Donal example above. Here is
    the incorrect script in shell language (using the above data file):

    $ cat data.tsv | awk '$1 ~ /1/ { print $2 " " $1 " " $3; }' | sed
    's#"##g' | sort
    ew 1 i@x
    rn 1 e@u
    rz 1 t@b

    correct version:

    $ cat data.tsv | awk '$1 !~ /1/ { print $2 " " $1 " " $3; }' | sed
    's#"##g' | sort
    gh 0 v@z
    mn 0 q@a
    tt 0 o@y
    uq 0 r@y

    Close, but no cigar.


  10. Default Re: Perl vs Python vs Ruby.... vs Tcl?

    On Sep 12, 8:19 am, "tom.rmadilo" <tom.rmad...> wrote:
    > On Sep 11, 2:16 pm, "Donal K. Fellows"
    >
    > <donal.k.fell...@manchester.ac.uk> wrote:
    > > Anyone asking why not use a DB instead, you're right! It's simpler and
    > > (probably) clearer...

    >
    > <snip>
    >
    > $cat data.tsv
    > "1" "rz" "t@b"
    > "0" "uq" "r@y"
    > "1" "rn" "e@u"
    > "0" "mn" "q@a"
    > "0" "tt" "o@y"
    > "1" "ew" "i@x"
    > "0" "gh" "v@z"
    >
    > Then do this:
    >
    > $ cat data.tsv | ./tsv.tcl
    >
    > Results:
    > gh 0 v@z
    > mn 0 q@a
    > tt 0 o@y
    > uq 0 r@y
    >
    > Although you could claim to do this faster or with less code, you have
    > to create the variables specified in the steps, replace the value of
    > certain variables, etc. These are significant to comparing languages.
    > Tcl can do each step in the order asked, creating only the variables
    > needed. Very nice is the removal of quotes from list elements.


    I just realised something when I saw your sample data file. If I read
    the spec correctly it states that the data is tab separated and
    surrounded by quotes. Isn't this simply a bunch of lists separated by
    newlines? Let the parser do the work:

    set EMAIL 2
    set CONTACTME 0
    set SKUTITLE 1

    while {[gets stdin line] >= 0} {
    if {[lindex $line $CONTACTME] == 1} {
    lappend contactRecords[list \
    [lindex $line $SKUTITLE] \
    [lindex $line $CONTACTME] \
    [lindex $line $EMAIL]
    ]
    }
    }

    foreach x [lsort contactRecords] {
    puts [join $x "\t"]
    }

    Ok, yeah I know, never treat unknown strings as lits. But the spec
    basically defines the input data to be proper tcl lists (if I
    understand it correctly).


+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. PERL vs. Ruby
    By Application Development in forum Perl
    Replies: 4
    Last Post: 04-25-2008, 01:17 AM
  2. OLE Error: only arrays with dimension 1supported using Ruby/Python/Perl
    By Application Development in forum Adobe illustrator
    Replies: 0
    Last Post: 07-09-2007, 07:37 PM
  3. Re: OpenGL Benchmarks: C vs Perl, Perl vs Python
    By Application Development in forum Graphics
    Replies: 2
    Last Post: 05-25-2007, 05:42 AM
  4. OpenGL Benchmarks: C vs Perl, Perl vs Python
    By Application Development in forum Graphics
    Replies: 0
    Last Post: 05-23-2007, 04:34 AM
  5. Sydney Python Meetup July 21: Python, Perl, and Cold Beverages
    By Application Development in forum Python
    Replies: 0
    Last Post: 07-18-2005, 01:03 AM