Ask a Question related to PERL Beginners, Design and Development.
-
Danny Miller #1
RE: cutting a string
As long as the format of what you want (file.txt) remains the same you
could do the following:
($stuffattheend) = $string =~ /\/(\w+\.\w+)$/;
Danny
-----Original Message-----
From: [email]sc00170@cc.uoi.gr[/email] [mailto:sc00170@cc.uoi.gr]
Sent: Monday, September 01, 2003 11:41 AM
To: Perl Beginners
Subject: cutting a string
What is the function of cutting a string from a point until the last
character?
For example
$string="C:/progra~1/directory1/directory2/file.txt";
i want to find the last backslash (/) of the string and keep the
sequence
following it (file.txt)
Is it simple?
I tried with the split function but it returns a list.
I just want a scalar!
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Danny Miller Guest
-
Text Cutting Off
I have a text box that users can type as much info in as they would like. The problem is for some users when they submit the form the content from... -
cutting items in a PDF
cutting items in a PDF I need to use something similar to an eraser to to rub out some defects in a scanned image. How does one go about doing... -
cutting a shape
new to freehand,imported a square with a round drawing on it. how do i select just the circle and cut it away from the square? thanks for your help -
Cutting Variable
Jeff 'Japhy' Pinyan wrote: Cool...that did the trick -
Cutting a picture
hello, i have taken a picture with my digital camera, it is my kid skateboarding, is it possible to just gut him out and save just him as an image... -
denis@teachlinux.com #2
Re: cutting a string
On Mon, 1 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
why not try this?> What is the function of cutting a string from a point until the last character?
>
> For example
> $string="C:/progra~1/directory1/directory2/file.txt";
>
> i want to find the last backslash (/) of the string and keep the sequence
> following it (file.txt)
>
> Is it simple?
>
> I tried with the split function but it returns a list.
>
> I just want a scalar!
>
>
>
>
#!/usr/bin/perl
my @test='';
my $test1='';
my $string = "C:/test/me";
@test = split('/',$string);
print "@test\n";
print "$test[$#test]\n";
HTH.. Denis
denis@teachlinux.com Guest
-
denis@teachlinux.com #3
Re: cutting a string
On Wed, 3 Sep 2003 [email]denis@teachlinux.com[/email] wrote:
FYI> On Mon, 1 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
>>> > What is the function of cutting a string from a point until the last character?
> >
> > For example
> > $string="C:/progra~1/directory1/directory2/file.txt";
> >
> > i want to find the last backslash (/) of the string and keep the sequence
> > following it (file.txt)
> >
> > Is it simple?
> >
> > I tried with the split function but it returns a list.
> >
> > I just want a scalar!
> >
> >
> >
> >
> why not try this?
>
> #!/usr/bin/perl
> my @test='';
> my $test1='';
> my $string = "C:/test/me";
> @test = split('/',$string);
> print "@test\n";
> print "$test[$#test]\n";
>
> HTH.. Denis
>
>
>
$test1 is not used.. was going to try the script another way and didn't..
denis@teachlinux.com Guest
-
Charles K. Clarkson #4
RE: cutting a string
denis <denis@teachlinux.com> offered this solution:
: why not try this?
:
: #!/usr/bin/perl
: my @test='';
: my $string = "C:/test/me";
: @test = split('/',$string);
: print "@test\n";
: print "$test[$#test]\n";
The last item of an array can be retrieved
using an index of -1.
print "$test[$#test]\n";
becomes
print "$test[-1]\n";
We don't need an array at all. We can ask split
to return one item:
@test = split('/',$string);
print "$test[-1]\n";
becomes
my $scalar = ( split '/', $string )[-1];
print "$scalar\n";
If we're going to do this a lot, we could write
a sub routine:
print strip_to_end( '/', 'C:/test/me' ), "\n";
sub strip_to_end {
my( $seperator, $string ) = @_;
return ( split $seperator, $string )[-1]
}
Then, if we find a faster method (like 'substr')
we can change the method without effecting the rest
of the program.
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
Charles K. Clarkson Guest
-
David Wall #5
Re: cutting a string
--On Wednesday, September 03, 2003 9:20 PM -0600 [email]denis@teachlinux.com[/email] wrote:
Well, that's what split() does. :-)> On Mon, 1 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
>>> What is the function of cutting a string from a point until the last
>> character?
>>
>> For example
>> $string="C:/progra~1/directory1/directory2/file.txt";
>>
>> i want to find the last backslash (/) of the string and keep the
>> sequence following it (file.txt)
>>
>> Is it simple?
>>
>> I tried with the split function but it returns a list.
#> my $test1='';>>> I just want a scalar!
>>
> why not try this?
>
># !/usr/bin/perl
> my @test='';Easier (IMO) and portable:> my $string = "C:/test/me";
> @test = split('/',$string);
> print "@test\n";
> print "$test[$#test]\n";
use File::Basename;
my $string="C:/progra~1/directory1/directory2/file.txt";
my $file = basename $string;
Then you don't need to worry about the directory separator, e.g.;
use File::Basename;
my @p = (
"C:/progra~1/directory1/directory2/file.txt",
'C:\progra~1\directory1\directory2\file.txt',
"C:\\progra~1\\directory1\\directory2\\file.tx t",
"C:/progra~1\\directory1\\directory2/file.txt"
);
print basename($_), "\n" for @p;
prints 'file.txt' four times.
David Wall Guest
-
sc00170@cc.uoi.gr #6
Re: cutting a string
basename is more convenient i think. What do you say?
Quoting [email]denis@teachlinux.com[/email]:
> On Mon, 1 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
>> character?> > What is the function of cutting a string from a point until the last>> >
> > For example
> > $string="C:/progra~1/directory1/directory2/file.txt";
> >
> > i want to find the last backslash (/) of the string and keep the sequence
> > following it (file.txt)
> >
> > Is it simple?
> >
> > I tried with the split function but it returns a list.
> >
> > I just want a scalar!
> >
> >
> >
> >
> why not try this?
>
> #!/usr/bin/perl
> my @test='';
> my $test1='';
> my $string = "C:/test/me";
> @test = split('/',$string);
> print "@test\n";
> print "$test[$#test]\n";
>
> HTH.. Denis
>
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
sc00170@cc.uoi.gr Guest
-
Freddy söderlund #7
Re: cutting a string
I think it would be easier to skip the split-function and use substr and
rindex insted. It's shorter code.
#!perl -w
my $path = "C:/program files/directory1/directory2/file.txt";
my $filename = substr($path,(rindex($path,"/")+1));
print $filename;
This code will give you "file.txt" as output.
Hope you will find it usefull.
/Freddy
----- Original Message -----
From: <denis@teachlinux.com>
To: <sc00170@cc.uoi.gr>
Cc: "Perl Beginners" <beginners@perl.org>
Sent: Thursday, September 04, 2003 5:20 AM
Subject: Re: cutting a string
character?> On Mon, 1 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
>> > What is the function of cutting a string from a point until the lastsequence> >
> > For example
> > $string="C:/progra~1/directory1/directory2/file.txt";
> >
> > i want to find the last backslash (/) of the string and keep the>> > following it (file.txt)
> >
> > Is it simple?
> >
> > I tried with the split function but it returns a list.
> >
> > I just want a scalar!
> >
> >
> >
> >
> why not try this?
>
> #!/usr/bin/perl
> my @test='';
> my $test1='';
> my $string = "C:/test/me";
> @test = split('/',$string);
> print "@test\n";
> print "$test[$#test]\n";
>
> HTH.. Denis
>
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
>
Freddy söderlund Guest
-
denis@teachlinux.com #8
Re: cutting a string
Sorry, lack of sleep.. but isn't File::Basename usally installed in the
standard Perl install?
Denis
On Fri, 5 Sep 2003 [email]denis@teachlinux.com[/email] wrote:
> On Thu, 4 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
>> Only if you can load the module on the system. running into issues here at> > basename is more convenient i think. What do you say?
> >
> work where I can load modules (security llama's)
>
> Denis
>>> >
> > Quoting [email]denis@teachlinux.com[/email]:
> >> >> > > On Mon, 1 Sep 2003 [email]sc00170@cc.uoi.gr[/email] wrote:
> > >
> > > > What is the function of cutting a string from a point until the last
> > > character?
> > > >
> > > > For example
> > > > $string="C:/progra~1/directory1/directory2/file.txt";
> > > >
> > > > i want to find the last backslash (/) of the string and keep the sequence
> > > > following it (file.txt)
> > > >
> > > > Is it simple?
> > > >
> > > > I tried with the split function but it returns a list.
> > > >
> > > > I just want a scalar!
> > > >
> > > >
> > > >
> > > >
> > >
> > > why not try this?
> > >
> > > #!/usr/bin/perl
> > > my @test='';
> > > my $test1='';
> > > my $string = "C:/test/me";
> > > @test = split('/',$string);
> > > print "@test\n";
> > > print "$test[$#test]\n";
> > >
> > > HTH.. Denis
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> > >
> >
> >
> >
>
>denis@teachlinux.com Guest



Reply With Quote

