Re: misunderstanding with the function split



sfantar wrote:
Hello everyone

Hello,

I would like to be able to print the songs'titles according to the author.
The format of the songs are as follow :

Freda Payne - In motion.mp3
Sylvester - Was it something that i said.mp3
George Benson & A. Franklin - Love All The Hurt Away.mp3



#!/usr/local/bin/perl -w

use strict;
use warnings;

my @author;
my @title;
my $song;
my $i;
my $j;

# @ARGV -> *.mp3

foreach $i (@ARGV) {
$i =~ s/ /_/g;
# print $i."\n";
@song = split (/-/, $i);

You should have gotten an error because @song is not declared.


}

The first time through the loop $i is assigned the string:
'Freda Payne - In motion.mp3'
The substitution operator converts that to:
'Freda_Payne_-_In_motion.mp3'
And then after the split() @song contains:
( 'Freda_Payne_', '_In_motion.mp3' )
The loop iterates through each file name in turn until finally $i is assigned:
'George Benson & A. Franklin - Love All The Hurt Away.mp3'
Then after the substitution and split @song contains:
( 'George_Benson_&_A._Franklin_', '_Love_All_The_Hurt_Away.mp3' )


foreach $j (@song) {

At this point @song will contain the author and title from the last entry in
@ARGV.

if ($j =~ /^_.*.mp3$/) {
print $j."\n";
}
}


NB : I haven't used the array @author yet because I don't know how I
can get the author from the song with the split function.

#!/usr/local/bin/perl
use strict;
use warnings;

my @authors;
my @titles;


for my $file ( @ARGV ) {
$file =~ tr/ /_/;

my ( $author, $title ) = split /-/, $file;

push @authors, $author;
push @titles, $title;
}

__END__



John
--
use Perl;
program
fulfillment
.



Relevant Pages