Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    11

    Unanswered: parameter expansion in perl

    Hi all.

    I am trying to use Perl to locate the longest filename in a directory and a subdirectory,

    what is the equivalance of the parameter expasnion to bash script to do:

    to extract "file1" from "/root/file1.txt"


    in bash script, i use

    ${file##.*} , then ${file%.} to extract


    thanks
    daryl

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use:
    Code:
    @files = glob($directory);
    -PatP

  3. #3
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Something like this perhaps
    Code:
    #!\user\bin\perl -w
    
    use File::Find;
    use File::Basename;
    
      # Current directory if nothing else was specified
      @ARGV=qw(') unless @ARGV;
      my($s_LongestFileName);
      # Set $s_Name to the filename and extension of the file under scrutiny; Set $s_LongestFileName to $s_FileName if $s_FileName is longer
      find (sub{ my($s_FileName)=basename($File::Find::name); $s_LongestFileName=$s_FileName if length($s_LongestFileName) < length($s_FileName);},@ARGV);
      print length($s_LongestFileName).":$s_LongestFileName\n";

  4. #4
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Assuming that the extension of File1.txt.bak is .bak and not .txt.bak, you should be able to use
    Code:
    #!\user\bin\perl -w
    
    use File::Find;
    use File::Basename;
    
      # Current directory if nothing else was specified
      @ARGV=qw(.) unless @ARGV;
      my($s_LongestFileName);
      # Set $s_Name to the filename and extension of the file under scrutiny; Set $s_LongestFileName to $s_FileName if $s_FileName is longer
      find (\&CheckThisFile,@ARGV);
      print length((fileparse($s_LongestFileName,'\.[^.]*'))[0]).":$s_LongestFileName\n";
    
      sub CheckThisFile {
        my($s_FileName)=$File::Find::name;
        $s_LongestFileName=$s_FileName if length((fileparse($s_LongestFileName,'\.[^.]*'))[0]) < length((fileparse($s_FileName,'\.[^.]*'))[0]);
         };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •