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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with substr to chop a string

Status
Not open for further replies.

pr0fess0r

Programmer
Jul 10, 2001
25
NZ
Hi Guys

I guess this is pretty simple
I want to remove the leftmost 53 characters in a string and return a string of everything that remains
i.e
@patharray contains
c:\inetpub\c:\inetpub\
I want to write

into a textfile

I tried something like this:

my $filelist = "$iwhome\\tmp\\filelist.txt";
open (FILELIST, "> $filelist");
foreach $item (@patharray) {
print FILELIST substr($item,10,10,"")."\n";
}
close FILELIST;


but that seems to chop the left 10 chars and only return 10 of the remaining...
can anyone suggest a better solution?

Many thanks in advance

Lucas Young
 
How about using a regular expression:

my $filelist = "$iwhome\\tmp\\filelist.txt";
open (FILELIST, "> $filelist");
foreach $item (@patharray) {
$item =~ s/\w\:\\[^\\]*\\//;
print FILELIST "$item\n";
}
close FILELIST; - tleish
 
#!perl
$str = 'c:\inetpub\$endstr = substr($str,11);
print "substr => $endstr\n";

# or, as tleish suggests... a better way is to regex it.
$str =~ s/c:\\inetpub\\//;
print "regex => $str\n";

TIMTOWTDI:)
HTH


keep the rudder amid ship and beware the odd typo
 
Your problem is that you're using the wrong syntax for substr. It has two forms:
substr(expr,offset,length)
substr(expr,offset)
If you don't specify a length, it will return everything to the end of the string.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top