Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I write this Perl statement in Tcl?

6 views
Skip to first unread message

Torsten Reincke

unread,
Mar 11, 2003, 5:22:14 AM3/11/03
to
I have a one-liner of Perl code here and want to translate that to
Tcl.

$rc = unpack('d',pack('CCCCCCCC',@array));

@array is an array with 8 numbers, for example:

"119 190 159 26 239 220 145 64"

$rc would be 1143.2335 in this case. I started by making two
statements out of the original Perl code:

$rc1 = pack('CCCCCCCC',@array);
$rc = unpack('d',$rc1);

The first line translates to

foreach item $array { append rc1 [binary format c $item] }

but this does not give the same result in Tcl and Perl. I'm stuck. Can
someone give me a hint?

Thank you, Torsten

steve_h

unread,
Mar 11, 2003, 7:02:23 AM3/11/03
to

This worked for me:


set l [list 119 190 159 26 239 220 145 64]
set rc [eval binary format cccccccc [join $l]]
binary scan $rc d result

puts $result

Arjen Markus

unread,
Mar 11, 2003, 7:00:54 AM3/11/03
to

This will depend on the endianness of your platform, but try:

set packed [binary format [string repeat c 8] 119 190 159 26 239 220 145
64]
binary scan $packed d dbl
puts $dbl

Regards,

Arjen

Torsten Reincke

unread,
Mar 11, 2003, 10:04:38 AM3/11/03
to
steve_h <steve....@bigfoot.co.m> wrote in message
> This worked for me:
>
>
> set l [list 119 190 159 26 239 220 145 64]
> set rc [eval binary format cccccccc [join $l]]
> binary scan $rc d result
>
> puts $result

Great! Thank you very much!

Torsten

lvi...@yahoo.com

unread,
Mar 11, 2003, 10:19:00 AM3/11/03
to

According to steve_h <steve....@bigfoot.co.m>:
:Torsten Reincke wrote:
:> $rc = unpack('d',pack('CCCCCCCC',@array));

:>
:> @array is an array with 8 numbers, for example:
:>
:> "119 190 159 26 239 220 145 64"
:>
:> $rc would be 1143.2335 in this case.

:This worked for me:


:
:set l [list 119 190 159 26 239 220 145 64]
:set rc [eval binary format cccccccc [join $l]]
:binary scan $rc d result
:
:puts $result

When I run these tcl lines in Tcl 8.4.2, I get
6.31920840155e+268

which doesn't seem like it would be equal to 1143.2335 .

--
Join us at the Tenth Annual Tcl/Tk Conference <URL: http://mini.net/tcl/6274 >
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvi...@yahoo.com > <URL: http://www.purl.org/NET/lvirden/ >

Glenn Jackman

unread,
Mar 11, 2003, 10:50:35 AM3/11/03
to
lvi...@yahoo.com <lvi...@yahoo.com> wrote:
>
> According to steve_h <steve....@bigfoot.co.m>:
> :Torsten Reincke wrote:
> :> $rc = unpack('d',pack('CCCCCCCC',@array));
> :>
> :> @array is an array with 8 numbers, for example:
> :>
> :> "119 190 159 26 239 220 145 64"
> :>
> :> $rc would be 1143.2335 in this case.
>
> :This worked for me:
> :
> :set l [list 119 190 159 26 239 220 145 64]
> :set rc [eval binary format cccccccc [join $l]]
> :binary scan $rc d result
> :
> :puts $result
>
> When I run these tcl lines in Tcl 8.4.2, I get
> 6.31920840155e+268
>
> which doesn't seem like it would be equal to 1143.2335 .

I guess it depends on the endedness of the platform. I get the same
result on solaris, but reversing the list:

proc lreverse {list} {
set tsil [list]
foreach element $list {
set tsil [linsert $tsil 0 $element]
}
return $tsil
}

set l [lreverse [list 119 190 159 26 239 220 145 64]]
set rc [eval binary format cccccccc $l]


binary scan $rc d result
puts $result

gives the expected result.


I suppose with the recent K discussion:
proc K {x y} {set x}
proc lreverse {list} {
set tsil {}
foreach element $list {
set tsil [linsert [K $tsil [set tsil {}]] 0 $element]
}
return $tsil
}

(and I note the inconsistent use of explicit/implicit returns... What a topical
posting ;)

--
Glenn Jackman
NCF Sysadmin
gle...@ncf.ca

Arjen Markus

unread,
Mar 12, 2003, 2:24:18 AM3/12/03
to
lvi...@yahoo.com wrote:
>
> According to steve_h <steve....@bigfoot.co.m>:
> :Torsten Reincke wrote:
> :> $rc = unpack('d',pack('CCCCCCCC',@array));
> :>
> :> @array is an array with 8 numbers, for example:
> :>
> :> "119 190 159 26 239 220 145 64"
> :>
> :> $rc would be 1143.2335 in this case.
>
> :This worked for me:
> :
> :set l [list 119 190 159 26 239 220 145 64]
> :set rc [eval binary format cccccccc [join $l]]
> :binary scan $rc d result
> :
> :puts $result
>
> When I run these tcl lines in Tcl 8.4.2, I get
> 6.31920840155e+268
>
> which doesn't seem like it would be equal to 1143.2335 .
>

Then you are using a machine with BigEndian instead of
LittleEndian.

Remind me to write a TIP on this limitation of the [binary]
command.

Regards,

Arjen

Torsten Reincke

unread,
Mar 12, 2003, 3:45:46 AM3/12/03
to
Arjen Markus <arjen....@wldelft.nl> wrote in message
> This will depend on the endianness of your platform, but try:
>
> set packed [binary format [string repeat c 8] 119 190 159 26 239 220 145
> 64]
> binary scan $packed d dbl
> puts $dbl

Ah, this is quite equivalent to the method proposed by Steve Howarth.
But hey, this is cool, because the endianness dependance wasn't clear
to me. One line up in the Perl I'm translating, it does just what
Glenn Jackman mentioned: it reverses the given list (claiming to be on
an x86 platform). So now, I can even translate the code AND be sure it
is independant of the used platform.

Hmm, perhaps another question. What I'm really trying to do is
extracting data from a Paradox database file. So I searched the net
for any knowledge about the file format and found a Perl module and a
descriptive text (that's where the code above comes from). Since all
this information is inofficial and the file format is not published by
Borland/Corel, I wonder whether it would be legal to build a Tcl
package and give it to the community. In other words: is
re-engineering allowed?

Torsten

Cameron Laird

unread,
Mar 12, 2003, 8:03:25 AM3/12/03
to
In article <6fe1d40.03031...@posting.google.com>,
Torsten Reincke <rei...@typoscriptics.de> wrote:
.
.

.
>Hmm, perhaps another question. What I'm really trying to do is
>extracting data from a Paradox database file. So I searched the net
>for any knowledge about the file format and found a Perl module and a
>descriptive text (that's where the code above comes from). Since all
>this information is inofficial and the file format is not published by
>Borland/Corel, I wonder whether it would be legal to build a Tcl
>package and give it to the community. In other words: is
>re-engineering allowed?
>
>Torsten

Some people are using TclODBC with Paradox.

No regulars here are lawyers. The little I understand
about intellectual property law includes that it apparently
could be a full-time job just researching the question of
how legal it is to reverse engineer various constructs.

At an informal level, I've seen secondary sources that
claim rights to reverse engineer are recognized in the
laws of some European countries, I can attest that a LOT
of it goes on, and that, if I were working currently with
Paradox, the risk of legal action by Borland would be low
on my personal list of worries.

Remember, though: in the US, innocence is no particular
defense against indictment.
--

Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html

lvi...@yahoo.com

unread,
Mar 12, 2003, 12:19:20 PM3/12/03
to

According to Torsten Reincke <rei...@typoscriptics.de>:
: So I searched the net
:[...] and found a Perl module and a

:descriptive text (that's where the code above comes from). Since all
:this information is inofficial and the file format is not published by
:Borland/Corel, I wonder whether it would be legal to build a Tcl
:package and give it to the community. In other words: is
:re-engineering allowed?

It depends on the license of the Perl module. If the Perl module does
not prevent you from reengineering the Perl module, then I think you
can reengineer it safely.

On the other hand, if the Perl module author broke the law in his creating
of the module, then you would be continuing that action by re-re-engineering
it...

Ken Jones

unread,
Mar 12, 2003, 1:19:57 PM3/12/03
to
"Glenn Jackman" <xx...@freenet.carleton.ca> wrote in message
news:slrnb6s1eb...@freenet9.carleton.ca...

>
> I guess it depends on the endedness of the platform. I get the same
> result on solaris, but reversing the list:
>
> proc lreverse {list} {
> set tsil [list]
> foreach element $list {
> set tsil [linsert $tsil 0 $element]
> }
> return $tsil
> }
>
> set l [lreverse [list 119 190 159 26 239 220 145 64]]
> set rc [eval binary format cccccccc $l]
> binary scan $rc d result
> puts $result
>
> gives the expected result.
>
> I suppose with the recent K discussion:
> proc K {x y} {set x}
> proc lreverse {list} {
> set tsil {}
> foreach element $list {
> set tsil [linsert [K $tsil [set tsil {}]] 0 $element]
> }
> return $tsil
> }

Your [lreverse] procedure is better implemented as:

proc lreverse { list } {
set newlist {}
set i [llength $list]
for { incr i -1 } { $i >= 0 } { incr i -1 } {
lappend newlist [lindex $list $i]
}
return $newlist
}

Trust me on this. I use this very example in my classes. :-)

- Ken Jones, President
Avia Training and Consulting
www.avia-training.com
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax


Glenn Jackman

unread,
Mar 12, 2003, 1:40:37 PM3/12/03
to
Ken Jones <k...@avia-training.com> wrote:
> "Glenn Jackman" <xx...@freenet.carleton.ca> wrote in message
> news:slrnb6s1eb...@freenet9.carleton.ca...
> > proc lreverse {list} {
> > set newlist [list]
> > foreach element $list {
> > set newlist [linsert $newlist 0 $element]
> > }
> > return $newlist

> > }
>
> Your [lreverse] procedure is better implemented as:
>
> proc lreverse { list } {
> set newlist {}
> set i [llength $list]
> for { incr i -1 } { $i >= 0 } { incr i -1 } {
> lappend newlist [lindex $list $i]
> }
> return $newlist
> }
>
> Trust me on this. I use this very example in my classes. :-)

Not that I don't trust you implicitly, but... ;)
What's the difference? (apart from style)

Tom Wilkason

unread,
Mar 12, 2003, 2:26:03 PM3/12/03
to
"Glenn Jackman" <xx...@freenet.carleton.ca> wrote in message
news:slrnb6uvp5...@freenet9.carleton.ca...

Speed, the 'set newlist [linsert $newlist 0 $element]' method requires an
internal copy of the built up list with each iteration (and rebuffering as
the list grows), whereas the lappend does not. You should see at least a
300X speedup using the latter method.

Tom Wilkason


Michael Schlenker

unread,
Mar 12, 2003, 2:35:35 PM3/12/03
to
If your shooting it out try this attempt from the wiki:
http://mini.net/tcl/43

proc lreverse L {
set res {}
set i [llength $L]
while {$i} {lappend res [lindex $L [incr i -1]]} ;# rmax
set res
} ;# RS, tuned 10% faster by [rmax]


Michael Schlenker

Tom Wilkason

unread,
Mar 12, 2003, 2:33:55 PM3/12/03
to

"Tom Wilkason" <tom.wi...@cox.net> wrote in message
news:gRLba.136$35.614@dfw-

> You should see at least a 300X speedup using the latter method.
That is, for long lists such as 10000 elements.

YMMV

Tom Wilkason


0 new messages