[26635] in Perl-Users-Digest
Perl-Users Digest, Issue: 8743 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 7 18:05:33 2005
Date: Wed, 7 Dec 2005 15:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 7 Dec 2005 Volume: 10 Number: 8743
Today's topics:
Copy characterdata from XML file to XML file <eric@NOgildewg.SPAMxs4all.nl>
Re: javascript to perl converter? <emschwar@pobox.com>
Re: javascript to perl converter? <emschwar@pobox.com>
Re: javascript to perl converter? <no-spam@new.rr.com>
Re: javascript to perl converter? <no-spam@new.rr.com>
perl 5.6.1 and Encode.pm <p_geni...@yahoo.fr>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 07 Dec 2005 21:04:32 +0100
From: Eric van Oorschot <eric@NOgildewg.SPAMxs4all.nl>
Subject: Copy characterdata from XML file to XML file
Message-Id: <pan.2005.12.07.20.04.29.420519@NOgildewg.SPAMxs4all.nl>
Hi,
I'm writing a Perl script that has to copy a block of data (nodes numbers
and coordinates) from one XML formatted file into another XML file.
I'm using XML::Parser to extract the data and XML::Writer to write the
data into the second file.
This does not work, since some of the numbers are corrupted after being
read by XML::Parser. Below I have copied a small bit that shows how the
data is corrupted. It always happens at the same line(s) of data.
67 2.9005093479606E+000 3.6637104002418E-001 7.9522656092442E-001
68 2.8852994122583E+000 3.5353599488296E-001 7.7516591265738E-001
69 2.9109259023248E+000 3.5272037818926E-001 8.1765470045
602E-001
70 2.9014248453522E+000 3.4032368974452E-001 7.9417266267164E-001
71 2.8849923984542E+000 3.2706829720117E-001 7.7537618002780E-001
My Perl script (I am not an experienced Perl programmer) is shown below.
The error occurs in the sub 'ReadCharacterData'. In this subroutine the
data is read and copied into a hash %tables. When writing this hash in the
output file the error shown above is found.
If anyone has an idea, or needs more info, please reply.
Regards,
Eric
use XML::Parser;
use IO::File;
use Switch ;
use XML::Writer;
my $fmsfile = shift ; # fms output file
my $reffile = shift ; # Exchange output deck
my $outfile = shift ; # Output file
die "Cannot find fms output file \"$xmlfile\""
unless -f $fmsfile;
die "Cannot find xml input deck \"$reffile\""
unless -f $reffile;
my $output = new IO::File(">$outfile");
my $writer = new XML::Writer( OUTPUT => $output, UNSAFE => 1 );
#
# Find tmax in fms file
#
my $tmax = 0.00 ;
open ( IN, $fmsfile ) ;
while ( <IN> ) {
if ( /TIME/ ) {
( $dum, $dum, $dum, $ti ) = split /\s+/ ;
$tmax = $ti if ( $ti > $tmax ) ;
}
}
close (IN) ;
$tag = "";
my %tables ; # hash with coordinates from fms file
my $model ; # naam van het FE model
my $i = 0 ; #
# Readfile to create hash of the coordinate tables
#
my $parser = new XML::Parser;
$parser->setHandlers( Char => \&ReadCharacterData,
Default => \&default);
print "Reading fms file ($fmsfile)\n" ; $parser->parsefile($fmsfile);
## Check info read in fms file
#foreach $i ( keys %tables ) {
# print "Table $i\n",$tables{$i},"\n End table $i\n\n";
# }
my $coords = 0 ;
#
# Read reffile and replace coordinate tables with data from fms file
#
my $bparser = new XML::Parser;
$bparser->setHandlers( XMLDecl => \&XmlDecl,
Doctype => \&DocType,
Start => \&startElement,
End => \&endElement,
Char => \&characterData,
CdataStart => \&cdatastart,
CdataEnd => \&cdataend,
Default => \&default);
print "Reading ($reffile) and writing ($outfile) \n" ;
$bparser->parsefile($reffile);
$writer->end() ;
#
########################################################################
#
sub XmlDecl {
my( $parseinst, $version, $encoding, $standalone ) = @_;
$writer->xmlDecl( $encoding, $standalone );
}
sub DocType {
my( $parseinst, $name, $sysid, $pub, $internal ) = @_;
$writer->doctype( $name, $pub, $sysid );
}
sub startElement {
# Reading xml data
my( $parseinst, $element, %attrs ) = @_;
SWITCH: {
if ($element eq "FE_MODEL") {
$model = $attrs{'NAME'} ;
$tag = "DEFINE";
# print "FE model $model\n" ;
last SWITCH;
}
if ($element eq "TABLE" && $attrs{'TYPE'} =~ /COORDINATE/ ) {
$coords = 1 ;
# print "$coords - TABLE COORDINATES\n" ;
}
last SWITCH ;
}
$writer -> startTag( $element , %attrs );
}
sub endElement {
my( $parseinst, $element ) = @_;
$coords = 0 ;
$writer -> endTag( $element ) ;
}
sub ReadCharacterData {
my( $parseinst, $data ) = @_;
SWITCH: {
if ( $data =~ /^\s*$/ ) {
last ;
};
if ( $data =~ /TIME/ ) {
( $dum, $dum, $dum, $ti ) = split /\s+/, $data ;
# print "Timepoint ", $ti, "\n" ;
last ;
} ;
if ( $data =~ /FE MODEL/ ) {
($dum, $dum, $dum, $dum, $dum ) = split /\s+/, $data ;
( $txt = $dum ) =~ s/\/.*\/// ; # strip system numbering
# print $txt, "\n" ;
$tables{$txt} = ' ' ;
last ;
} ;
if ( $ti == $tmax ) {
# print $data ;
$tables{$txt} .= $data . "\n" ;
last ;
} ;
}
}
sub characterData {
my( $parseinst, $data ) = @_;
if ( $writer->within_element('FE_MODEL') && $writer->within_element('TABLE') && $coords && $data != /^\s*$/ ) {
$writer -> characters ( $tables{$model} ) ;
$tables{$model} = ' ' ; #empty table
}
elsif ( ! $coords ) {
# print "Coords $coords : $data";
$writer -> characters( $data ) ;
}
}
sub cdatastart {
$writer -> raw( "<![CDATA[\n" );
}
sub cdataend {
$writer -> raw( "]]>\n" );
}
sub default {
# do nothing, but stay quiet
}
------------------------------
Date: Wed, 07 Dec 2005 14:18:01 -0700
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: javascript to perl converter?
Message-Id: <eto64q0txme.fsf@wilson.emschwar>
"P. Thompson" <no-spam@new.rr.com> writes:
> This would be the sort of thing I would like an auto converter for,
> more the hex conversions and character morphing. Sorry, I am trying
> to find the original from my example.
<snip>
> Thanks for looking.
I didn't. That's completely insane. If you have a contractual
relationship with someone that gives you code like that, either send
it back, or write up a new contract. In either case, beat the
creators of that... stuff roundly about the head and shoulders, if
possible.
There is no js->perl converter. Unless you are personally motivated
enough to create one, I doubt there will ever be such a thing. It's
simply too much work for very little gain, given that most of the
places the js is, you can't use perl anyhow.
-=Eric
------------------------------
Date: Wed, 07 Dec 2005 14:19:54 -0700
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: javascript to perl converter?
Message-Id: <eto1x0otxj9.fsf@wilson.emschwar>
"P. Thompson" <no-spam@new.rr.com> writes:
> On Tue, 6 Dec 2005, Eric Schwartz wrote:
>> No wonder. It's poorly-written.
>
> Deliberately obscurely written.
Amounts to the same thing, for me.
> The harder ones I had trouble getting my mind around to do that. I am
> actually better at perl than js but not a master. The js has been what
> has delayed or stymied me.
Sounds like you need to learn javascript better then; sorry, we only
do Perl here.
>> I seriously doubt such a utility would be feasible, and if one exists,
>> I would advise giving it a wide berth; the odds that it's correct and
>> good are very slim.
>
> Yes, the fancy html bits are probably not feasible. The cheesy url
> obscurers like my example would be feasible since perl is built with
> better support for the mathematical operations to begin with.
I think it's a lot harder than you seem to think it is, but hey, I'd
love to be wrong. Come back with your code to do it, and we can help
you improve it. I don't think you're going to find anyone skilled
enough to do it that's actually willing to help, though.
-=Eric
------------------------------
Date: Wed, 07 Dec 2005 21:44:51 GMT
From: "P. Thompson" <no-spam@new.rr.com>
Subject: Re: javascript to perl converter?
Message-Id: <Pine.LNX.4.63.0512071536260.28029@localhost.localdomain>
On Wed, 7 Dec 2005, Eric Schwartz wrote:
> Sounds like you need to learn javascript better then; sorry, we only
> do Perl here.
Indeed, that particular point is pretty obvious. Judging from the other
topics in this group, 'we' do a lot of surprising and amazing things with
perl, however.
> I don't think you're going to find anyone skilled
> enough to do it that's actually willing to help, though.
Hmm, rereading my past comments, I can't find anywhere where I was asking
anyone to do anything for me. Just if a given utility existed.
Thanks for looking, though.
---
Lord, protect me from those to whom you speak directly
All salute the new age, and I hope nobody escapes
------------------------------
Date: Wed, 07 Dec 2005 22:07:28 GMT
From: "P. Thompson" <no-spam@new.rr.com>
Subject: Re: javascript to perl converter?
Message-Id: <Pine.LNX.4.63.0512071548030.28029@localhost.localdomain>
On Wed, 7 Dec 2005, Eric Schwartz wrote:
> In either case, beat the
> creators of that... stuff roundly about the head and shoulders, if
> possible.
Ultimately, that's >exactly< what I would use such a converter for.
Today, viruses are spread by tricky fools who write code like what was
posted. Today for amateurs trace the viruese back to the source it is
easiest to decypher this stuff in a vm to let the browser's javascript
parser do its thing and see what comes out the other end, with all the
value-add the virus offers. Start over on the next virus.
The interesting idea to me is if perl could demunge the code taking the
browser out of the loop.
I have bits and pieces that do that work already, but was curious to know
if perhaps some alternative existed and I just didn't know. If not, no
biggy.
I don't know how I missed it before, but there are some utilities which I
can use. Not exactly what I had envisioned, but I can adapt:
http://search.cpan.org/~claesjac/JavaScript-0.55/JavaScript.pod#Why%3F
So much for 'we' only doing perl...
---
Lord, protect me from those to whom you speak directly
All salute the new age, and I hope nobody escapes
------------------------------
Date: 07 Dec 2005 20:28:22 GMT
From: "=?UTF-8?B?UGllcnJlIEfDqW5pZXlz?=" <p_geni...@yahoo.fr>
Subject: perl 5.6.1 and Encode.pm
Message-Id: <439745e6$0$25362$892e7fe2@authen.yellow.readfreenews.net>
Hi all,
I wish to use the Encode module (encode & decode methods)
but have only version 5.6.1 of Perl on Linux.
Old versions of Encode require perl 5.7.3 or higher.
Can I modify Encode.pm or replace it by another module
that can work correctly on Perl 5.6.1?
(I cannot update to a higher version of perl)
Thanks,
Pierre.
________________________________________________________________
échangez opinions et commentaires dans les forums de discussion.
http://www.usenetgratuit.com/
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8743
***************************************