ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: email@m-moser.de on January 06, 2013, 12:57:29 PM

Title: How to merge 2 arrays in ExifTool_config ?
Post by: email@m-moser.de on January 06, 2013, 12:57:29 PM
Hi,

I would like to merge the 2 tags "Keywords" and "SupplementalCategories" into one Array and remove duplicate entries.
I found the statements to remove duplicate entries, but I was not able to merge the two arrays before inside the ExifTool_config file. 
I tried several things but I was not successful, so I gave up.  Can anyone help me?

Thanks in advance,
Manfred

Title: Re: How to merge 2 arrays in ExifTool_config ?
Post by: Phil Harvey on January 06, 2013, 02:03:53 PM
Hi Manfred,

Assuming you want to merge SupplementalCategories into Keywords with no duplicates, you can do this:

exiftool -addtagsfromfile @ "-iptc:keywords-<supplementalcategories" "-iptc:keywords+<supplementalcategories" FILE

This is similar to an example in FAQ 17 (https://exiftool.org/faq.html#Q17) except that we are copying from anther tag in the same file.  Here the only trick is to use -addTagsFromFile (instead of the implied -tagsFromFile), otherwise the second assignment will override the first instead of combining with it.

- Phil
Title: Re: How to merge 2 arrays in ExifTool_config ?
Post by: Phil Harvey on January 06, 2013, 02:15:05 PM
Ooops, sorry.  Somehow I missed that you wanted to do this in a config file.  Here is some code to do this:

    # the first step is to make sure we have array references
    my $a0 = ref $val[0] eq 'ARRAY' ? $val[0] : [ $val[0] ];
    my $a1 = ref $val[1] eq 'ARRAY' ? $val[1] : [ $val[1] ];
    # the next step is to create an array with no duplicates
    my ($item, @array, %alreadyAdded);
    foreach $item (@$a0, @$a1) {
        next if $alreadyAdded{$item};
        push @array, $item;
        $alreadyAdded{$item} = 1;
    }
    return \@array;


There are other (quicker) ways to do this, but the above technique preserves the original ordering of the items as much as possible.

- Phil
Title: Re: How to merge 2 arrays in ExifTool_config ?
Post by: email@m-moser.de on January 07, 2013, 04:30:09 AM
Hi Phil,

I need your help again.
I put your code into my ExifTool_config file, but there is still something wrong.

    # Merge XMP:Subject and XMP:SupplementalCategories
    MyKeyw => {
        Require => {'XMP:Subject', 'XMP:SupplementalCategories'},
        ValueConv => q{
            # the first step is to make sure we have array references
            my $a0 = ref $val[0] eq 'ARRAY' ? $val[0] : [ $val[0] ];
            my $a1 = ref $val[1] eq 'ARRAY' ? $val[1] : [ $val[1] ];
            # the next step is to create an array with no duplicates
            my ($item, @array, %alreadyAdded);   
            foreach $item (@$a0, @$a1) {
                next if $alreadyAdded{$item};
                push @array, $item;      
                $alreadyAdded{$item} = 1;
            }
            return \@array;
        },   
    },

Here is the exiftool output:

P:\Fotos\Digital\TEST>..\exiftool -config ..\ExifTool_config -P -a -G1 -XMP:Subject -XMP:SupplementalCategories -MyKeyw TEST.JPG
[XMP-dc]        Subject                         : Subj1, Subj2, Duplicate
[XMP-photoshop] Supplemental Categories         : Cat1, Cat2, Duplicate


MyKeyw is empty and not printed.

When I use these "Require" statements:
       Require => 'XMP:Subject', 'XMP:SupplementalCategories',

I get this output:

P:\Fotos\Digital\TEST>..\exiftool -config ..\ExifTool_config -P -a -G1 -XMP:Subject -XMP:SupplementalCategories -MyKeyw TEST.JPG
[XMP-dc]        Subject                         : Subj1, Subj2, Duplicate
[XMP-photoshop] Supplemental Categories         : Cat1, Cat2, Duplicate
[Composite]     My Keyw                         : HASH(0x3a1e2c0)


Thanks in advance for your help,
Manfred
Title: Re: How to merge 2 arrays in ExifTool_config ?
Post by: Phil Harvey on January 07, 2013, 07:12:41 AM
Hi Manfred,

You forgot the indices in your Require entry:

    Require => { 0 => 'XMP:Subject', 1 => 'XMP:SupplementalCategories' },

- Phil