Perl script to process file list....

J

Jim Carter

Hi all,

Below is the issue I am working on in Windows 2000.

I have directory, C:\Carter and it it has around 100 file names (ex:
test1.txt, test2.txt, test3.txt......test100.txt).

1. First, I need another list with all these 100 file names with no
".txt" extension at the end (ex: test1, test2,test3, ....test100).
2. Then I want these two lists as key and value pair in a hash.
(EX: test1, test1.txt, test2, test2.txt, test3, test3.txt, ......,
test100, test100.txt).

I am struggling with the code to get the final hash. Can some one help
me out?

Thanks,
Carter
 
M

Matt Garrish

Jim Carter said:
Hi all,

Below is the issue I am working on in Windows 2000.

I have directory, C:\Carter and it it has around 100 file names (ex:
test1.txt, test2.txt, test3.txt......test100.txt).

1. First, I need another list with all these 100 file names with no
".txt" extension at the end (ex: test1, test2,test3, ....test100).
2. Then I want these two lists as key and value pair in a hash.
(EX: test1, test1.txt, test2, test2.txt, test3, test3.txt, ......,
test100, test100.txt).

I am struggling with the code to get the final hash. Can some one help
me out?

It makes it difficult to help when you don't post your code.

Matt
 
A

A. Sinan Unur

(e-mail address removed) (Jim Carter) wrote in
Hi all,

Below is the issue I am working on in Windows 2000.

I have directory, C:\Carter and it it has around 100 file names (ex:
test1.txt, test2.txt, test3.txt......test100.txt).

1. First, I need another list with all these 100 file names with no
".txt" extension at the end (ex: test1, test2,test3, ....test100).
2. Then I want these two lists as key and value pair in a hash.
(EX: test1, test1.txt, test2, test2.txt, test3, test3.txt, ......,
test100, test100.txt).

I am struggling with the code to get the final hash. Can some one help
me out?

At least in this forum, you are expected to show your attempt at solving
the problem. Please remember that in the future and do consult the
posting quidelines.

C:\carter> dir
2003/12/08 09:10 PM <DIR> .
2003/12/08 09:10 PM <DIR> ..
2003/12/08 08:56 PM 0 01 (1).txt
2003/12/08 08:56 PM 0 01 (10).txt
2003/12/08 08:56 PM 0 01 (11).txt
2003/12/08 08:56 PM 0 01 (12).txt
2003/12/08 08:56 PM 0 01 (13).txt
2003/12/08 08:56 PM 0 01 (2).txt
2003/12/08 08:56 PM 0 01 (3).txt
2003/12/08 08:56 PM 0 01 (4).txt
2003/12/08 08:56 PM 0 01 (5).txt
2003/12/08 08:56 PM 0 01 (6).txt
2003/12/08 08:56 PM 0 01 (7).txt
2003/12/08 08:56 PM 0 01 (8).txt
2003/12/08 08:56 PM 0 01 (9).txt
2003/12/08 08:56 PM 0 01.txt
2003/12/08 09:10 PM 295 zbzbz.pl

C:\carter> cat zbzbz.pl
use strict;
use warnings;

my %hash;
opendir DIR, 'C:/Carter' or die "Cannot open C:/Carter: $!\n";

ENTRY: while(my $entry = readdir(DIR)) {
next ENTRY if($entry !~ m/^(.+)\.txt$/o);
$hash{$1} = $entry;
}
closedir DIR;

use Data::Dumper;
print Dumper \%hash;


C:\carter> zbzbz.pl
$VAR1 = {
'01 (4)' => '01 (4).txt',
'01' => '01.txt',
'01 (2)' => '01 (2).txt',
'01 (10)' => '01 (10).txt',
'01 (8)' => '01 (8).txt',
'01 (12)' => '01 (12).txt',
'01 (13)' => '01 (13).txt',
'01 (7)' => '01 (7).txt',
'01 (6)' => '01 (6).txt',
'01 (3)' => '01 (3).txt',
'01 (1)' => '01 (1).txt',
'01 (11)' => '01 (11).txt',
'01 (9)' => '01 (9).txt',
'01 (5)' => '01 (5).txt'
};
 
B

Bob Walton

Jim Carter wrote:

....

Below is the issue I am working on in Windows 2000.

I have directory, C:\Carter and it it has around 100 file names (ex:
test1.txt, test2.txt, test3.txt......test100.txt).

1. First, I need another list with all these 100 file names with no
".txt" extension at the end (ex: test1, test2,test3, ....test100).
2. Then I want these two lists as key and value pair in a hash.
(EX: test1, test1.txt, test2, test2.txt, test3, test3.txt, ......,
test100, test100.txt).

I am struggling with the code to get the final hash. Can some one help
me out?


Um, it is pretty hard to help you with code we can't see. Post by
copy/pasting code (as opposed to retying) a complete example of your
code which anyone can copy/paste/execute which demonstrates the problem
you are having, and clearly (*not* "it doesn't work") state what you
perceive to wrong, including verbatim copies of error messages, and
folks here will gladly help you.
 
T

Tore Aursand

I have directory, C:\Carter and it it has around 100 file names (ex:
test1.txt, test2.txt, test3.txt......test100.txt).

1. First, I need another list with all these 100 file names with no
".txt" extension at the end (ex: test1, test2,test3, ....test100).

## Get a list of all *.txt files
opendir( DIR, 'C:\Carter' ) or die "$!\n";
my @files = grep { /\.txt$/ } readdir( DIR );
closedir( DIR );

## Create a list of those files without the extension and put them
## in a hash pointing to their original filename
use File::Basename qw( fileparse );
my %hash = ();
foreach ( @files ) {
my ($filename, undef, undef) = fileparse( $_ );
$hash{ $filename } = $_;
}


--
Tore Aursand <[email protected]>
"Scientists are complaining that the new "Dinosaur" movie shows
dinosaurs with lemurs, who didn't evolve for another million years.
They're afraid the movie will give kids a mistaken impression. What
about the fact that the dinosaurs are singing and dancing?" -- Jay
Leno
 
J

Jay Tilton

(e-mail address removed) (Jim Carter) wrote:

: Below is the issue I am working on in Windows 2000.
:
: I have directory, C:\Carter and it it has around 100 file names (ex:
: test1.txt, test2.txt, test3.txt......test100.txt).
:
: 1. First, I need another list with all these 100 file names with no
: ".txt" extension at the end (ex: test1, test2,test3, ....test100).
: 2. Then I want these two lists as key and value pair in a hash.
: (EX: test1, test1.txt, test2, test2.txt, test3, test3.txt, ......,
: test100, test100.txt).
:
: I am struggling with the code to get the final hash. Can some one help
: me out?

use File::Slurp 'read_dir';
my %hash = reverse map /^((.+)\.txt)$/, read_dir('C:/Carter');
 
M

Michele Dondi

I have directory, C:\Carter and it it has around 100 file names (ex:
test1.txt, test2.txt, test3.txt......test100.txt).

1. First, I need another list with all these 100 file names with no
".txt" extension at the end (ex: test1, test2,test3, ....test100).
2. Then I want these two lists as key and value pair in a hash.
(EX: test1, test1.txt, test2, test2.txt, test3, test3.txt, ......,
test100, test100.txt).

OK, too late, but... one more WTDI:

my %a = map {my $t=$_; s/\.txt$//; $_, $t} glob "test*.txt";

not exactly what the OP asked, but possibly a good approximation, and
could be made better if needed (e.g. "test*[0-9].txt").


Michele
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top