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. | [reply] [d/l] [select] |
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
| [reply] |
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)
| [reply] [d/l] |
Thanks, I tried the split syntax change and It works great.
right on
Cal
| [reply] |
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
#!/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. | [reply] [d/l] [select] |
print "$title[1]\n"; prints second element of @title, not of $title.
$title and @title is diferent variables in perl;
| [reply] [d/l] |