[9480] in Perl-Users-Digest
Perl-Users Digest, Issue: 3072 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 6 21:18:06 1998
Date: Mon, 6 Jul 98 18:07:09 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 6 Jul 1998 Volume: 8 Number: 3072
Today's topics:
Re: regexp s/// for removing tail end of string (John Moreno)
Re: regexp s/// for removing tail end of string <eugene@verticalnet.com>
Re: regexp s/// for removing tail end of string (Larry Rosler)
Re: regexp s/// for removing tail end of string (Larry Rosler)
Re: regexp s/// for removing tail end of string <dgris@rand.dimensional.com>
Re: regexp s/// for removing tail end of string (Larry Rosler)
Re: regexp s/// for removing tail end of string <quentin@shaddam.amd.com>
Re: regexp s/// for removing tail end of string (Abigail)
Re: regexp s/// for removing tail end of string (Larry Rosler)
Re: repost perl asp question (correct e-mail adress) <kenny@weng.dk>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 06 Jul 1998 02:01:02 GMT
From: phenix@interpath.com (John Moreno)
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <1dbpglv.n8d1bu1737wxoN@roxboro0-026.dyn.interpath.net>
Jerry <jerry@fitzweb.com> wrote:
> Hi there, hope someone can give me a quick hack to do this.
>
> I have a string that is a full path to a file, i.e.
> dir/subdir/subsubdir/file.ext What I want to do is strip off the file name,
> leaving just the path info (with the final / ) in a string. The paths will
> have a varying number of directories, so I don't think I can use split to
> just grab all the dirs, throw out the last one, and join the remainder
> together. (If this is the way to go, please show me I can't see it).
>
> I tried substitution, using this:
> $path =~ s/\/.*?$//;
> but it strips off everything after the first '/'. I thought the '.*?' would
> give me the minimal match, and the '$' would make the match start at the
> end of the string, but I'm wrong.
>
> Checked the FAQ, newsgroup, manpages, several books. All to no avail. I
> know I'm missing something elementarily simple....
Try $path =~ s/(.+\/)/$1/ or $path =~ s/[^\/]+$// - the ? isn't doing
what you think.
--
John Moreno
------------------------------
Date: Mon, 06 Jul 1998 00:08:52 -0400
From: Eugene Sotirescu <eugene@verticalnet.com>
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <35A04DD3.BF636974@verticalnet.com>
And if you'd rather not use regexps for this:
-----------------------------
#!/usr/local/bin/perl -w
# basename.pl
use File::Basename;
$fullname = "/usr/home/eugene/bin/basename.pl/";
print ($dirname = dirname($fullname) . "/");
------------------------------
Prints: /usr/home/eugene/bin/
Jerry wrote:
> Hi there, hope someone can give me a quick hack to do this.
>
> I have a string that is a full path to a file, i.e.
> dir/subdir/subsubdir/file.ext What I want to do is strip off the file name,
> leaving just the path info (with the final / ) in a string. The paths will
> have a varying number of directories, so I don't think I can use split to
> just grab all the dirs, throw out the last one, and join the remainder
> together. (If this is the way to go, please show me I can't see it).
>
> I tried substitution, using this:
> $path =~ s/\/.*?$//;
> but it strips off everything after the first '/'. I thought the '.*?' would
> give me the minimal match, and the '$' would make the match start at the
> end of the string, but I'm wrong.
>
> Checked the FAQ, newsgroup, manpages, several books. All to no avail. I
> know I'm missing something elementarily simple....
>
> TIA,
> Fitz
------------------------------
Date: Sun, 5 Jul 1998 23:22:55 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <MPG.100a0d1bfec9379f989718@nntp.hpl.hp.com>
In article <6norj5$afs$1@news.iquest.net> on Sun, 5 Jul 1998 16:35:34 -
0500, Jerry <jerry@fitzweb.com> says...
> Hi there, hope someone can give me a quick hack to do this.
>
> I have a string that is a full path to a file, i.e.
> dir/subdir/subsubdir/file.ext What I want to do is strip off the file name,
> leaving just the path info (with the final / ) in a string. The paths will
> have a varying number of directories, so I don't think I can use split to
> just grab all the dirs, throw out the last one, and join the remainder
> together. (If this is the way to go, please show me I can't see it).
Several people have already focused on the regex approach (which I think
is cleanest), but I thought to show you the split-and-join anyhow:
$_ = 'dir/subdir/subsubdir/file.ext';
my @a = split /(\/)/;
my $out = join "", @a[0 .. $#a - 1];
A couple of variations on this approach:
my @a = split /(\/)/;
pop @a;
my $out = join "", @a;
my @a = split /\//;
my $out = join '/', @a[0 .. $#a - 1], "";
I hope someone with greater skills than I will show how to do this
efficiently as a "one-liner" (i.e., without using a named temporary
array).
join "", (split /(\/)/)[0 .. -2]
would be very nice, but that's not how '..' works.
--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 6 Jul 1998 00:17:10 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <MPG.100a19d066fdea0e989719@nntp.hpl.hp.com>
In article <MPG.100a0d1bfec9379f989718@nntp.hpl.hp.com> on Sun, 5 Jul
1998 23:22:55 -0700, Larry Rosler <lr@hpl.hp.com> says...
...
> my @a = split /\//;
> my $out = join '/', @a[0 .. $#a - 1], "";
>
> I hope someone with greater skills than I will show how to do this
> efficiently as a "one-liner" (i.e., without using a named temporary
> array).
...
I came up with this later, using a ref to an anonymous array:
my $p;
my $out = join "", splice @{$p = [split /(\/)/]}, 0, $#$p;
Bizarre! Now to get rid of $p ...
--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 06 Jul 1998 07:28:24 GMT
From: Daniel Grisinger <dgris@rand.dimensional.com>
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <6nptph$o9c$1@rand.dimensional.com>
[posted and mailed to the cited author]
In article <MPG.100a0d1bfec9379f989718@nntp.hpl.hp.com>
lr@hpl.hp.com (Larry Rosler) wrote:
>I hope someone with greater skills than I will show how to do this
>efficiently as a "one-liner" (i.e., without using a named temporary
>array).
I don't think I qualify as having `greater skills', but here it is
without a named temp.
print join '/', (map {pop @$_; @$_;} [split /\//]), "\n";
Daniel
--
Daniel Grisinger dgris@perrin.dimensional.com
"No kings, no presidents, just a rough consensus and
running code."
Dave Clark
------------------------------
Date: Mon, 6 Jul 1998 01:04:36 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <MPG.100a24eacd3b7ff398971a@nntp.hpl.hp.com>
[This followup was posted to comp.lang.perl.misc and a copy was sent to
the cited author.]
In article <6nptph$o9c$1@rand.dimensional.com> on Mon, 06 Jul 1998
07:28:24 GMT, Daniel Grisinger <dgris@rand.dimensional.com> says...
> [posted and mailed to the cited author]
> In article <MPG.100a0d1bfec9379f989718@nntp.hpl.hp.com>
> lr@hpl.hp.com (Larry Rosler) wrote:
>
> >I hope someone with greater skills than I will show how to do this
> >efficiently as a "one-liner" (i.e., without using a named temporary
> >array).
>
> I don't think I qualify as having `greater skills', but here it is
> without a named temp.
> print join '/', (map {pop @$_; @$_;} [split /\//]), "\n";
Gasp! Treat a ref to an anonymous array as a one-element list, use 'map'
to get the ref into $_, then pop and dereference it to get the truncated
list for 'join'. (My slice or splice approaches would have worked too,
once one captured the name of the array as @$_ -- that is the trick.)
Twistier than I would have dreamed. I knew I would learn some more
"outside the box" Perl-think from pushing this question. Thanks.
--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 06 Jul 1998 11:11:50 -0500
From: Quentin Fennessy <quentin@shaddam.amd.com>
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <ximd8bj5589.fsf@shaddam.amd.com>
>>>>> "Jerry" == Jerry <jerry@fitzweb.com> writes:
Jerry> I have a string that is a full path to a file, i.e.
Jerry> dir/subdir/subsubdir/file.ext What I want to do is strip
Jerry> off the file name, leaving just the path info (with the
Jerry> final / ) in a string.
Try this:
s|[^/]*$||
This deletes all non-/ characters before the end of line.
--
Quentin Fennessy AMD, Austin Texas
Secret hacker rule #11 - hackers read manuals
------------------------------
Date: 6 Jul 1998 19:07:21 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <6nr799$o1q$1@client3.news.psi.net>
Larry Rosler (lr@hpl.hp.com) wrote on MDCCLXX September MCMXCIII in
<URL: news:MPG.100a0d1bfec9379f989718@nntp.hpl.hp.com>:
++ In article <6norj5$afs$1@news.iquest.net> on Sun, 5 Jul 1998 16:35:34 -
++ 0500, Jerry <jerry@fitzweb.com> says...
++ > Hi there, hope someone can give me a quick hack to do this.
++ >
++ > I have a string that is a full path to a file, i.e.
++ > dir/subdir/subsubdir/file.ext What I want to do is strip off the file name,
++ > leaving just the path info (with the final / ) in a string. The paths will
++ > have a varying number of directories, so I don't think I can use split to
++ > just grab all the dirs, throw out the last one, and join the remainder
++ > together. (If this is the way to go, please show me I can't see it).
++
++ Several people have already focused on the regex approach (which I think
++ is cleanest), but I thought to show you the split-and-join anyhow:
++
++ $_ = 'dir/subdir/subsubdir/file.ext';
++
++ my @a = split /(\/)/;
++ my $out = join "", @a[0 .. $#a - 1];
++
++ A couple of variations on this approach:
++
++ my @a = split /(\/)/;
++ pop @a;
++ my $out = join "", @a;
++
++ my @a = split /\//;
++ my $out = join '/', @a[0 .. $#a - 1], "";
++
++ I hope someone with greater skills than I will show how to do this
++ efficiently as a "one-liner" (i.e., without using a named temporary
++ array).
++ join "", (split /(\/)/)[0 .. -2]
++ would be very nice, but that's not how '..' works.
++
my $out = sub {split m{/}; pop; join '/', @_} -> ();
Abigail
--
Then again, @_ is named....
------------------------------
Date: Mon, 6 Jul 1998 13:14:46 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: regexp s/// for removing tail end of string
Message-Id: <MPG.100ad012c8f93ac59896e9@nntp.hpl.hp.com>
[This followup was posted to comp.lang.perl.misc and a copy was sent to
the cited author.]
In article <6nr799$o1q$1@client3.news.psi.net> on 6 Jul 1998 19:07:21
GMT, Abigail <abigail@fnx.com> says...
...
> my $out = sub {split m{/}; pop; join '/', @_} -> ();
>
> Abigail
> --
> Then again, @_ is named....
Perhaps I should have said 'declared explicitly'. :-)
With '-w', this code produces two identical warnings:
Use of implicit split to @_ is deprecated at ... line ...
Use of implicit split to @_ is deprecated at ... line ...
This is perl, version 5.004_03
[The omission of the trailing slash requested in the original submission
is trivial to fix, of course.]
--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 6 Jul 1998 12:24:14 +0200
From: "Weng" <kenny@weng.dk>
Subject: Re: repost perl asp question (correct e-mail adress)
Message-Id: <6nq8g8$rau$1@news-inn.inet.tele.dk>
I have had the same problem when trying to open the file with the
OpenTextFile
The using the MapPath, this works.
Mvh
Kenny Weng
----------------------------------------------------------------------
E-mail : kenny@weng.dk
----------------------------------------------------------------------
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 3072
**************************************