Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string splitting question

Reply
Thread Tools

string splitting question

 
 
Emily Beylor
Guest
Posts: n/a
 
      10-10-2003
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.


 
Reply With Quote
 
 
 
 
Jayaprakash Rudraraju
Guest
Posts: n/a
 
      10-10-2003


#!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 http://www.velocityreviews.com/forums/(E-Mail Removed) | 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 .
 
Reply With Quote
 
 
 
 
Kris Wempa
Guest
Posts: n/a
 
      10-10-2003

"Emily Beylor" <(E-Mail Removed)> wrote in message
news:bm6qse$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      10-10-2003
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

 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      10-10-2003
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 <(E-Mail Removed)>
 
Reply With Quote
 
Emily Baylor
Guest
Posts: n/a
 
      10-13-2003
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote in message news:<bm7093$(E-Mail Removed)>...
> "Emily Beylor" <(E-Mail Removed)> wrote in message
> news:bm6qse$(E-Mail Removed)...
> > 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-13-2003
Emily Baylor <(E-Mail Removed)> 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
 
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
Splitting Strings in a map<string, string> byte8bits@gmail.com C++ 3 04-15-2008 06:24 PM
Question: Splitting A String VB Programmer ASP .Net 2 02-01-2006 10:07 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments