Hi, I am sorry for my mistakes. Thanks for your suggestion. I tried as you said but still problem is there, I am showing what I tried as below

#!/usr/bin/perl use warnings; use strict; use Carp; use File::Find; use File::Spec::Functions qw( canonpath ); use XML::LibXML::Reader; use Data::Dumper; my @ARGV ="c:/main/folder"; die "Need directories\n" unless @ARGV; my $Number; my %hash; find( sub { my $file = $_; # my $path = canonpath $File::Find::name; my $path =$_; return unless -f $path; return unless $file =~ /(_service\.xml)$/; extract_information($path, \%hash); return; }, @ARGV ); sub extract_information { my( $path, $hash)=@_; my $ret = open my $xmlin, '<', $path; unless ($ret) { carp "Cannot open '$path': $!"; return; } my $reader = XML::LibXML::Reader->new(IO => $xmlin); unless ($reader) { carp "Cannot create reader using '$path'"; return; } while ( $reader->nextElement( 'Service' )) { my $Number = $reader->getAttribute( 'num'); $reader->nextElement( 'Suppliermodules' ); while ( $reader->nextElement( 'Hardwaremodule' )) { my $module = {}; $module->{type} = $reader->getAttribute('Type'); $module->{nr} = $reader->getAttribute('Nr'); push @{$hash->{$Number}}, $module; } while ( $reader->nextElement( 'softwaremodule' )) { my $module1 = {}; $module1->{service} = $reader->getAttribute('Service'); push @{$hash->{$Number}}, $module1; } $reader->nextElement( 'customermodules' ); while ( $reader->nextElement( 'softwaremodule' )) { my $module2 = {}; $module2->{cusservice} = $reader->getAttribute('Service'); push @{$hash->{$Number}}, $module2; } } close $xmlin or carp "Cannot close '$path': $!"; return; } print Dumper \%hash;

I tried with find(\&wanted, @directories); to search directives But I have some difficulties with that so I fallowed my procedure.What I want to do is I need to create hash with service num as the key and and all information of software module and hardware module attributes in customer and supplier module elements are array of values for that key. here I have two elements customer and supplier modules so I think array of values in a hash value.So finally after running this script I need to look my hash like (Service num attribute as the key and remaining information as the array of values for that key. so I have all service nums as the keys and its information is array of values.) I tried like that in the above script but it doesn't give any errors.I am not getting exact output, it giving some element attributes and some service num's only.I am new to perl, I am not able to produce what I want, please help me with that. If any typing errors is there please excuse me.

My output like this

'B7a'=>[ { Service='ADC', path='....' } { Type='engine', Nr='18', Servicenum='1', path='....' } { Type='motor', Nr='7', Servicenum='1', path='....' } { Type='supply', Nr='1', Servicenum='1', path='....' } { Service='input', path='----' } { Service='signal', path='----' } ].....................

Thanks in advance.


In reply to Re^2: how to get attribute values and store in a hash. by veerubiji
in thread how to get attribute values and store in a hash. by veerubiji

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.