Re: A Script To Convert iTunes XML To Tab Delimited Format
- From: Tad McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Fri, 26 May 2006 21:36:43 -0500
jean@xxxxxxxxxxxxxxx <jean@xxxxxxxxxxxxxxx> wrote:
Here is a script written in PERL
No it isn't.
It is written in Perl.
open(File,$InputXMLFile);
You should always, yes *always*, check the return value from open():
open(File, $InputXMLFile) or die "could not open '$InputXMLFile' $!";
if ($Line =~ /\<key\>\d+\<\/key\>/) {
Angle brackets are not special in regular expressions, so there
is no need to backslash them.
If you choose a delimiter other than slash, then you don't
have to backslash the slash either:
if ($Line =~ m#<key>\d+</key>#) {
if ($RecordHash{'Song'} ne "") {
if ($RecordHash{'Song'} eq "") {
Control can never get here, so there is not much point in
putting code here...
if ($RecordHash{'Artist'} eq "") {
$RecordHash{'Artist'} = "Empty Field";
}
if ($RecordHash{'Album'} eq "") {
$RecordHash{'Album'} = "Empty Field";
}
if ($RecordHash{'File Type'} eq "") {
$RecordHash{'File Type'} = "Empty Field";
}
if ($RecordHash{'Genre'} eq "") {
$RecordHash{'Genre'} = "Empty Field";
}
You can replace all 4 of those ifs with:
foreach my $key ( qw/Artist Album Genre/, 'File Type' ) { #untested
$RecordHash{$key} = 'Empty Field' unless length $RecordHash{$key};
}
my $RecordEntry = join("\t", $RecordHash{'Album'},
$RecordHash{'Artist'}, $RecordHash{'Song'}, $RecordHash{'Track
Number'}, $RecordHash{'Genre'}, $RecordHash{'Running Time'},
$RecordHash{'File Size'}, $RecordHash{'File Type'}, $RecordHash{'File
Location'});
That can written more clearly using a "hash slice":
my $RecordEntry = join("\t", @RecordHash{'Album', 'Artist', 'Song',
'Track Number', 'Genre',
'Running Time', 'File Size',
'File Type', 'File Location'
};
if ($Field =~ /Name/i) {
$RecordHash{'Song'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Artist/i) {
$RecordHash{'Artist'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Album/i) {
$RecordHash{'Album'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Genre/i) {
$RecordHash{'Genre'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Kind/i) {
$RecordHash{'File Type'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Size/i) {
$RecordHash{'File Size'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Total Time/i) {
$RecordHash{'Running Time'} =
&TrimAndDecode($Value);
} elsif ($Field =~ /Track Number/i) {
$RecordHash{'Track Number'} =
&TrimAndDecode($Value);
That can be written more clearly by using a hash to contain
the name mappings:
my %names = (
Name => 'Song',
Artist => 'Artist',
Album => 'Album',
Genre => 'Genre',
Kind => 'File Type',
Size => 'File Size',
'Total Time' => 'Running Time',
'Track Number' => 'Track Number'
);
foreach my $name (keys %names) {
$RecordHash{ $names{$name} } = TrimAndDecode($Value);
}
--
Tad McClellan SGML consulting
tadmc@xxxxxxxxxxxxxx Perl programming
Fort Worth, Texas
.
- References:
- Prev by Date: Re: How many elements are there in the array.
- Next by Date: Re: Assigning a hash of array to another hash
- Previous by thread: A Script To Convert iTunes XML To Tab Delimited Format
- Next by thread: Re: A Script To Convert iTunes XML To Tab Delimited Format
- Index(es):
Relevant Pages
|