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!

substr prints but does not assign to string

Status
Not open for further replies.

Droppy

Technical User
Jun 30, 2009
1
US
Hello everyone,

This is in a subroutine.

my @block = @_;
my $string = join ("", @block);

First I do this to turn an array of strings into one single string. Then assign two variables to random values (for purpose of testing):

$preDB = "whydoyou";
$postDB = "hateme";

Then

if($string =~ m/Pre BD/i)
{
$indexPre = index($string, "Pre BD:");
$indexPost = index($string, "Post BD:");
$indexLV = index($string, "Lung Volumes:");
$preDB = substr($string, $indexPre, ($indexPost - $indexPre));
$postDB = substr($string, $indexPost, ($indexLV - $indexPost));

print "\nIn IF String: " . $string;
print "\nIn IF pre: " . $indexPre;
print "\nIn IF post: " . $indexPost;
print "\nIn IF LV: " . $indexLV;
print "\nIn IF PreDB: " . $preDB;
print "\nIn IF PostDB: " . $postDB;

In my output, string outputs fine and all the index have appropriate integer values given the string. However, both preDB and postDB continue to taunt me with their hatred. If I change the above assignment to preDB and postDB to print statements of the substr, the output is appropriate (ie. it shows the appropriate substring).

I am stumped as to why the output of substr will properly print but not assign to a string.

Thanks for your time and consideration.

My best wishes,

Droppy
 
It will really help if we can see the data you are working with.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Is it really supposed to be "BD" in some places, but "DB" in others?

Are you saying that this works:

Code:
$postDB = substr($string, $indexPost, ($indexLV - $indexPost));
print "\nIn IF PostDB: " . $postDB;

... but this doesn't:

Code:
print "\nIn IF PostDB: " . substr($string, $indexPost, ($indexLV - $indexPost));


Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top