Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi ive wrote a script then looks in directories, reads a file in each directory, and grabs some values in the file. This seems to work. But when i print the output, i get weird results.
#!/usr/bin/perl #configuration thingies $lsdir = '/var/www/foto'; #this is the dir to look in $dir = '2004'; #hard to explain this one, but it doesnt mat +ter much $year = '04'; #what is the value of yy in yy-mm-dd in on o +f the values grabbed ############################################## foreach (`ls $lsdir`) { #do a ls if (/album(\d{2,})/) { $album_nr = $1; $file = "$lsdir" . '/album' . "$album_nr" . '/album.da +t'; open FILE, "$file" or warn "coud not open $file: $! \n" and next; #s +o this looks in each /var/www/foto/album{2,}/album.dat $/ = ';'; #each string in album.dat is seperated by a +; <FILE>;<FILE>; $_ = <FILE>; s/\n//g; $name = (/.*\"(.*)\"/)[0]; #get the name from album.da +t <FILE>; $_ = <FILE>; s/\n//g; ($desc = (/.*\"(.+)\"/)[0]) || ($desc = "");# get the +description from album.dat while (<FILE>) { if (/parentAlbumName/) { $_ = <FILE>; $file_dir = (/.*\"(.+)\"/)[0]; last; } } #get the virtual directory from album.dat #all the values needed are stored. now some conditions the values shou +ld meet, or else get lost. if ($file_dir eq $dir) { if ($name =~ /\d\d\-\d\d\-\d\d/) { $date = "$&"; $date =~ s/\-//g; unless ($date =~ /^$year/) { warn "the date of album $album +_nr ($name) doesn't seem to be in yy-mm-dd format. skipping..\n"; next; } } else { warn "the title of album $album_nr ($n +ame) doesn't seem to contain a date, or a date in yy-mm-dd format. sk +ipping..\n"; next; } #so if the values met those conditions, store them in @names. unshift @names, "$album_nr:$name:$desc\n"; } } } print "@names"; #print
------------end of program
ok, now the real problem: when i run it in bash i get this output:
the title of album 107 (Eerste kwartaal van 2004) doesn't seem to cont +ain a date, or a date in yy-mm-dd format. skipping.. the title of album 131 (Untitled) doesn't seem to contain a date, or a + date in yy-mm-dd format. skipping.. :by SuuZ, Oxi, Vaisha, SPoT_D, SCVNGR, Groenshirt & TeKnoID 130:04-05-05 Zwolle:by Oxi & SPoT_D 124:04-04-28 Amsterdam:by Oxi 118:04-04-17 Hilversum:by Apraxia 117:04-04-17 Zeist:by SuuZ 116:04-04-17 Rotterdam:by SuuZ 113:04-04-17 Hilversum:by Oxi 112:04-04-17 Zeist:by Vaisha & Oxi 111:04-04-17 Bazart Anti Lounge:by Vaisha 110:04-04-10 Maarssen:by SPoT_D & Oxi 106:04-04-03 Tilburg:>Groene Shirt</a> 105:04-04-02 Hypnoskull Nighttown:by Apraxia 104:04-04-03 Tilburg:by Vaisha 103:04-04-17 Bazart Anti-Lounge:by Apraxia
the first two sentences are ok.. but after that comes this:
:by SuuZ, Oxi, Vaisha, SPoT_D, SCVNGR, Groenshirt & TeKnoID
and that should've been:
75:04-04-29 Teknival Noord-Frankrijk:by SuuZ, Oxi, Vaisha, SPoT_D, SCV +NGR, Groenshirt & TeKnoID
as you can see, this really sucks. one variable seems to replace another in (only) the first value of the array @names.
i have completely no idea why this happens, so i thought mayb you would :)

if you could help me with this, thanxs a lot

Replies are listed 'Best First'.
Re: Problem parsing and printing data from file
by Roy Johnson (Monsignor) on May 06, 2004 at 14:47 UTC
    I haven't figured out the main problem, yet, but I'll offer some suggestions for tidying this up:
    # $file = "$lsdir" . '/album' . "$album_nr" . '/album.dat'; # becomes $file = "$lsdir/album$album_nr/album.dat"; # open FILE, "$file" or # Don't quote variables unless you're constructing new strings open FILE $file or # $name = (/.*\"(.*)\"/)[0]; # no need for leading .*; better as ($name) = /"(.*)"/; # s/\n//g; # ($desc = (/.*\"(.+)\"/)[0]) || ($desc = ""); # Better as tr/\n//d; ($desc) = /"(.+)"/;
    Okay, I just thought of a possibility: There is a carriage return in the $name field for that album. In that case, the data is all there, but when you print it, the carriage return is causing the data that follows it to overwrite everything before it.

    The PerlMonk tr/// Advocate
      hey, that is the solution :)

      thanks a lot..
      (and for the code style tips) :)

      i had ours of headache on this one, but to go deeper in on this problem(so i don't make this error in the future):
      what exactly is the difference between a \n and \r?
      never really understood that

      tnx
        oh.. and why is tr/\n//d better that s/\n//g ?

        :)