Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with split() function.

Status
Not open for further replies.

calabama

Programmer
Feb 19, 2001
180
US
The LOOP seems to work ok. I just keep getting nothing with the print statement print "$text_link\n";.

I created a sub'fix_file' that would fix each element' $file ' of the array' @filenames ' and put the data into a $scalar' $text_link '

Since each of these are file names formated the same way. They consist of this file format' xxyyzz.shtml'

I would like to split each element at the dot .
The file extension shtml will not be used.
The first 6 chars of each element go into $text_link.


Thanks You s-)


--------- LOOP ---------------
foreach my $file (@filenames) {
next if $file eq'.';
next if $file eq'..';
&fix_file;
print &quot;<A HREF=../../ppc/library/minutes_archive/$year/$file>$text_link $year</A>\n&quot;;
}


--------- sub fix_file -----------------
sub fix_file{
$file_text=($text_link , $ext) = split(/./ ,$file);
print &quot;$text_link\n&quot;;
}
#fix_file
In the begining
Let us first assume that there was nothing to begin with.
 
Skip the extra sub my man... use a REGex :

foreach my $file (@filenames) {
next if $file eq'.';
next if $file eq'..';

# strip out the first period and anything after it.
# I hope your 100% sure about the nomeclature of your
# files!
($text_link = $file) =~ s/\..*//;

print &quot;<A HREF=../../ppc/library/minutes_archive/$year/$file>$text_link $year</A>\n&quot;;
}
 
I would change the line:
$file_text=($text_link , $ext) = split(/./ ,$file);
to:
($text_link, $ext) = split(/./, $file);
and that should work just peachy.
-- Glenn
 
Thanks Coderifous,

It works like a charm within the loop..

I needed to do some other processing in the sub. Is it harder to do the same thing in the sub.

Tim In the begining
Let us first assume that there was nothing to begin with.
 
Calabama,

No. You could do the same thing in the fix_file sub.

--------- LOOP ---------------
foreach my $file (@filenames) {
next if $file eq'.';
next if $file eq'..';


#here we are calling your sub and explicitly passing the
#file name to it.

fix_file($file);

print &quot;<A HREF=../../ppc/library/minutes_archive/$year/$file>$text_link $year</A>\n&quot;;
}


--------- sub fix_file -----------------
sub fix_file{
#the @_ default array, stores all values passed to the sub.
#you can access the values just as you would access any
#other arrays values. I use:

my $file = shift @_;

#but you could also say:
# my $file = @_[0];

($text_link = $file) =~ s/\..*//;

print &quot;Text link is: $text_link\n&quot;;

#do other processing

}
#fix_file
 
Thanks a million

Tim Briggs In the begining
Let us first assume that there was nothing to begin with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top