newbie also

Discussion in 'Perl Misc' started by NEAL ZIERKE, Aug 29, 2008.

  1. NEAL ZIERKE

    NEAL ZIERKE Guest

    I'm just learning perl, so that said.

    question is.


    If I have file names in directory like /mydir/abcd12e.fgh ,,, in unix system

    I wish to test the "12" part of filenames. I understand and lot of examples
    on net for directory stuff ( File::Find::name )

    I wish to test against other file names that will look the same but the 12
    would be different ( this is the version of the file ) , say it might look
    like this /mydir/abcd45e.fgh, where as this is newest version.

    What is the easiest way to test those 2 char's of file name in perl




    So like




    use File::Find;

    find(\&myprocessing,"startdir");




    sub myprocessing

    {

    if ( "test the "12" vs the "45" )

    {

    "move the new to old file code "

    }




    }
    NEAL ZIERKE, Aug 29, 2008
    #1
    1. Advertising

  2. NEAL ZIERKE

    Ted Zlatanov Guest

    matching file name without digits (was: newbie also)

    On Fri, 29 Aug 2008 14:18:06 GMT "NEAL ZIERKE" <> wrote:

    NZ> If I have file names in directory like /mydir/abcd12e.fgh ,,, in unix system

    NZ> I wish to test the "12" part of filenames. I understand and lot of examples
    NZ> on net for directory stuff ( File::Find::name )

    NZ> I wish to test against other file names that will look the same but the 12
    NZ> would be different ( this is the version of the file ) , say it might look
    NZ> like this /mydir/abcd45e.fgh, where as this is newest version.

    NZ> What is the easiest way to test those 2 char's of file name in perl

    Logically, you want to group a bunch of files by their base name without
    digits.

    use File::Basename;

    my %bybase;

    sub store_name
    {
    my $name = shift @_;
    my $key = basename $name;
    $key =~ s/\d+//g;
    push @{$bybase{$key}}, $name;
    }

    After you use store_name on every file name, the %bybase hash will
    contain the groupings you want. Use Data::Dumper to see how it looks.

    So unlike your example, you wouldn't check when you find the file, you'd
    just store_name on that file name. Then after the full search is done
    you would go through %bybase.

    Ted
    Ted Zlatanov, Aug 29, 2008
    #2
    1. Advertising

  3. NEAL ZIERKE

    Dr.Ruud Guest

    Re: matching file name without digits

    Ted Zlatanov schreef:

    > my $key = basename $name;
    > $key =~ s/\d+//g;


    I would probably make that

    $key =~ s/[0-9]+/#/g;

    or just

    $key =~ s/[0-9]+/9/g;

    to prevent collissions.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Aug 29, 2008
    #3
  4. NEAL ZIERKE

    Jim Gibson Guest

    In article <yyTtk.2$393.0@trnddc05>, NEAL ZIERKE <>
    wrote:

    > I'm just learning perl, so that said.
    >
    > question is.
    >
    >
    > If I have file names in directory like /mydir/abcd12e.fgh ,,, in unix system
    >
    > I wish to test the "12" part of filenames. I understand and lot of examples
    > on net for directory stuff ( File::Find::name )
    >
    > I wish to test against other file names that will look the same but the 12
    > would be different ( this is the version of the file ) , say it might look
    > like this /mydir/abcd45e.fgh, where as this is newest version.
    >
    > What is the easiest way to test those 2 char's of file name in perl


    What are you trying to test? You can extract the digits using a regular
    expression:

    $file = '/mydir/abcd12e.fgh';
    if( $file =~ /^(.+)(\d+)(.+)$/ ) {
    my $ver = $2;
    print "Version $ver of file $1..$3\n";
    }


    > So like
    >


    > use File::Find;
    > find(\&myprocessing,"startdir");
    > sub myprocessing
    > {
    > if ( "test the "12" vs the "45" )
    > {
    > "move the new to old file code "
    > }
    > }


    find will only have one file name at a time. You can save the file
    names in a global array and process the contents of the array after you
    have called find. For efficiency, extract the version numbers and put
    the remainder of the file name in a hash as you traverse the array,
    which will allow you to easily check if two versions of the same file
    exist in the directory (untested):

    my @files;
    find( sub( push(@files,$File::Find::name) ), $startdir);
    my %versions;
    for my $file ( @files ) {
    if( $file =~ /^(.+)(\d+)(.+)$/ ) {
    my $name = "$1$3";
    my $ver = $2;
    if( exists $versions{$name} ) {
    print "Versions $ver and $versions{$name} exist for $name\n";
    # do whatever processing needed
    }else{
    $versions{$name} = $ver;
    }
    }
    }

    --
    Jim Gibson
    Jim Gibson, Sep 4, 2008
    #4
    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. James Dyer
    Replies:
    5
    Views:
    632
  2. James Cline
    Replies:
    2
    Views:
    344
    Natty Gur
    Aug 20, 2003
  3. Guest
    Replies:
    0
    Views:
    411
    Guest
    May 21, 2004
  4. Russ Perry Jr
    Replies:
    2
    Views:
    558
    Russ Perry Jr
    May 19, 2004
  5. Colin B.

    Newbie question on split, and also awk.

    Colin B., May 16, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    106
    Peter J. Holzer
    May 18, 2006
Loading...

Share This Page