Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string manipulation

Reply
Thread Tools

string manipulation

 
 
Xicheng
Guest
Posts: n/a
 
      01-27-2006
Paul Lalli wrote:
> Xicheng wrote:
> > a wrote:
> > > Also, I have a directory as an input.
> > > e.g //server/dirA/dirB/
> > > How can I check the last character is a / or not?

> > if $str =~ m{/$} {
> > #the last char is /
> > }

>
> no need to invoke the regexp engine for such a simple task. (Oh, and
> what you typed is a syntax error).

yep, you are right, I forgot to add parenthesis to the 'if' clause

Xicheng
> if (substr($str, -1) eq '/') {
> print "Last char is a slash\n";
> }
>
> Paul Lalli


 
Reply With Quote
 
 
 
 
DJ Stunks
Guest
Posts: n/a
 
      01-27-2006
a wrote:
> <first question snipped>
>
> Also, I have a directory as an input.
> e.g //server/dirA/dirB/
> How can I check the last character is a / or not?
> Thanks a lot


I assume you only want to check for that trailing slash in order to
know whether or not it's actually a directory, so just test for that.

print "$_ is a directory" if ( -d );

-jp

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-27-2006
a <(E-Mail Removed)> wrote:

> How can I check the last character is a / or not?



print "last is a slash\n" if substr($path, -1) eq '/';


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-28-2006
James Taylor <(E-Mail Removed)> wrote:

> my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

^
^

That anchor serves no useful purpose, so it probably shouldn't be there.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      01-28-2006
Tad McClellan <(E-Mail Removed)> wrote:

> James Taylor <(E-Mail Removed)> wrote:
>
> > my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

> ^
> ^
>
> That anchor serves no useful purpose, so it probably shouldn't be there.


My newsreader doesn't have a monospaced font (I must fix that) so your
caret appears to point to the dollar singin front of $path. I must
assume though that you were pointing at the beginning of line anchor in
my regex. The reason it is there is to ensure that the match fails in
linear time if there is no slash in the $path.

--
James Taylor
 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      01-29-2006
James Taylor schrieb:
> Tad McClellan <(E-Mail Removed)> wrote:
>
>
>>James Taylor <(E-Mail Removed)> wrote:
>>
>>
>>>my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

>>
>> ^
>> ^
>>
>>That anchor serves no useful purpose, so it probably shouldn't be there.

>
> The reason it is there is to ensure that the match fails in
> linear time if there is no slash in the $path.
>


However, for strings without slashes, the pattern with the anchor will
be (a little) slower than without the anchor.

(If anybody want's to know )


use warnings;
use strict;
use Benchmark qw(cmpthese) ;

my $string = "a_string_with_or_without_slashes";
my $string2 = "a_string_with_or/without_slashes";
cmpthese(1000000, {
'anchor' => sub{my($foo,$bar)=$string =~ m|^(.*)/(.*)|},
'no_anchor' => sub{my($foo,$bar)=$string =~ m|(.*)/(.*)|},
'an+/' => sub{my($foo,$bar)=$string2 =~ m|^(.*)/(.*)|},
'no_an+/' => sub{my($foo,$bar)=$string2 =~ m|(.*)/(.*)|} });


prints:

Rate no_an+/ an+/ anchor no_anchor
no_an+/ 275028/s -- -1% -83% -84%
an+/ 278164/s 1% -- -83% -84%
anchor 1636661/s 495% 488% -- -5%
no_anchor 1721170/s 526% 519% 5% --

Btw: Is there an expression like 'peacounting' in english ??

Christoph
--
please reply to

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      01-29-2006
James Taylor schrieb:
> Tad McClellan <(E-Mail Removed)> wrote:
>
>
>>James Taylor <(E-Mail Removed)> wrote:
>>
>>
>>>my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

>>
>> ^
>> ^
>>
>>That anchor serves no useful purpose, so it probably shouldn't be there.

>
> The reason it is there is to ensure that the match fails in
> linear time if there is no slash in the $path.
>


However, for strings without slashes, the pattern with the anchor will
be (a little) slower than without the anchor.

(If anybody want's to know )


use warnings;
use strict;
use Benchmark qw(cmpthese) ;

my $string = "a_string_with_or_without_slashes";
my $string2 = "a_string_with_or/without_slashes";
cmpthese(1000000, {
'anchor' => sub{my($foo,$bar)=$string =~ m|^(.*)/(.*)|},
'no_anchor' => sub{my($foo,$bar)=$string =~ m|(.*)/(.*)|},
'an+/' => sub{my($foo,$bar)=$string2 =~ m|^(.*)/(.*)|},
'no_an+/' => sub{my($foo,$bar)=$string2 =~ m|(.*)/(.*)|} });


prints:

Rate no_an+/ an+/ anchor no_anchor
no_an+/ 275028/s -- -1% -83% -84%
an+/ 278164/s 1% -- -83% -84%
anchor 1636661/s 495% 488% -- -5%
no_anchor 1721170/s 526% 519% 5% --

Btw: Is there an expression like 'peacounting' in english ??

Christoph
--
please reply to

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      01-29-2006
Ch Lamprecht <(E-Mail Removed)> wrote:

> Btw: Is there an expression like 'peacounting' in english ??


Yes, almost the same, it's "bean counting".

--
James Taylor
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments