Harold Castro wrote:
> Good day!

Hello,

> I'm about to transfer our ip allocation table from
> openoffice spreadsheet into an ldif format. The
> spreadsheet has 19 columns, some cells are empty
> though. So far this is what I got:
> 
> #!/usr/local/bin/perl
> use warnings;
> use strict;
> 
> my $input = shift @ARGV;
> my $output = shift @ARGV;
> our @attributes = ('ipNetworkNumber: ', 'Prefix: '
> ,'ipNetmaskNumber: ','Range: ', 'BID: ','PID:
> ','ipAssignedTo: '
>                    ,'description: ','RID: ','Region:
> ','Area: ','SID: ', 'ipAllocStatus: ','manager:
> ','ipAssignedDate: '
>                    ,'TID: ','ipNetworkType: ','2ndOF:
> ','inetNum: ');
> 
> 
> open INPUT, "$input" or die $!;
> open OUTPUT, ">$output" or die $!;
> 
> while (<INPUT>){
> 
> #this two takes care of empty cells
> s/:$/:NA/g;
> s/:(?=:)/:NA/g;
> 
>    foreach $field (split/:/, $_){
>      print OUTPUT "$field ";
>    }
> 
> }
> 
> What I wanted to do is to replace the line:
> "print OUTPUT "$field... with something that will,
> upon splitting a $field, it will pair it to the
> content of the @attributes:
> 
> For example: 
> I have an @array = ('favorite: ', 'lessfavorite:
> 'worstfavorite: ')
> 
> then I have a line separated with colons that has:
> dog:cat:bird
> 
> 
> I would then split this line and pair it one by one to
> the content of the array
> e.g;
> 
> favorite: dog
> lessfavorite: cat
> worstfavorite: bird

Here is one way to do it:

my @array = qw( favorite lessfavorite worstfavorite );

while ( <DATA> ) {
    chomp;
    my @fields = map length() ? $_ : 'NA', split /:/, $_, -1;

    next unless @fields == @array;

    my %pairs;
    @pairs{ @array } = @fields;

    print map( "$_: $pairs{$_} ", @array ), "\n";
    }


__DATA__
dog:cat:bird
::
one::three
four:five:




John
-- 
use Perl;
program
fulfillment

-- 
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