Perl script to process file list....

Discussion in 'Perl Misc' started by Jim Carter, Dec 9, 2003.

  1. Jim Carter

    Jim Carter Guest

    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
    Jim Carter, Dec 9, 2003
    #1
    1. Advertising

  2. Jim Carter

    Matt Garrish Guest

    "Jim Carter" <> wrote in message
    news:...
    > 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
    Matt Garrish, Dec 9, 2003
    #2
    1. Advertising

  3. (Jim Carter) wrote in
    news::

    > 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'
    };


    --
    A. Sinan Unur

    Remove dashes for address
    Spam bait: mailto:
    A. Sinan Unur, Dec 9, 2003
    #3
  4. Jim Carter

    Bob Walton Guest

    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.


    > Carter


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Dec 9, 2003
    #4
  5. Jim Carter

    Tore Aursand Guest

    On Mon, 08 Dec 2003 17:36:07 -0800, Jim Carter wrote:
    > 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 <>
    "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
    Tore Aursand, Dec 9, 2003
    #5
  6. Jim Carter

    Jay Tilton Guest

    (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');
    Jay Tilton, Dec 9, 2003
    #6
  7. On 8 Dec 2003 17:36:07 -0800, (Jim Carter) wrote:

    >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
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
    Michele Dondi, Dec 10, 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. Wet Basement
    Replies:
    1
    Views:
    2,515
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,785
  3. pj
    Replies:
    3
    Views:
    281
  4. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    134
    Paul Lalli
    Jan 5, 2005
  5. Replies:
    20
    Views:
    700
    Gunnar Hjalmarsson
    Jan 18, 2005
Loading...

Share This Page