Re: A Script To Convert iTunes XML To Tab Delimited Format



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
.



Relevant Pages

  • Re: WWW
    ... The only reason we need the term "forward slash", as I understand it, is ... But does the average non-expert get much exposure to the backslash? ... When I use Windows I use a command shell a lot, ... and I run into a lot of trouble compiling files that are ...
    (alt.usage.english)
  • Re: Forward vs. backslash notation
    ... described by a forward slash, X/S, whereas set complements are ... Well that "backslash" is actually a form of a minus sign. ... at least since FORTRAN, though I have a feeling this notation predates ... OTOH why the backslash in complement? ...
    (sci.math)
  • Re: Forward vs. backslash notation
    ... described by a forward slash, X/S, whereas set complements are ... Well that "backslash" is actually a form of a minus sign. ... something else is needed for the relative complement of sets. ... at least since FORTRAN, though I have a feeling this notation predates ...
    (sci.math)
  • Re: midterm for 94.404 at UMass Lowell
    ... Hash Table creation is O ... Well, the expected running time may be O, if you ... If the instructor expected you to ... Did the instructor have some more clever solution in mind that achieves ...
    (comp.theory)
  • Re: CFileDialog breaks on paths with slashes
    ... A backslash is the de facto standard ... The WinAPI file I/O functions will do a conversion for you as ... The fact is that a backslash has been ... I've rarely seen anyone use a forward slash over ...
    (microsoft.public.vc.language)