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

Greetings,
I am a little confused why the print statement print "$file\n"; works fine and prints test.html
but
print statement print "$title[1]\n"; does not print second element of $title
Thanks
-------------------------
my $file ="test.html"; my @title = split (".", $file); print "$file\n"; print "$title[1]\n";

Edit by dws to fix tags

Replies are listed 'Best First'.
Re: print statement problem
by sschneid (Deacon) on Jul 22, 2002 at 22:23 UTC
    You're using the incorrect syntax for split...
    #!/usr/local/bin/perl -w my $file = "test.html"; my @title = split(/\./, $file); print "$file\n"; print "$title[1]\n";
    Split also works off patterns, so you need to preceed the period with a backslash to escape it.

    Hope it helps,
    scott.
      Thanks,
      I presume the code that was posted contained the syntax change.
      no change, I still cannot get the second print statement to work. Cal
Re: print statement problem
by dpuu (Chaplain) on Jul 22, 2002 at 22:30 UTC
    A bit subtle this one: the first arg to split is actually a regular expression. "." matches any character; so every character is a separator. So the elements in @title are blank.

    To solve it, simply backslash it:

    split('\.', $file)
      Thanks,
      I tried the split syntax change and It works great.
      right on
      Cal
        It should really be written as split /\./, $file though, since split actually uses a regex as the delimiter. Passing a string is misleading and may trip someone who casually reads your code.

        Makeshifts last the longest.

Re: print statement problem
by cal (Beadle) on Jul 22, 2002 at 22:36 UTC
    correction:
    Greetings,
    I am a little confused why the print statement print "$file\n";works fine and prints test.html
    but
    print statement print "$title[1]\n"; does not print second element of $title
    Thanks
      #!/usr/bin/perl -w use strict; local $^W=1; use Data::Dumper; my $file ="test.html"; my @title = split (".", $file); print "$file\n"; print "$title[0]\n"; print "$title[1]\n"; print "$title[2]\n"; warn Dumper(\@title); @title = split (/\./, $file); print "$file\n"; print "$title[1]\n"; warn Dumper(\@title); use File::Basename; ## remember, suffix's are regex patterns my ($name,$path,$suffix) = fileparse($file,"\Q.html\E"); print "path $path\n"; print "name $name\n"; print "sufx $suffix\n";
      I suggest you read perlfunc:split as well as the File::Basename documentation, and for gods sake, Use strict warnings and diagnostics or die. As you can see from the above code (which you should run), the reason print "$title[1]\n"; doesn't print anything is because @title is empty, its as simple as that. If you had warnings turned on you would've been warned, so please always use warnings and strict , they'll save you a lot of grief (and use diagnostics if you're not accustomed to using warnings and strict).

      ____________________________________________________
      ** The Third rule of perl club is a statement of fact: pod is sexy.

       print "$title[1]\n"; prints second element of @title, not of $title.
      $title and @title is diferent variables in perl;