INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: OT: iTunes playlist /Perl script problem  (Read 2523 times)

Fred1

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 491
  • Change this by choosing profile
OT: iTunes playlist /Perl script problem
« on: December 09, 2013, 11:45:12 am »

Hi,
i have a little problem with a Perl script which doesn't do what i want.

The script opens an exported iTunes playlist file that contains TAB-separated field values.
It is a simple text file.

I'd like to simply print the contained filenames which come in the end of each line.
(Later i want to delete just these files from the filesystem).

This is the Perl code;

Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;

open(TXT, "<FilesToDelete.txt");

my @array = <TXT>;

foreach (@array)
{
  my $line = $_;
  #print $line; <- this would print every line of the text file

 my @mp3files = split("\t", $line); # <- get the tab separated fields into an array
 print $mp3files[-1]; # <- print the very last field of the current line to console
}

close (TXT);



The separation of the filenames seems to work - but only the filename from the last line of the text file is printed instead of every file name from every line.
What's going wrong here?

Thanks for you help!
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: OT: iTunes playlist /Perl script problem
« Reply #1 on: December 09, 2013, 01:41:12 pm »

You're about to discover End of Line differences across various platforms.

Your Mac's export of an iTunes playlist will end lines with the "\r" character (015).  But perl by default wants "\n".  So you have to tell perl what to expect as a newline.  Here's a cleaned up and simplified version of your script, that uses more defaults:

Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;
use v5.12;     # or later to get "say" and "unicode_strings" feature

$/ = "\r";      # set end-of-line char to "\r" instead of "\n".

foreach (<>) {
    chomp;                          # eat the end of line char
    /\t([^\t]+)$/;                # capture only the stuff after the last tab
    next if $1 eq 'Location';  # ignore header line
    say $1
}

Run your code passing a file name argument:

   yourscript.pl FilesToDelete.txt

Beware: you're probably going to run into Unicode issues...
Logged
The opinions I express represent my own folly.

Fred1

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 491
  • Change this by choosing profile
Re: OT: iTunes playlist /Perl script problem
« Reply #2 on: December 09, 2013, 04:52:30 pm »

Thank you very much, MrC!

Tomorrow i will try your code and report success.
Logged

Fred1

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 491
  • Change this by choosing profile
Re: OT: iTunes playlist /Perl script problem
« Reply #3 on: December 10, 2013, 05:20:03 am »

Hello MrC,

i tried your script, but unfortunately it generates no output on my system.

But that doesn't matter because, with your tips, my own script runs very well now.
$/ = "\r"; and "say" were the keywords for my problem.

So, now i can delete my unwanted files with the script and an iTunes playlist from my filesystem.
Thank you again for your help!
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: OT: iTunes playlist /Perl script problem
« Reply #4 on: December 10, 2013, 12:10:47 pm »

I'm curious to know why, and what you changed to make it work for you.  I tested the script here against my Mac iTunes export and it worked fine.  Was yours a Windows iTunes export?

Glad it is working for you.
Logged
The opinions I express represent my own folly.

Fred1

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 491
  • Change this by choosing profile
Re: OT: iTunes playlist /Perl script problem
« Reply #5 on: December 10, 2013, 03:36:28 pm »

I'm testing on a Mac under Lion, the iTunes list is generated on a Mac, too.

The header Line of the list looks like this (it is a german iTunes):
Code: [Select]
Name Interpret Komponist Album Werk Genre Größe Dauer CD-Nummer Zähler Titelnummer Zähler Jahr Geändert Hinzugefügt Datenrate Abtastrate Lautstärkeanpassung Art Equalizer Kommentar Wiedergaben Zuletzt gespielt Übersprungen Zuletzt übersprungen Meine Wertung Ort
Your slightly modified code:
Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;
use v5.12;                  # or later to get "say" and "unicode_strings" feature

$/ = "\r";                  # set end-of-line char to "\r" instead of "\n".

foreach (<>) {
chomp;                      # eat the end of line char
/\t([^\t]+)$/;              # capture only the stuff after the last tab
next if $1 eq 'Ort';        # ignore header line
say $1
}
perl listfiles.pl files.txt  does not output anything on my system.

My own modified script works like expected now (but isn't very Perly):
Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;
use v5.12;

$/ = "\r";  #Mac verwendet \r anstelle von \n !!!

open(TXT, "<files.txt");

my @array = <TXT>;

foreach (@array)
{
  my $zeile = $_;

  my @datei = split("\t", $zeile);
  say $datei[-1];
 
  #unlink $datei[-1] or warn "Konnte Datei nicht löschen: $datei[-1]";
}
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: OT: iTunes playlist /Perl script problem
« Reply #6 on: December 10, 2013, 03:42:13 pm »

Great.

The reason not to open the file explicitly is that you're not able to use your code as a filter taking input from STDIN.

Using the <> input operator automatically uses ARGV or STDIN when ARGV is empty.  So these act equivalently:

   perl listfiles.pl files.txt
   perl listfiles.pl < files.txt
   cat files.txt | perl listfiles.pl

as well as:

   cat file1.txt file2.text ... | perl listfiles.pl
Logged
The opinions I express represent my own folly.
Pages: [1]   Go Up