in reply to Removing digits until you see | in a string
$ perl -le ' > $str = q{703555121245874|45874 Smith St|Your Town|New Hampshire}; > chop( $index = substr( $str, 0, index( $str, q{|} ) + 1, q{} ) ); > print qq{$index\n$str};' 703555121245874 45874 Smith St|Your Town|New Hampshire $
The index( $str, q{|} ) + 1 finds the position one past the first pipe symbol, you then replace from start of string to that point with an empty string (4th argument) and substr returns what it has just replaced, which is assigned to $index but it will still have the pipe symbol at the end so use chop to remove the last character from the LHS.
I think frodo72's modification of friedo's update is the cleanest and easiest to understand of the solutions proposed.
Cheers,
JohnGG
|
|---|