[23493] in Perl-Users-Digest
Perl-Users Digest, Issue: 5706 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 23 18:10:41 2003
Date: Thu, 23 Oct 2003 15:10:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 23 Oct 2003 Volume: 10 Number: 5706
Today's topics:
Regular Expression newbie help/question (Sven)
Re: Regular Expression newbie help/question (Greg Bacon)
Re: Regular Expression newbie help/question <noreply@gunnar.cc>
Re: Regular Expression newbie help/question (Tad McClellan)
Re: Simulating case sensitivity for win32 files (Roy Johnson)
Undefined array values <e02@removethis.toao.net>
Re: Undefined array values <ak+usenet@freeshell.org>
Re: Undefined array values <grazz@pobox.com>
Re: Undefined array values <grazz@pobox.com>
Re: Undefined array values <xx087@freenet.carleton.ca>
Re: Undefined array values <e02@removethis.toao.net>
Re: Undefined array values <e02@removethis.toao.net>
Re: Undefined array values (Tad McClellan)
XML parser <bart-news@NOSPAMtvreclames.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 23 Oct 2003 13:02:49 -0700
From: sventore@gmx.net (Sven)
Subject: Regular Expression newbie help/question
Message-Id: <ad7279fb.0310231202.3fcc779@posting.google.com>
Hi,
I need some assistance on a search and replace issue with a
commaseparated file.
I need to replace all commas(,) on every lines EXCEPT those that occur
within doublequotes (",")
Thanks,
Sven
------------------------------
Date: Thu, 23 Oct 2003 20:56:16 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Regular Expression newbie help/question
Message-Id: <vpgg3g5lh9bse3@corp.supernews.com>
In article <ad7279fb.0310231202.3fcc779@posting.google.com>,
Sven <sventore@gmx.net> wrote:
: I need some assistance on a search and replace issue with a
: commaseparated file.
: I need to replace all commas(,) on every lines EXCEPT those that occur
: within doublequotes (",")
If you want to roll your own:
$ cat try
#! /usr/local/bin/perl
use warnings;
use strict;
while (<DATA>) {
s/(".*?"|,)/$1 eq ',' ? '<=>' : $1/eg;
print;
}
__DATA__
"foo,bar"
foo,bar
foo,bar,"baz,quux"
foo,,bar,"baz,quux"
foo,bar,,"baz,,quux"
$ ./try
"foo,bar"
foo<=>bar
foo<=>bar<=>"baz,quux"
foo<=><=>bar<=>"baz,quux"
foo<=>bar<=><=>"baz,,quux"
Consider using Text::CSV_XS, available on the CPAN.
Greg
--
The greatest dangers to liberty lurk in insidious encroachment by men
of zeal, well-meaning but without understanding.
-- Justice Louis D. Brandeis
------------------------------
Date: Thu, 23 Oct 2003 23:11:54 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Regular Expression newbie help/question
Message-Id: <bn9ges$sd738$1@ID-184292.news.uni-berlin.de>
Sven wrote:
> I need some assistance on a search and replace issue with a
> commaseparated file.
> I need to replace all commas(,) on every lines EXCEPT those that
> occur within doublequotes (",")
s/("[^"]*")|,/$1 ? $1 : $replacement/eg;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 23 Oct 2003 16:01:06 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Regular Expression newbie help/question
Message-Id: <slrnbpggci.7gp.tadmc@magna.augustmail.com>
Sven <sventore@gmx.net> wrote:
> I need to replace all commas(,) on every lines EXCEPT those that occur
^^^^^^
> within doublequotes (",")
perldoc -q EXCEPT
How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)
You are expected to check the Perl FAQ before posting to the
Perl newsgroup.
After you get the fields split, use join() to put them back
together with some other separator.
Or, better yet, use a module that understands the CSV format.
http://search.cpan.org/search?query=CSV&mode=all
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 23 Oct 2003 14:30:26 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Simulating case sensitivity for win32 files
Message-Id: <3ee08638.0310231330.43ba0be8@posting.google.com>
I just realized that the caching I suggested is pointless, since you
immediately rename the file. Of course, you can update the cache each
time you do a rename.
If you're only going to see each filename once, there's no point in
maintaining a cache. Just slurp the whole directory list into a hash
mapping lc(name)=>name, and then use it to lookup the file names when
you see them.
If you're going to rename all the files that aren't equal to their
canonical names (I'm guessing lowercase?), just do the rename for
every file, and don't worry about those that don't change.
I'm not real sure how you're planning to work with all the files, but
I hope some of these thoughts helped.
------------------------------
Date: Thu, 23 Oct 2003 19:13:32 GMT
From: "Experienced but Undocumented" <e02@removethis.toao.net>
Subject: Undefined array values
Message-Id: <wLVlb.160288$pl3.35654@pd7tw3no>
Newbie question: I've got an array like so:
$foo{'bar'} = "test";
Is there a way to assign a default value to any undefined values? For
example, if the program requests $foo{'abc'} and we haven't defined it, for
the default value to be used?
Thanks
------------------------------
Date: Thu, 23 Oct 2003 19:53:26 +0000 (UTC)
From: Andreas Kahari <ak+usenet@freeshell.org>
Subject: Re: Undefined array values
Message-Id: <slrnbpgcdl.qro.ak+usenet@mx.freeshell.org>
In article <wLVlb.160288$pl3.35654@pd7tw3no>, Experienced but Undocumented wrote:
> Newbie question: I've got an array like so:
> $foo{'bar'} = "test";
That's a hash, but never mind.
> Is there a way to assign a default value to any undefined values? For
> example, if the program requests $foo{'abc'} and we haven't defined it, for
> the default value to be used?
You could do something like this:
$value = (exists $foo{'bar'} ? $foo{'bar'} : $defaultvalue);
where $defaultvalue is some default value. Look up the
documentation for the exist function ("perdoc -f exists") and
the ternary "?:" operator ("perldoc perlop").
--
Andreas Kähäri
------------------------------
Date: Thu, 23 Oct 2003 20:00:00 GMT
From: Steve Grazzini <grazz@pobox.com>
Subject: Re: Undefined array values
Message-Id: <4rWlb.17337$Fc5.2109@nwrdny01.gnilink.net>
Experienced but Undocumented <e02@removethis.toao.net> wrote:
> Newbie question: I've got an array like so:
> $foo{'bar'} = "test";
That's usually called a hash (since the "array" is another of
Perl's basic data types, and it's not the one you mean).
> Is there a way to assign a default value to any undefined values?
> For example, if the program requests $foo{'abc'} and we haven't defined
> it, for the default value to be used?
You could do it with TIEHASH --
{
package Hash::Default;
use Tie::Hash;
our @ISA = qw(Tie::ExtraHash);
sub TIEHANDLE {
my ($class, $default) = @_;
bless [{}, $default], $class;
}
sub FETCH {
my ($ref, $default) = @{ shift() };
my $key = shift;
exists $ref->{$key} ? $ref->{$key} : $default;
}
}
tie my %foo, 'Hash::Default', 42;
print $foo{bar};
--
Steve
------------------------------
Date: Thu, 23 Oct 2003 20:02:17 GMT
From: Steve Grazzini <grazz@pobox.com>
Subject: Re: Undefined array values
Message-Id: <dtWlb.17346$Fc5.11501@nwrdny01.gnilink.net>
Steve Grazzini <grazz@pobox.com> wrote:
> You could do it with TIEHASH [ ... ]
>
> sub TIEHANDLE {
Silly of me; I guess that means you can just use the default
version of TIEHASH.
--
Steve
------------------------------
Date: 23 Oct 2003 20:04:00 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Undefined array values
Message-Id: <slrnbpgd2k.9ko.xx087@smeagol.ncf.ca>
Experienced but Undocumented <e02@removethis.toao.net> wrote:
> Newbie question: I've got an array like so:
> $foo{'bar'} = "test";
>
> Is there a way to assign a default value to any undefined values? For
> example, if the program requests $foo{'abc'} and we haven't defined it, for
> the default value to be used?
$foo{$key} = $default unless exists $foo{$key};
or
$foo{$key} ||= $default;
The second is a different test: it assigns the default value if the
hash value is undefined. A hash key can exist but have an undefined
value:
$foo{bar} = "test";
$foo{abc} = undef;
@keys = keys %foo; # @keys is ('bar', 'abc') in some order
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
Date: Thu, 23 Oct 2003 20:22:34 GMT
From: "Experienced but Undocumented" <e02@removethis.toao.net>
Subject: Re: Undefined array values
Message-Id: <eMWlb.160682$pl3.8923@pd7tw3no>
> In article <wLVlb.160288$pl3.35654@pd7tw3no>, Experienced but Undocumented
wrote:
> > Newbie question: I've got an array like so:
> > $foo{'bar'} = "test";
"Andreas Kahari" <ak+usenet@freeshell.org> wrote
> That's a hash, but never mind.
What I meant *blushes* :P
------------------------------
Date: Thu, 23 Oct 2003 20:22:45 GMT
From: "Experienced but Undocumented" <e02@removethis.toao.net>
Subject: Re: Undefined array values
Message-Id: <pMWlb.160685$pl3.99066@pd7tw3no>
"Experienced but Undocumented" <e02@removethis.toao.net> wrote in message
news:wLVlb.160288$pl3.35654@pd7tw3no...
> Newbie question: I've got an array like so:
> $foo{'bar'} = "test";
Thanks for your prompt replies, everyone!
------------------------------
Date: Thu, 23 Oct 2003 15:55:51 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Undefined array values
Message-Id: <slrnbpgg2n.7gp.tadmc@magna.augustmail.com>
Glenn Jackman <xx087@freenet.carleton.ca> wrote:
> Experienced but Undocumented <e02@removethis.toao.net> wrote:
>> For
>> example, if the program requests $foo{'abc'} and we haven't defined it, for
>> the default value to be used?
>
> $foo{$key} = $default unless exists $foo{$key};
> or
> $foo{$key} ||= $default;
But neither of those do what the OP asked for.
He didn't say he wants to add entries to the hash.
He wants something other than undef to be returned when
exists() is false.
tie()ing is the way to go.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 23 Oct 2003 20:58:05 +0200
From: "Bart van den Burg" <bart-news@NOSPAMtvreclames.nl>
Subject: XML parser
Message-Id: <bn98fi$f63$1@reader11.wxs.nl>
Hi
I'm in need of a very basic XML parser. All it has to do is read an XML file
and put all the data in a variable (hash reference?)
I tested to XML parsers, XML::Simple and XML::Smart on an XML file of 7110
bytes. The difference is quite big:
perl -e 'use Time::HiRes qw(gettimeofday tv_interval); my $t0 =
[gettimeofday]; use XML::Smart; XML::Smart->new("englishiso.xml"); my $t1 =
[gettimeofday]; print "XML::Smart: ". tv_interval($t0, $t1)."\n"; $t0 =
[gettimeofday]; use XML::Simple; XMLin("english.xml"); print "XML::Simple:
".tv_interval($t0, [gettimeofday])."\n"'
XML::Smart: 0.093696
XML::Simple: 0.6697
The reason I had to use 2 different xml files is that XML::Simple doesn't
support iso-8859-1 as encoding before version 5.7.2 and I'm running 5.6.1,
and XML::Smart gives me an error when I try to let it parse a document with
encoding ASCII (I need iso-8859-1 anyway, so that means XML::Simple is out
of the question at this point)
XML::Smart is way too slow for me, and XML::Simple is actually quite okay,
but now I'm wondering which other XML I could test, cause i want the fastest
out there, without it being too advanced. It's for a website (it's language
files), and it's opened once every time a page is requested
Which ones could I try?
thanks
Bart
------------------------------
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.
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 5706
***************************************