string splitting question

Discussion in 'Perl Misc' started by Emily Beylor, Oct 10, 2003.

  1. Emily Beylor

    Emily Beylor Guest

    I have a path string (/usr/local/bin/file.10102003) that I want to split
    into a $path and $file section as well as a $date section.

    I'm able to get the $path and $file information (perhaps not very elegantly,
    but still) as follows:

    while(/\//g) {
    $position = pos();
    }

    $getFile = substr($_, $position, length($_));
    $getPath = substr($_, 0, $position);

    I'm not sure how to get the $date information from the $file name. I've
    tried with the following code, but it doesn't work:

    $_=$getFile;
    @a = m/([\d]+)/g;
    print "@a\n";


    How do I get the date information out of vi.10102003 to a representation of
    $date = 10/10/2003 ?

    Thanks

    EB.
    Emily Beylor, Oct 10, 2003
    #1
    1. Advertising

  2. #!perl

    while(<DATA>) {
    /([^\/]*)$/;
    ($path, $file, $date) = ($`, split(/\./, $1));
    print "$path $file ", join ('/', $date =~ /^(\d\d)(\d\d)(\d+)$/);
    }

    __DATA__
    /usr/local/bin/file.10102003



    12:40pm, IP packets from Emily Beylor delivered:

    > I have a path string (/usr/local/bin/file.10102003) that I want to split
    > into a $path and $file section as well as a $date section.
    >
    > I'm able to get the $path and $file information (perhaps not very elegantly,
    > but still) as follows:
    >
    > while(/\//g) {
    > $position = pos();
    > }
    >
    > $getFile = substr($_, $position, length($_));
    > $getPath = substr($_, 0, $position);
    >
    > I'm not sure how to get the $date information from the $file name. I've
    > tried with the following code, but it doesn't work:
    >
    > $_=$getFile;
    > @a = m/([\d]+)/g;
    > print "@a\n";
    >
    >
    > How do I get the date information out of vi.10102003 to a representation of
    > $date = 10/10/2003 ?
    >
    > Thanks
    >
    > EB.
    >
    >
    >


    --
    echo | perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'
    T h i s t a g l i n e h a s b e e n u n z i p p e d .
    Jayaprakash Rudraraju, Oct 10, 2003
    #2
    1. Advertising

  3. Emily Beylor

    Kris Wempa Guest

    "Emily Beylor" <> wrote in message
    news:bm6qse$...
    > I have a path string (/usr/local/bin/file.10102003) that I want to split
    > into a $path and $file section as well as a $date section.
    >
    > I'm able to get the $path and $file information (perhaps not very

    elegantly,
    > but still) as follows:
    >
    > while(/\//g) {
    > $position = pos();
    > }
    >
    > $getFile = substr($_, $position, length($_));
    > $getPath = substr($_, 0, $position);
    >


    This is a lot of unnecessary work. Assuming your full path filename is in
    $str , you can use this:

    if ($str =~ /\/([^\/]+)$/) {
    $filename = $1; # file is everything BETWEEN the first set of
    parentheses
    $dirname = $`; # dir is everything BEFORE the matched pattern
    }

    > I'm not sure how to get the $date information from the $file name. I've
    > tried with the following code, but it doesn't work:
    >
    > $_=$getFile;
    > @a = m/([\d]+)/g;
    > print "@a\n";
    >
    >
    > How do I get the date information out of vi.10102003 to a representation

    of
    > $date = 10/10/2003 ?


    Using the results stored in $filename, just do something like this:

    if ($filename =~ /(\d\d)(\d\d)(\d\d\d\d)/) {
    $date = "$1/$2/$3";
    }

    I'm sure there's simpler ways, but this comes to mind quickly.
    Kris Wempa, Oct 10, 2003
    #3
  4. Tina Mueller <usenet@expires12.2003.tinita.de> wrote:

    > Emily Beylor wrote:
    >> I have a path string (/usr/local/bin/file.10102003) that I want
    >> to split into a $path and $file section as well as a $date
    >> section.

    >
    > as another suggestion: to make this portable, you might want to
    > have a look at File::Spec and splitpath()


    Or File::Basename
    David K. Wall, Oct 10, 2003
    #4
  5. Emily Beylor

    Tore Aursand Guest

    On Fri, 10 Oct 2003 12:09:13 -0700, Jayaprakash Rudraraju wrote:
    > while(<DATA>) {
    > /([^\/]*)$/;
    > ($path, $file, $date) = ($`, split(/\./, $1));
    > print "$path $file ", join ('/', $date =~ /^(\d\d)(\d\d)(\d+)$/);
    > }


    Excellent, indeed. I would have, however chosen a _slightly_ different
    approach.

    Wouldn't it be more "foolproof" to use File::Basename's fileparse() method
    to get the a) path, b) filename, and c) the suffix (ie. the date)?

    In that case;

    #!/usr/bin/perl
    #
    use strict;
    use warnings;
    use File::Basename qw( fileparse );

    while ( <DATA> ) {
    my ($path, $filename, $suffix) = fileparse( $_ );
    my $date = join('/', $suffix =~ /^(\d{2})(\d{2})(\d+)$/ );
    }

    Just my thought of using already existing modules, and in this case be
    sure that your code works even when being run on an obscure OS. :)


    --
    Tore Aursand <>
    Tore Aursand, Oct 10, 2003
    #5
  6. Emily Beylor

    Emily Baylor Guest

    "Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message news:<bm7093$>...
    > "Emily Beylor" <> wrote in message
    > news:bm6qse$...
    > > I have a path string (/usr/local/bin/file.10102003) that I want to split
    > > into a $path and $file section as well as a $date section.
    > >
    > > I'm able to get the $path and $file information (perhaps not very

    > elegantly,
    > > but still) as follows:
    > >
    > > while(/\//g) {
    > > $position = pos();
    > > }
    > >
    > > $getFile = substr($_, $position, length($_));
    > > $getPath = substr($_, 0, $position);
    > >

    >
    > This is a lot of unnecessary work. Assuming your full path filename is in
    > $str , you can use this:
    >
    > if ($str =~ /\/([^\/]+)$/) {
    > $filename = $1; # file is everything BETWEEN the first set of
    > parentheses
    > $dirname = $`; # dir is everything BEFORE the matched pattern
    > }
    >
    > > I'm not sure how to get the $date information from the $file name. I've
    > > tried with the following code, but it doesn't work:
    > >
    > > $_=$getFile;
    > > @a = m/([\d]+)/g;
    > > print "@a\n";
    > >
    > >
    > > How do I get the date information out of vi.10102003 to a representation

    > of
    > > $date = 10/10/2003 ?

    >
    > Using the results stored in $filename, just do something like this:
    >
    > if ($filename =~ /(\d\d)(\d\d)(\d\d\d\d)/) {
    > $date = "$1/$2/$3";
    > }
    >
    > I'm sure there's simpler ways, but this comes to mind quickly.


    Kris,

    Thanks. It's funny how there are so many ways of doing perl.

    EB
    Emily Baylor, Oct 13, 2003
    #6
  7. Emily Beylor

    Anno Siegel Guest

    Emily Baylor <> wrote in comp.lang.perl.misc:

    [snip]

    > Thanks. It's funny how there are so many ways of doing perl.


    Hehe! Can you say TIMTOWTDI?

    Anno
    Anno Siegel, Oct 13, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Splitting A String

    VB Programmer, Feb 1, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    321
    VB Programmer
    Feb 1, 2006
  2. John Ericson
    Replies:
    0
    Views:
    423
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    439
  4. John Dibling
    Replies:
    0
    Views:
    411
    John Dibling
    Jul 19, 2003
  5. Replies:
    3
    Views:
    649
Loading...

Share This Page