Yet another way, with
chop,
substr and
index. I'm not suggesting this is a sensible way to do it but in the spirit of TIMTOWTDI
$ 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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.