[30391] in Perl-Users-Digest
Perl-Users Digest, Issue: 1634 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 12 11:09:49 2008
Date: Thu, 12 Jun 2008 08:09:10 -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, 12 Jun 2008 Volume: 11 Number: 1634
Today's topics:
Re: Change only one character to uppercase <rvtol+news@isolution.nl>
EPIC and "my" variables <steve.nospam.rainbird@mssint.nospam.com>
Re: FAQ 5.13 How can I output my numbers with commas ad <dalessio@motorola.NOSPAM.com>
Re: FAQ 7.14 What is variable suicide and how can I pre <brian.d.foy@gmail.com>
Re: FAQ 7.22 What's the difference between calling a fu <brian.d.foy@gmail.com>
Re: FAQ 7.22 What's the difference between calling a fu <mjcarman@mchsi.com>
How to unpack long long ? <ghadad@gmail.com>
How to unpack long long ? <ghadad@gmail.com>
Re: OT: please help me for my script <RedGrittyBrick@SpamWeary.foo>
Re: Perl DBI Module: SQL query where there is space in <wgumgfy@gmail.com>
Re: Question on learning Perl <tzz@lifelogs.com>
Re: sorting a hash / 2008-06-01 <dave@nospam.deezee.org>
Re: sorting a hash / 2008-06-01 <dave@nospam.deezee.org>
Re: sorting a hash / 2008-06-01 <uri@stemsystems.com>
XML::Simple how to force hash generation for keys <news@nana.franken.de>
Re: XML::Simple how to force hash generation for keys <danrumney@warpmail.net>
Re: XML::Simple how to force hash generation for keys <pue@gmx.net>
Re: XML::Simple how to force hash generation for keys <news@nana.franken.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 12 Jun 2008 12:16:25 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Change only one character to uppercase
Message-Id: <g2r4aq.14k.1@news.isolution.nl>
Peter Makholm schreef:
> Samik R.:
>> I am trying to change only one character to uppercase, if the char is
>> after a hyphen (-). I can't think of a RE to do that.
>
> You are able to make a regexp that matches a char after a hyphern,
> right?
From what follows, you meant "(matches and )captures", not just
"matches".
> The replace this match with "\U$1" and you're done. The escape
> sequence '\U' uppercases the rest of the string (or until a
> '\E'). Read about it in 'perldoc perlop' under the heading 'Quote and
> Quote-like Operators'
$ perl -wle 's/(?<=-)(.)/\u$1/g, print for @ARGV' abc-def-ghi
abc-Def-Ghi
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Thu, 12 Jun 2008 15:55:36 +0100
From: "Steve Rainbird" <steve.nospam.rainbird@mssint.nospam.com>
Subject: EPIC and "my" variables
Message-Id: <6bcrnaF3b2612U1@mid.individual.net>
I am running eclipse 3.3.2 on MS Vista with cygwin.
I have installed padWalker but still can't see the "my" variables when I
debug.
Any ideas?
--
Steve
------------------------------
Date: Wed, 11 Jun 2008 22:46:24 -0500
From: "Mario D'Alessio" <dalessio@motorola.NOSPAM.com>
Subject: Re: FAQ 5.13 How can I output my numbers with commas added?
Message-Id: <g2q6nf$n5f$1@newshost.mot.com>
"brian d foy" <brian.d.foy@gmail.com> wrote in message
news:110620080749240173%brian.d.foy@gmail.com...
> In article <g2m6bq$60s$1@newshost.mot.com>, Mario D'Alessio
> <dalessio@motorola.NOSPAM.com> wrote:
>
>> Rather than using a complex RE, I reverse the
>> string and then just add a comma after each set
>> of 3 numbers:
>
> Try it with 1234567.1234567 :)
Note that I said "Besides the fact that my code ignores
the plus and minus signs and the decimal point..."
I use the code only in places where I know I'm
getting whole numbers (e.g. reading byte sizes
of files).
Mario
------------------------------
Date: Thu, 12 Jun 2008 09:27:58 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 7.14 What is variable suicide and how can I prevent it?
Message-Id: <120620080927586545%brian.d.foy@gmail.com>
In article
<a72f0e8e-7e89-4655-a129-56f5e42af350@t54g2000hsg.googlegroups.com>,
Bill H <bill@ts1000.us> wrote:
> On Jun 11, 9:03 am, PerlFAQ Server <br...@stonehenge.com> wrote:
>
> > 7.14: What is variable suicide and how can I prevent it?
> >
> > This problem was fixed in perl 5.004_05, so preventing it means
> > upgrading your version of perl. ;)
> Two things:
>
> 1) this just explains the behaviour - it doesn't say what to do
> (unless I am blind)
You mean what to do to prevent vaiable suicide? It's the first sentence
of the answer: upgrade Perl.
------------------------------
Date: Thu, 12 Jun 2008 09:30:38 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 7.22 What's the difference between calling a function as &foo and foo()?
Message-Id: <120620080930386146%brian.d.foy@gmail.com>
In article <uZ74k.205237$yE1.69979@attbi_s21>, Michael Carman
<mjcarman@mchsi.com> wrote:
> PerlFAQ Server wrote:
> > When you call a function as &foo, you allow that function access to your
> > current @_ values, and you bypass prototypes. The function doesn't get
> > an empty @_--it gets yours! While not strictly speaking a bug (it's
> > documented that way in perlsub), it would be hard to consider this a
> > feature in most cases.
> I realize the FAQ isn't a tutorial but it is a tool for learning. To
> that end should it mention the primary case where this *is* a feature
> (AUTOLOAD)? e.g.
I don't know if that's the primary case, at least for my use. Simply
mentioning AUTOLOAD doesn't really add anything the answer, but having
a longer example with AUTOLOAD might be fine.
I'll add this to my queue of things to think about.
Thanks, :)
------------------------------
Date: Thu, 12 Jun 2008 11:43:22 GMT
From: Michael Carman <mjcarman@mchsi.com>
Subject: Re: FAQ 7.22 What's the difference between calling a function as &foo and foo()?
Message-Id: <uZ74k.205237$yE1.69979@attbi_s21>
PerlFAQ Server wrote:
>
> When you call a function as &foo, you allow that function access to your
> current @_ values, and you bypass prototypes. The function doesn't get
> an empty @_--it gets yours! While not strictly speaking a bug (it's
> documented that way in perlsub), it would be hard to consider this a
> feature in most cases.
I realize the FAQ isn't a tutorial but it is a tool for learning. To
that end should it mention the primary case where this *is* a feature
(AUTOLOAD)? e.g.
One notable case where this B<is> a useful feature is autoloading.
See L<AUTOLOAD: Proxy Methods> in L<perltoot> for more details.
-mjc
------------------------------
Date: Thu, 12 Jun 2008 06:51:50 -0700 (PDT)
From: Golan <ghadad@gmail.com>
Subject: How to unpack long long ?
Message-Id: <c34b3717-89d8-44b0-a5d4-45bd6f1ef166@59g2000hsb.googlegroups.com>
How do i unpack "long long" int type ( 8 bytes ) ?
this unpack("l8",$var) returns spaces .
but unpack("l4,substr($var,4)) returns the correct value .
Thanks
Golan
------------------------------
Date: Thu, 12 Jun 2008 06:52:06 -0700 (PDT)
From: Golan <ghadad@gmail.com>
Subject: How to unpack long long ?
Message-Id: <ba249144-5ad9-4c0f-ae23-484f7972b938@25g2000hsx.googlegroups.com>
How do i unpack "long long" int type ( 8 bytes ) ?
this unpack("l8",$var) returns spaces .
but unpack("l4,substr($var,4)) returns the correct value .
Thanks
Golan
------------------------------
Date: Thu, 12 Jun 2008 10:56:13 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: OT: please help me for my script
Message-Id: <4850f2c1$0$2488$da0feed9@news.zen.co.uk>
Dan Rumney wrote:
> swilting wrote:
>
> [snip]
>>
>> do you use FILEHANDLE ? perharps ?
>>
>> i not succes release with png random
>> ai text png
>>
>> help me ? its very important !
>> its this funny trool to the program cgi
>>
>> i do use html code into the perl code?
>
> Sorry John,
>
> I can't understand what you're asking at all.
>
> Are you using a program to translate to English?
You might find it instructive to check his posting history
--
RGB
------------------------------
Date: Thu, 12 Jun 2008 01:21:58 -0700
From: "Waylen Gumbal" <wgumgfy@gmail.com>
Subject: Re: Perl DBI Module: SQL query where there is space in field name
Message-Id: <6bc4l8F3b2bdnU1@mid.individual.net>
Sir Robin wrote:
> On Fri, 16 May 2008 20:07:20 -0700, Andrew DeFaria
> <Andrew@DeFaria.com> wrote:
> > Sir Robin wrote:
> > > On Mon, 12 May 2008 07:35:27 -0700, Andrew DeFaria
> > > <Andrew@DeFaria.com> wrote:
(...)
> > > > Here's a wonderful reason - because it bothers you.
> > > Say, you can't really read, can you?
> > Sure I can read. Hey, I can even disagree with you!
> No - you cant. Read, that is. On blindly disagreeing you do master
> the talent.
So one cannot disagree with you, or it's "blindly disagreeing"?
> > > No, *I* can ignore it quite well but it would indeed be a
> > > wonderful
> > > reason. My reason is that:
> > >
> > > 1. It bothers *others*.
> > > 2. No benefit whatsoever.
> > >
> > > So GTFY.
> > Why don't you let the others argue themselves if it really doesn't
> > bother you?
> That your kind will propably never be able to understand.
What you both are demonstrating is the core constant problem on Usenet:
failing to communicate; failing to even attempt to understand the other
person's point of view. I see people like you respond to people like
DeFaria (people who you obviously consider to be inferior) and display a
complete inability to see past your own views, and thus you have a never
ending fight between the academia types and the more every day types.
I don't understand why it's so difficult to comprehend that everyone
comes from a different back ground, upbringing; everything that happens
in your life shapes who you are, and no one chooses who they are born
as. So what good does is do to continually pass judgments just because
someone may not of been lucky enough to attain the same level of
education.
So please stop judging and try to make some effort to actually
communicate. If you really have so much more education then what excuse
is there for such a lack of communication? People like you should be
setting an example of such rather than fueling pointless debates. If the
academia types among us are unable to do something so basic as
effectively communicate with differing parties, then, I ask you, what
good is all that higher education??
-wg
------------------------------
Date: Thu, 12 Jun 2008 09:06:19 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Question on learning Perl
Message-Id: <86ej72px2c.fsf@lifelogs.com>
On Wed, 11 Jun 2008 22:29:13 -0700 Andrew DeFaria <Andrew@DeFaria.com> wrote:
AD> Ted Zlatanov wrote:
>> On Tue, 10 Jun 2008 23:36:16 -0700 Andrew DeFaria <Andrew@DeFaria.com> wrote:
>>
AD> Jürgen Exner wrote:
>>>> Dan Rumney <danrumney@warpmail.net> wrote:
>>>>>> When you first started using the language called English did you memorize most of the words, their meanings, etc.?
>>>> Oh yes, vey much so. Every week some 20-40 new words and the teacher would check them at the beginning of each class.
AD> Ah no you didn't. According to http://hypertextbook.com/facts/2001/JohnnyLing.shtml
...
>> I think the original quote meant "most of the words you learn at
>> first" not "most of the English words." 100-1000 words cover 50-75%
>> of English vocabulary by frequency[1], so memorization is quite
>> possible although it's definitely the wrong approach.
AD> I find your statement laughable being as I was the the one who
AD> originally made the quote itself! And let me tell ya brother I am
AD> 100% qualified to explain to you exactly what I meant and you are 0%
AD> qualified, and I must say I most certainly did mean "most of the
AD> words present in the English language"! Again, how would I know
AD> that? Why because it was me who said it!
Sorry about that.
When you follow up, can you please keep citation lines so people don't
get confused, as I did? Thanks.
>> English is very rich in vocabulary, but every modern language has
>> about the same number of basic terms, so that quote is a bit
>> misleading.
AD> Not in the least. The quote I made carries exactly the meaning I
AD> assign to it because I'm the guy who made it!. The quote about
AD> 500,000 is again literally true. I was making exactly that point,
AD> that people don't get anywhere close to learning everything about
AD> any language before they start using it - be it English or Perl. The
AD> point just flew waaaaayyyy over you head (which isn't really
AD> surprising).
Thanks for clarifying. Your posts are always educational *and*
humorous. I can assure you I look forward to the next one.
Ted
------------------------------
Date: 12 Jun 2008 09:12:45 GMT
From: "Dave Saville" <dave@nospam.deezee.org>
Subject: Re: sorting a hash / 2008-06-01
Message-Id: <fV45K0OBJxbE-pn2-CUTupfdPefN4@localhost>
On Thu, 12 Jun 2008 04:02:19 UTC, Uri Guttman <uri@stemsystems.com>
wrote:
> >>>>> "EP" == Eric Pozharski <whynot@pozharski.name> writes:
>
> EP> Dave Saville <dave@nospam.deezee.org> wrote:
> EP> *SKIP*
>
> >> Sorry to jump in with another question but I have a very similar
> >> problem. I am processing a consolidated apache2 logfile. I have
> >> multiple virtual hosts. All I care about are the site, the page
> >> served, a counter and the date.
>
> EP> Piece of advice. The next time you'll would like to I<jump in> consider
> EP> stoling the thread. Otherwise your question can be left unanswered.
> EP> Because it wasn't seen.
>
True, but the problem looked so similar.
> EP> *SKIP*
> >> I have tried various ideas I found by google but they all tend to be
> >> similar to this
>
> EP> Forget B<google>, use B<perldoc> instead.
>
> >> sub by_count
> >> {
> >> $urls{$site}{$b}[0] <=> $urls{$site}{$a}[0] or $a cmp $b;
> >> }
>
> EP> Hopefully Uri won't see that. I<$a> and I<$b> are special. However
> EP> only in context of B<sort>.
>
> i did. my eyes are bleeding!
>
Sorry I don't understand what you are getting at - apart from an in
joke.
> >> I would be grateful for any pointers.
>
> EP> If I guessed your problem right way, than:
>
> EP> print "$site $_ $urls{$site}{$_}[0] $urls{$site}{$_}[1]\n"
> EP> foreach(
> EP> sort { $urls{$site}{$a}[0] <=> $urls{$site}{$b}[0]; }
> EP> sort { $urls{$site}{$a}[1] <=> $urls{$site}{$b}[1]; }
> EP> keys %{$urls{$site}});
>
> are you (or the OP) trying to do a multilevel sort? it looks like yours
> will work but it is unusual to do two sort passes. and it relies on the
> sort to be stable (meaning equal keys stay in the same ordering post
> sort). perl now uses a stable sort but earlier versions didn't. it is
> not something you should depend upon.
<snip>
No not multi level here, just two ways of presenting the data
depending on which $site it came from. Thanks for the help guys, but
they are only variations on what I had tried with no luck. However, I
have discovered that here (OS/2) there is a bug in perl (5.8.2). I
don't know yet if it is a bug in perl or the port. I suspect the
latter, but
foreach my $url (sort {$urls{$site}{$b}[0] <=> $urls{$site}{$a}[0] }
keys %{$urls{$site}})
and
foreach my $url (sort by_value keys %{$urls{$site}})
sub by_value
{
$urls{$site}{$b}[0] <=> $urls{$site}{$a}[0];
}
Give different results. The first works correctly and the second for
some reason yet to be determined gets the *wrong* value of $site. I
stuck a print $site in the subroutine. That is where all the errors
came from, it was trying to compare site A's urls against site B's -
No wonder there where a lot of errors :-)
I was going to run my test case on my Solaris box but the darn thing
decided to trash its hard drive :-(
Oh, and the date is text and sortable - YYYY/MM/DD.
--
Regards
Dave Saville
NB Remove nospam. for good email address
------------------------------
Date: 12 Jun 2008 09:18:05 GMT
From: "Dave Saville" <dave@nospam.deezee.org>
Subject: Re: sorting a hash / 2008-06-01
Message-Id: <fV45K0OBJxbE-pn2-HHduqgo2SpEz@localhost>
On Wed, 11 Jun 2008 21:39:54 UTC, Jim Gibson <jimsgibson@gmail.com>
wrote:
> In article <fV45K0OBJxbE-pn2-Oh63rjwfG98s@localhost>, Dave Saville
> <dave@nospam.deezee.org> wrote:
>
> > On Fri, 30 May 2008 13:11:22 UTC, Gunnar Hjalmarsson
> > <noreply@gunnar.cc> wrote:
> >
> > > dn.perl@gmail.com wrote:
> > > > I want to sort a hash. The hash contains a list of cities and their
> > > > temperature
> > >
> > > Well, I'd rather say it contains three hash references.
> > >
> > > This is one sensible way to sort that data structure:
> > >
> > > foreach my $state ( sort keys %hash ) {
> > > print "State: $state\n";
> > > foreach my $city ( sort { $a cmp $b } keys %{ $hash{$state} } ) {
> > > print "$city = $hash{$state}{$city}{max_temp}\n";
> > > }
> > > print "\n";
> > > }
> >
> >
> > Sorry to jump in with another question but I have a very similar
> > problem. I am processing a consolidated apache2 logfile. I have
> > multiple virtual hosts. All I care about are the site, the page
> > served, a counter and the date.
> >
> > So my hash looks like $urls{$site}{$url} Beyond that I have a counter
> > and date thus:
> > $urls{$site}{$url}[0]++; # count
> > $urls{$site}{$url}[1] = $date;
> >
> > This works fine and I can list by site the page, count and date.
> >
> > foreach $site ( keys %urls)
> > {
> > foreach my $url (keys %{$urls{$site}})
> > {
> > print "$site $url $urls{$site}{$url}[0] $urls{$site}{$url}[1]\n";
> > }
> > }
> >
> > Putting a sort into the url loop gives me the results sorted by page
> > as expected. What I cannot figure out is how to do it by count and by
> > date.
> >
> > I have tried various ideas I found by google but they all tend to be
> > similar to this
> >
> > sub by_count
> > {
> > $urls{$site}{$b}[0] <=> $urls{$site}{$a}[0] or $a cmp $b;
> > }
> >
> > But this throws lots of "Use of uninitialized value....." errors on
> > that line and in doing so gets the wrong pages attributed to a site. I
> > have tried with yet another hash on the end with count & date keys
> > instead of the array, but it does not help.
> >
> > I would be grateful for any pointers.
>
> Are you trying to sort all of your records at once, regardless of site?
> You can't do that with a simple sort. If you just want to sort the data
> by site, here is an example:
<snip>
Thanks Jim. No, one site's ouput needs sorting by date and the rest by
count. I have found the problem, reported in another reply, but
basically my version of perl treats
sort { some sort statement} keys %hash
and
sort by_value keys %hash
where by_value contains the exact same sort statement differently.
--
Regards
Dave Saville
NB Remove nospam. for good email address
------------------------------
Date: Thu, 12 Jun 2008 14:53:11 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: sorting a hash / 2008-06-01
Message-Id: <x7bq26zovc.fsf@mail.sysarch.com>
>>>>> "DS" == Dave Saville <dave@nospam.deezee.org> writes:
DS> No not multi level here, just two ways of presenting the data
DS> depending on which $site it came from. Thanks for the help guys, but
DS> they are only variations on what I had tried with no luck. However, I
DS> have discovered that here (OS/2) there is a bug in perl (5.8.2). I
DS> don't know yet if it is a bug in perl or the port. I suspect the
DS> latter, but
DS> foreach my $url (sort {$urls{$site}{$b}[0] <=> $urls{$site}{$a}[0] }
DS> keys %{$urls{$site}})
DS> and
DS> foreach my $url (sort by_value keys %{$urls{$site}})
DS> sub by_value
DS> {
DS> $urls{$site}{$b}[0] <=> $urls{$site}{$a}[0];
DS> }
DS> Give different results. The first works correctly and the second for
DS> some reason yet to be determined gets the *wrong* value of $site. I
DS> stuck a print $site in the subroutine. That is where all the errors
DS> came from, it was trying to compare site A's urls against site B's -
DS> No wonder there where a lot of errors :-)
i highly doubt this is a perl bug. my gut feeling is that you have a
scoping problem. the first sort keeps $a and $b inside the sort block
and those will be set correctly. if you lexically declared $a and $b in your
code before the by_value sub, those will be used and screw up your
sort. or maybe a different $site is being used because it is in a
different scope. you need to post more code so we can see the
problem. it isn't just with the above code.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 12 Jun 2008 11:42:50 +0200
From: Wolfgang Thomas <news@nana.franken.de>
Subject: XML::Simple how to force hash generation for keys
Message-Id: <4850ef9a@news.alcatel.com>
Hi,
I use XML::Simple to parse the following small XML file:
<test>
<firstElem>
<anything id="1" value1="v11" value2="v12"/>
</firstElem>
<secondElem>
<anything id="2" value1="v21" value2="v22"/>
<anything id="3" value1="v31" value2="v32"/>
</secondElem>
</test>
Perl-Scipt:
use strict;
use warnings;
use Data::Dumper;
my $database = eval {XMLin("test.xml", keyattr => ['id'])};
if ($@) {
die ("Cannot parse test.xml: $@\n");
}
print Dumper ($database);
XML::Simple generates the following data structure:
$VAR1 = {
'firstElem' => {
'anything' => {
'value1' => 'v11',
'id' => '1',
'value2' => 'v12'
}
},
'secondElem' => {
'anything' => {
'3' => {
'value1' => 'v31',
'value2' => 'v32'
},
'2' => {
'value1' => 'v21',
'value2' => 'v22'
}
}
}
};
Now I wonder why XMLin the hash-structure differs between 'firstElem'
and 'secondElem'. This makes parsing the datastructure (in my opinion)
unnecessary complex.
How can I force XMLin to generate a hash for id = '1', so that the
result would look like:
$VAR1 = {
'firstElem' => {
'anything' => {
'1' => {
'value1' => 'v11',
'value2' => 'v12'
},
}
},
'secondElem' => {
'anything' => {
'3' => {
'value1' => 'v31',
'value2' => 'v32'
},
'2' => {
'value1' => 'v21',
'value2' => 'v22'
}
}
}
};
I wonder whether I am doing something fundamentally wrong, as I have
looked into lots of FAQs and into the documentation of XML::Simple, but
could not figure out any option to generate the output the way I need it.
Kind regards
Wolfgang
------------------------------
Date: Thu, 12 Jun 2008 06:58:41 -0400
From: Dan Rumney <danrumney@warpmail.net>
Subject: Re: XML::Simple how to force hash generation for keys
Message-Id: <4851015e$0$30238$4c368faf@roadrunner.com>
Wolfgang Thomas wrote:
[snip]
>
> Perl-Scipt:
> use strict;
> use warnings;
> use Data::Dumper;
>
>
> my $database = eval {XMLin("test.xml", keyattr => ['id'])};
> if ($@) {
> die ("Cannot parse test.xml: $@\n");
> }
> print Dumper ($database);
[snip]
What is XMLin?
This Perl script fails to run:
Cannot parse test.xml: Undefined subroutine &main::XMLin called at
newscript1.cgi line 6.
------------------------------
Date: Thu, 12 Jun 2008 14:52:08 +0200
From: =?ISO-8859-1?Q?Andreas_P=FCrzer?= <pue@gmx.net>
Subject: Re: XML::Simple how to force hash generation for keys
Message-Id: <6bckfmF39n2gsU1@mid.individual.net>
Wolfgang Thomas schrieb:
> Hi,
>
> I use XML::Simple to parse the following small XML file:
[snip]
> How can I force XMLin to generate a hash for id = '1'
[more snippage]
> I wonder whether I am doing something fundamentally wrong, as I have
> looked into lots of FAQs and into the documentation of XML::Simple, but
> could not figure out any option to generate the output the way I need it.
>
You want the 'ForceArray' option:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use XML::Simple;
my $xml = XMLin(
\*DATA,
ForceArray => ['anything'],
KeyAttr => ['id'],
);
print Dumper($xml);
__DATA__
<test>
<firstElem>
<anything id="1" value1="v11" value2="v12"/>
</firstElem>
<secondElem>
<anything id="2" value1="v21" value2="v22"/>
<anything id="3" value1="v31" value2="v32"/>
</secondElem>
</test>
> Kind regards
> Wolfgang
Greetings,
Andreas Pürzer
--
Have Fun,
and if you can't have fun,
have someone else's fun.
The Beautiful South
------------------------------
Date: Thu, 12 Jun 2008 15:04:17 +0200
From: Wolfgang Thomas <news@nana.franken.de>
Subject: Re: XML::Simple how to force hash generation for keys
Message-Id: <48511ed1$1@news.alcatel.com>
Oops, I forgot the most important use statement. Please add the
following line:
use XML::Simple;
Sorry for the inconvenience
Wolfgang
Dan Rumney wrote:
> Wolfgang Thomas wrote:
> [snip]
>>
>> Perl-Scipt:
>> use strict;
>> use warnings;
>> use Data::Dumper;
>>
>>
>> my $database = eval {XMLin("test.xml", keyattr => ['id'])};
>> if ($@) {
>> die ("Cannot parse test.xml: $@\n");
>> }
>> print Dumper ($database);
>
> [snip]
>
> What is XMLin?
>
> This Perl script fails to run:
>
> Cannot parse test.xml: Undefined subroutine &main::XMLin called at
> newscript1.cgi line 6.
------------------------------
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 V11 Issue 1634
***************************************