Re: howto strip the first char of a string?
by gellyfish (Monsignor) on Sep 08, 2004 at 13:23 UTC
|
Those are both perfectly reasonable ways of removing the first character of a string. Other far less reasonable ways might include:
my @foo = split //;
shift @foo;
$_ = join '', @foo;
or
my $foo = reverse($_);
chop($foo);
$_ = reverse($foo);
or
$_ = unpack "xA*", $_;
and so forth.
/J\ | [reply] [Watch: Dir/Any] [d/l] |
|
Excellent! Could anti-golf be the new golf?
| [reply] [Watch: Dir/Any] |
|
Notwithstanding gellyfish's excellent diversion, there is no "longest" way to do it. You can always add another semi-colon.
On the other hand, once you whittle a solution down to the empty string, I'm reasonably sure* that nothing shorter will show up.
And for the longest contest, after the first few TB solutions, we'll all grow tired of waiting for the latest entry to download.
* Solutions by TheDamian excepted.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: howto strip the first char of a string?
by thospel (Hermit) on Sep 08, 2004 at 13:26 UTC
|
In perl there are rarely recommended ways. Do what makes sense for whatever your exact situation is. The main issue will probably be if you want to change an existing string inplace or make a new string. And your ideas are fine for that.
New string:
$new = substr $old, 1
Modify old string:
substr($str, 0, 1) = ""
or
substr $str, 0, 1, ""
or
$str =~ s/^.//s
(notice the s modifier, which is needed if you want the code to also work if the first character is a \n. You may want to replace the ^ with \A for extra paranoia)
The other main point to consider is what you want to happen if the original string is empty. Error ? Warning ? Silence and just give the empty string as result ? All solutions are easily modified for whatever you want there. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: howto strip the first char of a string?
by Anonymous Monk on Sep 08, 2004 at 13:25 UTC
|
substr $_, 0, 1 = "";
or substr $_, 0, 1, "";
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
perl -lpe 'substr $_, 0, 1 = "";'
Can't modify constant item in scalar assignment at -e line 1, near """
+;"
Execution of -e aborted due to compilation errors.
perl -lpe 'substr $_, 0, 1, "";'
this is
his is
The first one will work with a minor tweak
perl -lpe 'substr($_, 0, 1) = "" '
test
est
Cheers, R. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: howto strip the first char of a string?
by Aristotle (Chancellor) on Sep 08, 2004 at 22:03 UTC
|
s/^.//;
What if the first character is a newline? :-) Don't forget the /s! As an aside, FWIW, the s/// is much less efficient than the substr solution here.
Another overengineered way:
use Fcntl qw( :seek );
$_ = "12345";
{
open my $fh, "<", \$_;
seek $fh, 1, SEEK_SET;
local $/;
$_ = <$fh>;
}
Makeshifts last the longest.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: howto strip the first char of a string?
by herveus (Prior) on Sep 08, 2004 at 13:34 UTC
|
Howdy!
Well, there's always chop reverse; reverse;
| [reply] [Watch: Dir/Any] [d/l] |
|
Did you try running that? Perhaps you meant the pseudo-code reverse; chop; reverse; which spelled out in Perl would be $_ = reverse; chop; $_ = reverse;?
If not, I don't understand how you'd expect chop reverse; reverse; to do anything useful.
ihb
Read argumentation in its context!
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Well, in the insane-o universe where reverse was an lvalue, chop reverse is all you'd need. That, however is certainly not the case. It's funny just to think, though, about someone implementing an lvalue reverse function... can you lvalue a tied scalar? I should look into that :-p
------------
:Wq
Not an editor command: Wq
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
Re: howto strip the first char of a string?
by Anonymous Monk on Sep 08, 2004 at 14:29 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: howto strip the first char of a string?
by QM (Parson) on Sep 08, 2004 at 14:33 UTC
|
I find a pencil sharpener to work in many cases. But if you crank too many times, you'll end up just holding the eraser.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] [Watch: Dir/Any] |
Re: howto strip the first char of a string?
by valentin (Abbot) on Sep 08, 2004 at 15:35 UTC
|
Thank you all, for that much insperation! | [reply] [Watch: Dir/Any] |
Re: howto strip the first char of a string?
by ambrus (Abbot) on Apr 11, 2006 at 14:27 UTC
|
Here is a list of threads on choping the first character of strings; thanks to Limbic~Region for finding them.
| [reply] [Watch: Dir/Any] |