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

Splitting and using variables as inputs for a function

Status
Not open for further replies.

mikawin

Technical User
Apr 15, 2009
28
US
Hello,

I have a column of data that is of the following format:

611,1,X;323,1,C;209,1,R;119,1,Y;86,1,P

I am splitting a field based on ';' and on each of the split values calling on a function (an arbitrary example is the substring function.) Substring works on all of the values except the 86,1,P. What do I need to modify to include this field as well?

Thanks,
Mika

 
Don't understand what doesn't work. Can you show some code?
 
Hi mikrom,

Here is a sample:

$inString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$replaceString = '6,1,X;23,1,C;19,1,R;4,1,Y;5,1,P'

@replaceArray = split( ";", $replaceString);

foreach $elements (@replaceArray)
{
($offset, $len, $replace) = split(",", $elements);
substr($inString, $offset, $len, $replace);
print "$inString\n";
}


In the above case, I am not able to replace for the last set of values 5,1,P.

~Mika
 
Running your script I got this
Code:
ABCDEFXHIJKLMNOPQRSTUVWXYZ
ABCDEFXHIJKLMNOPQRSTUVWCYZ
ABCDEFXHIJKLMNOPQRSRUVWCYZ
ABCDYFXHIJKLMNOPQRSRUVWCYZ
ABCDYPXHIJKLMNOPQRSRUVWCYZ
What's wrong and what you want to do?
 
I tried this
Code:
$inString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$replaceString = '6,1,X;23,1,C;19,1,R;4,1,Y;5,1,P';

@replaceArray = split( ";", $replaceString);

foreach $elements (@replaceArray) {
  print "$inString\n";
  ($offset, $len, $replace) = split(",", $elements);
  print "\$offset=$offset, \$len=$len, \$replace=$replace\n";
  $ss=substr($inString, $offset, $len, $replace);
  print "\$ss=$ss\n";
  print "$inString\n";
  print "---------------------------------\n";
}
and got this output
Code:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
$offset=6, $len=1, $replace=X
$ss=G
ABCDEFXHIJKLMNOPQRSTUVWXYZ
---------------------------------
ABCDEFXHIJKLMNOPQRSTUVWXYZ
$offset=23, $len=1, $replace=C
$ss=X
ABCDEFXHIJKLMNOPQRSTUVWCYZ
---------------------------------
ABCDEFXHIJKLMNOPQRSTUVWCYZ
$offset=19, $len=1, $replace=R
$ss=T
ABCDEFXHIJKLMNOPQRSRUVWCYZ
---------------------------------
ABCDEFXHIJKLMNOPQRSRUVWCYZ
$offset=4, $len=1, $replace=Y
$ss=E
ABCDYFXHIJKLMNOPQRSRUVWCYZ
---------------------------------
ABCDYFXHIJKLMNOPQRSRUVWCYZ
$offset=5, $len=1, $replace=P
$ss=F
ABCDYPXHIJKLMNOPQRSRUVWCYZ
---------------------------------
i.e for the last part 5,1,P you get this
Code:
---------------------------------
ABCDY[COLOR=red]F[/color]XHIJKLMNOPQRSRUVWCYZ
$offset=5, $len=1, $replace=P
$ss=F
ABCDY[COLOR=blue]P[/color]XHIJKLMNOPQRSRUVWCYZ
---------------------------------
That means that you should replace from the original string the substring on position 5 with length 1 i.e 'F' with 'P'. And as you can see: it was done. I don't see a problem ...




 
mikrom,

Thanks for your input. Basically my code was to replace characters in the input string with the replace character I feed.

However for the ones where it did not work, it turns out that the input character was the same as the replace character. (With over 1^10x7 strings, I will have to come with some kind of sanity check before I the replacement.)

~Mika

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top