in reply to Really dumb question... ('ne' not working)

Well, I tried this code on my work server, which I know has a perfectly working Perl install. It still is giving me the same error:
Argument "Daft Punk\\Homework\\01 Daftendirekt\r\n" isn't numeric in numeric ne (!=) at mp3listgen.pl line 142, <INPUTFILE> line 1.

Here's my code...I'd greatly appreciate any help I could get.
#!/usr/bin/perl -w $fileOUT = "mp3list.txt"; open(OUTPUTFILE,">$fileOUT"); $filename = "A02.txt"; &parseDown; $filename = "A03.txt"; &parseDown; $filename = "A04.txt"; &parseDown; $filename = "A05.txt"; &parseDown; $filename = "A06.txt"; &parseDown; $filename = "A07.txt"; &parseDown; $filename = "A08.txt"; &parseDown; $filename = "A09.txt"; &parseDown; $filename = "A10.txt"; &parseDown; close(OUTPUTFILE); sub parseDown { open(INPUTFILE,"$filename"); $dir = ""; while(($line = <INPUTFILE>) != null) { $line =~ m%(.*)\\(.*)%; $tmp_dir = $1; $line = $2; if ($tmp_dir ne $dir) { print OUTPUTFILE "\n" . $tmp_dir . "\n"; $dir = $tmp_dir; } chomp($line); print OUTPUTFILE "\t" . $line . "\n"; } close(INPUTFILE); }
The files have entries that look like this:
Paul van Dyk\Seven Ways\01 Home Paul van Dyk\Seven Ways\02 Seven Ways Paul van Dyk\Seven Ways\03 Heaven Paul van Dyk\Seven Ways\04 I Like It Paul van Dyk\Seven Ways\05 Come (and Get It) Paul van Dyk\Seven Ways\06 Forbidden Fruit Paul van Dyk\Seven Ways\07 Beautiful Place Paul van Dyk\Seven Ways\08 People Paul van Dyk\Seven Ways\09 The Greatness of Britain Paul van Dyk\Seven Ways\10 I Can't Feel It Paul van Dyk\Seven Ways\11 Words

Thanks again,
Justin

Replies are listed 'Best First'.
(arturo): Re: Really dumb question... ('ne' not working)
by arturo (Vicar) on May 22, 2001 at 02:59 UTC

    Two things: (1) the error message says you're trying to treat a string as a number. ne tests for unequal *strings*, while != tests for unequal *numbers*.

    Perl (which, as you're finding out, is Not Java =) provides the defined() function to test for null values. So write that as

    while (defined ( my $line = <INPUT>) ) {

    Actually, go ahead and try

    while ($line = <INPUT>) {

    as even blank lines contain newlines, and so evaluate to true.

    (2) it makes more sense to write parseDown1 as a subroutine that accepts $filename as an argument than as something that expects a global to be set. Here it's not causing problems but it's not a good design to use in any script you'll keep (or cadge code from!).

    sub parseDown { my $filename = shift @_; # or $_[0] or just plain shift # rest of sub }

    (3) use strict and warnings (-w in pre-5.6.0 Perl). Doing this will require that you declare your variables with my2 and pay attention to scope. This is a Good thing.

    (4) don't blindly use $1 and $2 without checking to see whether your regex matched. If it doesn't weird things can happen. Instead, do this:

    if (my ($tmp_dir, $file) = $line =~ m%(.*)\\(.*)% ) { # do the rest of the stuff } else { warn "$line has incorrect format!\n"; }

    (5) I can't count.

    HTH

    1 That spelling, and the != null test, suggest you're coming from a Java background. Apologies if that's incorrect.
    2(update) or our (5.6.0) or use vars. Included for completeness. Ignore this for now, I just don't want to say anything false.

      wow. i actually come from a c++ background, but ive been doing _waaaaayyy_ too much java lately. good call. :) i cant believe i forgot that i use "defined" and not testing to see if it's != null....but i did, and we all make mistakes. ;-) anyways, thanks for the mini-lesson on standards and proper coding techniques -- i seriously lack these in perl, and i really need to start learning them.

      anyways, thanks a lot for your help, it was exactly what i needed!

      justin