Re: String Length
by merlyn (Sage) on Apr 04, 2001 at 23:11 UTC
|
| [reply] |
|
|
I think your suggestion may work. I guess the best way to approach this problem would be to get the length and if the length = 3 then use chomp and then substring the numeric charcter out of the string and pass it to a variable. Then the same would hold true for other similar situations??
TIA-
ep
| [reply] |
|
|
actually, you should use chop twice.
chomp only eats end of line characters (default are "\r\n").
chop eats whatever is there.
Although I really think you should use a regexp instead, in case the last couple characters aren't "ST".
| [reply] |
|
|
|
|
|
|
(dws - rant)Re: String Length
by dws (Chancellor) on Apr 05, 2001 at 04:17 UTC
|
<rant volume="low">
Folks, this node is a good example of how we often take a vague statement of requirements and jump right in to solutions before we're clear on the problem that we're trying to solve. (I'm guilty here, too.)
Take a moment to look at the problem statement we were given to work with. Does it warrant the solutions that have been proposed, or might it be more appropriate to get clarifications before proceeding to code.
Here's what we were given to work with:
- Find the length of a string.
- Remove the last two characters from the string.
- Save the remaining character to a variable.
We can drop item 1) as being a non-requirement. It's really one step towards a possible implementation of 2)
Next we're given an example:
If I had the following strings: "1ST", "21ST" and "101ST", I would like to remove the "ST" and retain the numbers.
At this point we should stand back and ask:
- Will all input be leading digits plus a two character suffix?
- Will the suffix always be "ST"? What about "st"? What about "TH"?
- What should we do with input that isn't leading digits plus a two character suffix?
Only after we get answers to theses are we really in a position to start suggesting implementations. To do otherwise risks having the problem come back to us n more times, each time revealing a new requirement that our solution didn't satisfy.
</rant> | [reply] [d/l] [select] |
|
|
If I was in possesion of enough chits to vote on posts, I'd definately ++ this. But seeing as though I haven't earned my voting badge yet, I'll just have to resort to saying that you're %100 correct. -Rich
| [reply] |
|
|
Actually, I'm a huge fan of diving in with code before the problem has been specified. I quite enjoy deliberately misinterpreting the newbies question and posting an irrelevant chunk of code.
$processing->further(s/T"$//);
I also think this is a great way for the newbies to learn to clarify their questions!
ep: the lesson here is that at least half of your job is understanding the project's requirements. If the Monks demonstrate how they get nowhere by misunderstanding your requirements, then maybe you'll learn that you will get nowhere until you understand your client's requirements.
| [reply] [d/l] |
|
|
1) No,we cannot assume that the input will be leading digits plus a two char. suff. ... we can only hope!
2) Hopefully, the suffix will be "ST" or "TH" or "RD", but we cant be assured of case or it meet the criteria above.
3)Input that is not leading digits plus a two character suffix is junk and therefore not needed!
Hope this helps-
ep
| [reply] |
|
|
| [reply] |
Re: String Length
by cLive ;-) (Prior) on Apr 04, 2001 at 23:39 UTC
|
# one way;
my $day = '1ST';
my $num = substr ($day, 0, -2)
print $num;
# another, more fun way, if doing to $_;
chop;
chop;
print;
# so to print out a list
for (@days) {
print substr($_, 0, -2)."\n";
}
cLive ;-) | [reply] [d/l] |
|
|
As long as you're doing that, why not grab both the number
and the suffix? It's not critical for this example, but
as long as we're being picky...
my $day = '1ST';
my $suffix = substr($day, -2, 2, '');
print $day; # "1"
print $suffix; # "ST"
buckaduck | [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
|
Re: String Length
by thabenksta (Pilgrim) on Apr 04, 2001 at 23:15 UTC
|
$string =~ s/(.*).{2}$/$1/;
my $name = 'Ben Kittrell';
$name=~s/^(.+)\s(.).+$/\L$1$2/g;
my $nick = 'tha' . $name . 'sta'; | [reply] [d/l] |
Re: String Length
by dws (Chancellor) on Apr 04, 2001 at 23:14 UTC
|
Assuming that you want to remove a trailing "ST", use a regex. This'll protect you against the day when, unexpectedly, something other than "ST" sneaks in to your input stream.
($stripped = $string) =~ s/ST$//; | [reply] [d/l] |
Re: String Length
by domo (Initiate) on Apr 05, 2001 at 14:39 UTC
|
Just so long as we're beating this issue to a pulp...
$var =~ tr/0-9//dc;
zaps non-digits (any non-digits, not just letters) wherever they appear in $var. | [reply] [d/l] |
Re: String Length
by Anonymous Monk on Apr 04, 2001 at 23:20 UTC
|
Here is an addendum to the problem I just submitted. The last two characters are variable...that is they could be "ST" or "RD" or "TH". Maybe this will help a little bit...although I think Merlyn's suggestion will get me what I want...I think?
TIA-
ep | [reply] |
Re: String Length
by rchiav (Deacon) on Apr 05, 2001 at 00:05 UTC
|
if you know that the return values are always going to be numeric, you can do this..
/^(\d+)/;
print $1;
or to remove the text..
s/^(\d+).*/\1/;
print;
The last example will output the actual string if it doesn't match the regex though. The key will be to verify that you're only giving it what it's supposed to be reveiving..
Rich | [reply] [d/l] [select] |
Re: String Length
by Daddio (Chaplain) on Apr 05, 2001 at 02:18 UTC
|
If you are looping through using an array for each of the values you mention, you could do something like this:
@array = ('1ST','10ST','101ST');
foreach (@array) {
($start,$end) = m/(.*)(.{2})/o;
print "start = $start\nend = $end\n\n";
}
That would give you two values, upon which further processing would be possible. | [reply] [d/l] |
Re: String Length
by dmckee (Scribe) on Apr 06, 2001 at 00:26 UTC
|
It needs to be said: There's More than One Way to Do it! TMTOWDI!
Dave
Eight years involved with the nuclear industry have taught me that when nothing can possible go wrong and every avenue has been covered, then is the time to buy a house on the next continent. Terry Pratchett
Edit 2001-03-05 by tye to close <small> | [reply] |
Re: String Length
by Anonymous Monk on Apr 05, 2001 at 10:52 UTC
|
HI,
Try to use the splice function.
removed = splice(array, offset, length, list) | [reply] |