Re: XML::Simple help request

2006-08-22 Thread Owen
On Tue, 22 Aug 2006 09:13:47 +0100
Gary Stainburn <[EMAIL PROTECTED]> wrote:

> On Tuesday 22 August 2006 05:32, Owen Cook wrote:
> > I am trying to get email addresses out of a Sylpheed address book. The
> > output of Dumper is;
> >
> > $VAR1 = {
> >   'attribute-list' => [
> >   {}
> > ],
> >   'first-name' => '',
> >   'uid' => '158149473',
> >   'cn' => 'Julie Jumper',
> >   'address-list' => [
> > {
> >   'address' => [
> >{
> >  'email' =>
> > '[EMAIL PROTECTED]', 'uid' => '158149474', 'remarks' =[1]> '',
> >  'alias' => ''
> >}
> >  ]
> > }
> >   ],
> >   'last-name' => '',
> >   'nick-name' => ''
> > };


Thanks to all that replied. Here is the script to get at Sylpheed address in a 
readable form.

#!/usr/bin/perl

use XML::Simple;

#use Data::Dumper; #For debugging
use strict;

my $xml = new XML::Simple( KeyAttr => [], ForceArray => 1 );

my $data = $xml->XMLin("ab6.xml");# In form 'addrbook-06.xml' etc.

#print Dumper($data); #For debugging

foreach my $d ( @{ $data->{person} } ) {
print $d->{cn} . "\t";
print $d->{'address-list'}[0]->{'address'}[0]->{'email'} . "\n";
}


And here is a script that will do the same to an LDIF file

#!/usr/bin/perl

use strict;
my $file   = shift; #Just pass the first part of the file name.
my $ldif_addresses = "/path/to/ldifs/$file.ldif"; #ldif extension added here
my $ldif_sorted= "/path/to/writeto/$file.sorted";
open( FH, "$ldif_addresses" ) or die "cant open $ldif_addresses $!\n";
open( OF, ">$ldif_sorted" )   or die "cant open $ldif_addresses $!\n";
while () {
chomp;
my $line = $_;
if ( $line =~ /cn: / )   { print OF "$'\t"; }
if ( $line =~ /mail: / ) { print OF "$'\n"; }

}
close OF;
close FH;
open( FH, "$ldif_sorted" ) or die "cant open $ldif_sorted $!\n";
my @list = ;
print " ";
@list = sort { $a cmp $b } @list;
print "@list";

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: XML::Simple help request

2006-08-22 Thread Mumia W.

On 08/21/2006 11:32 PM, Owen Cook wrote:

I am trying to get email addresses out of a Sylpheed address book. The
output of Dumper is;

$VAR1 = {
  'attribute-list' => [
  {}
],
  'first-name' => '',
  'uid' => '158149473',
  'cn' => 'Julie Jumper',
  'address-list' => [
{
  'address' => [
   {
 'email' => '[EMAIL PROTECTED]',
 'uid' => '158149474',
 'remarks' =[1]> '',
 'alias' => ''
   }
 ]
}
  ],
  'last-name' => '',
  'nick-name' => ''
};

I cannot get my head around extracting the 'email' it seems be be buried
deep inside an array and a hash

Here is an attempt;

#!/usr/bin/perl

use XML::Simple;
use Data::Dumper;
use strict;

my $xml = new XML::Simple (KeyAttr=>[], ForceArray => 1);

my $data = $xml->XMLin("ab6.xml");

foreach my $d (@{$data->{person}}) {
  print $d->{cn};#<- that works
print "\t";
  print $d->{"address-list"} # <-need to get down to address and email.
 # most attempts failed but some gave a
 # hash reference [...]


print $d->{address-list}[0]{address}[0]{email};

or try this:

use Alias;
alias 'addresses' => $d->{address-list}[0]{address}
foreach my $ar (@addresses) {
  print "email = $ar->{email}\n";
}

WARNING: UNTESTED CODE




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: XML::Simple help request

2006-08-22 Thread Gary Stainburn
On Tuesday 22 August 2006 05:32, Owen Cook wrote:
> I am trying to get email addresses out of a Sylpheed address book. The
> output of Dumper is;
>
> $VAR1 = {
>   'attribute-list' => [
>   {}
> ],
>   'first-name' => '',
>   'uid' => '158149473',
>   'cn' => 'Julie Jumper',
>   'address-list' => [
> {
>   'address' => [
>{
>  'email' =>
> '[EMAIL PROTECTED]', 'uid' => '158149474', 'remarks' =[1]> '',
>  'alias' => ''
>}
>  ]
> }
>   ],
>   'last-name' => '',
>   'nick-name' => ''
> };
>
> I cannot get my head around extracting the 'email' it seems be be buried
> deep inside an array and a hash
>
> Here is an attempt;
>
> #!/usr/bin/perl
>
> use XML::Simple;
> use Data::Dumper;
> use strict;
>
> my $xml = new XML::Simple (KeyAttr=>[], ForceArray => 1);
>
> my $data = $xml->XMLin("ab6.xml");
>
> foreach my $d (@{$data->{person}}) {
>   print $d->{cn};#<- that works
> print "\t";
>   print $d->{"address-list"} # <-need to get down to address and email.
># most attempts failed but some gave a
># hash reference
> print "\n";
>
> }
>
> TIA for any assistance/clues
>
>
> --
> Owen

print "cn='".$VAR1->{'cn'}."'\n";
print "email='".$VAR1->{'address-list'}[0]->{'address'}[0]->{'email'}."'\n";

Work your way down the list counting curly and square brackets.

$VAR1-> pointer to whole hash
{'address-list'}hash element
[0]->   because address-list is an array => [
{'address'} hash element of array's first entry
[0]->   because address is an array
{'email'}   hash element in 'address's 1st entry
-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




XML::Simple help request

2006-08-21 Thread Owen Cook

I am trying to get email addresses out of a Sylpheed address book. The
output of Dumper is;

$VAR1 = {
  'attribute-list' => [
  {}
],
  'first-name' => '',
  'uid' => '158149473',
  'cn' => 'Julie Jumper',
  'address-list' => [
{
  'address' => [
   {
 'email' => '[EMAIL PROTECTED]',
 'uid' => '158149474',
 'remarks' =[1]> '',
 'alias' => ''
   }
 ]
}
  ],
  'last-name' => '',
  'nick-name' => ''
};

I cannot get my head around extracting the 'email' it seems be be buried
deep inside an array and a hash

Here is an attempt;

#!/usr/bin/perl

use XML::Simple;
use Data::Dumper;
use strict;

my $xml = new XML::Simple (KeyAttr=>[], ForceArray => 1);

my $data = $xml->XMLin("ab6.xml");

foreach my $d (@{$data->{person}}) {
  print $d->{cn};#<- that works
print "\t";
  print $d->{"address-list"} # <-need to get down to address and email.
 # most attempts failed but some gave a
 # hash reference
print "\n";

}

TIA for any assistance/clues


-- 
Owen


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]