Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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
    4. Cutting Variable
      Jeff 'Japhy' Pinyan wrote: Cool...that did the trick
    5. 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...
  3. #2

    Default Re: cutting a string

    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

    denis@teachlinux.com Guest

  4. #3

    Default Re: cutting a string

    On Wed, 3 Sep 2003 [email]denis@teachlinux.com[/email] wrote:
    > 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
    >
    >
    >
    FYI


    $test1 is not used.. was going to try the script another way and didn't..

    denis@teachlinux.com Guest

  5. #4

    Default 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

  6. #5

    Default Re: cutting a string


    --On Wednesday, September 03, 2003 9:20 PM -0600 [email]denis@teachlinux.com[/email] wrote:
    > 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.
    Well, that's what split() does. :-)
    >> 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";
    Easier (IMO) and portable:

    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

  7. #6

    Default 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:
    >
    > > 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]
    >


    sc00170@cc.uoi.gr Guest

  8. #7

    Default 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

    > 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]
    >
    >

    Freddy söderlund Guest

  9. #8

    Default 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:
    >
    > > basename is more convenient i think. What do you say?
    > >
    > Only if you can load the module on the system. running into issues here at
    > 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139