How about:

cat fn | grep <string to be searched for> | wc

as a non-Perl approach to the problem...

-----Original Message-----
From: Hans Meier (John Doe) [mailto:[EMAIL PROTECTED] 
Sent: Thursday, March 30, 2006 11:38 AM
To: beginners@perl.org
Subject: Re: Counting specific elements in a XML object

Dave Adams am Donnerstag, 30. März 2006 21.12:
> If I have a xml file like the following:
>
> <?xml version='1.0'?>
> <employee>
>         <name>John Doe</name>
>         <age>43</age>
>         <sex>M</sex>
>         <department>Recieving</department>
> </employee>
> <employee>
>         <name>Bob Gordon</name>
>         <age>50</age>
>         <sex>M</sex>
>         <department>Shipping</department>
> </employee>
>
>
> Is there some perl module out there that can help me get the number of
> employees or in other words, the number occurences of "<employee>"?
>
> I guess I can get it conventionally by reading in the file and doing a
> count every time the script encounters "<name>".  But there must be an
> easier way.

Ok, let's start with:

perl -e 'while(<STDIN>){/<employee>/&&$i++};print $i' < data.xml

Anybody with an easier way in the sense of
- less typing
- more understandable
- less module imports ;-)
?

[preconditions: nothing else to do with the input; no CDATA containing 
<employees>; valid XML; etc.]

Hans



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to