string splitting question

E

Emily Beylor

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.
 
J

Jayaprakash Rudraraju

#!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.
 
K

Kris Wempa

Emily Beylor said:
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.
 
T

Tore Aursand

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. :)
 
E

Emily Baylor

Kris Wempa said:
Emily Beylor said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top