Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by Laurent_R
    Hi,

    Your second attempt (Post # 13) seems to be trying to do something else, it looks like you are trying to print every file name and directory path in the list.

    One error is that $count is reset to 0 at each iteration through the while loop, so that you put each $name into the first element of the array, each time overriding what you had put there previously. The second error is that the @dir_name array is declared within the while loop, so that it is lexically scoped within the while loop. When you exit the loop and start your foreach loop, the array is no longer in scope (basically, you could say it is empty at that point, though it would be more correct to say that is is undefined). So, you would have to declare the @dir_name array and the $count var before entering the while loop. I think this would work properly (but I haven't tries these changes, there maybe something else wrong that I did not see).

    But, in fact, you don't really need to store your values in the @dir_name array. It is simpler to change this:

    Perl Code:
    #...
         splice @splitted_line, 0, 5;
         $name = join ' ', @splitted_line;
         $dir_name[$count] = $name;
         $count = $count +1;
    }
     
    foreach $i(@dir_name) {
         print $i;
    }


    To:

    Perl Code:
    #...
        splice @splitted_line, 0, 5;
        $name = join ' ', @splitted_line;
        print $name, "\n";
    }


    Actually, you don't even really need the $name var. So you could shorten it even more to:

    Perl Code:
    #...
        splice @splitted_line, 0, 5;
        print join ' ', @splitted_line, "\n";
    }

    can an array be copied to another directly without looping through each index ?

    what this means please tell me

    $name = join ' ',@splitted_line;
    This seems to be maintained in single dimension varaible ... is it ?
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    846
    Rep Power
    496
    Originally Posted by vkchaitanya
    can an array be copied to another directly without looping through each index ?
    Yes, it is done quite often.

    For example, you can write:

    Perl Code:
    my @old_array = ('Jan', 'Feb', 'Mar', 'Apr');
    my @new_array = @old_array [0..2]; 
    # @new_array now contains ('Jan', 'Feb', 'Mar') 
    # (here I have copied only the first three elements, 
    # but "my @new_array = @old_array;" would have copied everything)


    Originally Posted by vkchaitanya
    what this means please tell me

    Code:
    $name = join ' ',@splitted_line;
    This seems to be maintained in single dimension varaible ... is it ?
    It means: take all the elements in the @splitted_line array and merge (join) their contents into a single scalar string ($name), separating each element from the next with a white space (the ' ' part).

    With my example above, you could do:

    Perl Code:
    my @array = ('Jan', 'Feb', 'Mar', 'Apr');
    my $string = join ' ', @array [0..2]; $string now contains this: "Jan Feb Mar"
    # ...
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,019
    Rep Power
    1228
    You can also interpolate it inside a string without using join.

    Code:
    my @array = ('Jan', 'Feb', 'Mar', 'Apr');
    my $string = "The first 3 months of the year are @array[0..2]";
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    846
    Rep Power
    496
    Right, I don't think about this possibility most of the time.
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,019
    Rep Power
    1228
    vkchaitanya,

    How many times do you want to extract the code from a given directory? Every time it's seen or only once per group of lines of a given directory?
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,019
    Rep Power
    1228
    See if this does what you want.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use File::Basename;
    use Data::Dumper;
    
    my $p7zip = 'p7zip.txt';
    open my $p7zip_fh, '<', $p7zip or die "Failed to open '$p7zip' $!";
    
    my %code;
    
    LINE:
    while (my $line = <$p7zip_fh>) {
        
        # this may need to be adjusted if the Attr field differs from the sample data
        next LINE unless $line =~ /\d+ [.]{5} \d+/;
        
        my $path = (split(/ /, $line))[5];
        my $dir = dirname($path);
        
        # the regex may need to be adjusted
        # depending on what constitutes a "code"
        if ( $dir =~ /_([A-Z]+)$/i ) {
            $code{$dir} = $1;
        }
    }
    
    print Dumper \%code;
    This is the test file I used.
    Code:
    7-Zip (A) [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
    p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,4 CPUs)
    
    Listing archive: 093624942528_01_DDP.zip
    
    --
    Path = 093624942528_01_DDP.zip
    Type = zip
    Physical Size = 694397157
    
    Date Time Attr Size Compressed Name
    ------------------- ----- ------------ ------------ ------------------------
    2012-03-23 12:22:08 D.... 0 0 075678342424_01_ODP
    2011-10-17 12:04:14 D.... 0 0 075678342424_01_ODP/075678342424_AEC
    2011-10-05 13:18:24 ..... 6148 178 075678342424_01_ODP/075678342424_AEC/.DS_Store
    2012-03-23 17:48:54 D.... 0 0 __MACOSX
    2012-03-23 17:48:54 D.... 0 0 __MACOSX/075678342424_01_ODP
    2012-03-23 17:48:54 D.... 0 0 __MACOSX/075678342424_01_ODP/075678342424_AEC
    2011-10-05 13:18:24 ..... 82 40 __MACOSX/075678342424_01_ODP/075678342424_AEC/._.DS_Store
    2011-10-17 12:04:14 ..... 12445 3545 075678342424_01_ODP/075678342424_AEC/075678342424.eps
    2011-10-17 12:04:14 ..... 49326 3808 __MACOSX/075678342424_01_ODP/075678342424_AEC/._075678342424.eps
    2011-10-14 11:02:14 D.... 0 0 075678342424_01_ODP/075678342424_AEC/075678342424_booklet
    2011-10-10 17:10:10 ..... 6471105 6327034 075678342424_01_ODP/075678342424_AEC/075678342424_booklet/075678342424_booklet.pdf
    2012-03-23 17:48:54 D.... 0 0 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_booklet
    2011-10-10 17:10:10 ..... 82 47 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_booklet/._075678342424_booklet.pdf
    2011-10-28 16:13:16 D.... 0 0 075678342424_01_ODP/075678342424_AEC/075678342424_cover
    2011-10-28 16:13:16 ..... 6416264 6121768 075678342424_01_ODP/075678342424_AEC/075678342424_cover/075678342424.tif
    2012-03-23 17:48:56 D.... 0 0 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_cover
    2011-10-28 16:13:16 ..... 117556 98796 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_cover/._075678342424.tif
    2011-10-14 11:02:16 D.... 0 0 075678342424_01_ODP/075678342424_AEC/075678342424_disc
    2011-10-05 17:13:44 ..... 3773060 446198 075678342424_01_ODP/075678342424_AEC/075678342424_disc/075678342424_disc.bmp
    2012-03-23 17:48:56 D.... 0 0 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_disc
    2011-10-05 17:13:44 ..... 198603 85486 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_disc/._075678342424_disc.bmp
    2011-10-14 11:02:18 D.... 0 0 075678342424_01_ODP/075678342424_AEC/075678342424_tray
    2011-10-05 15:04:26 ..... 1430703 1353361 075678342424_01_ODP/075678342424_AEC/075678342424_tray/075678342424_tray.pdf
    2012-03-23 17:48:56 D.... 0 0 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_tray
    2011-10-05 15:04:26 ..... 82 47 __MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_tray/._075678342424_tray.pdf
    ------------------- ----- ------------ ------------ ------------------------
    18475456 14440308 12 files, 13 folders
    And, here's the output:
    D:\perl>p7zip.pl
    Code:
    $VAR1 = {
              '075678342424_01_ODP/075678342424_AEC/075678342424_disc' => 'disc',
              '__MACOSX/075678342424_01_ODP/075678342424_AEC' => 'AEC',
              '075678342424_01_ODP/075678342424_AEC/075678342424_tray' => 'tray',
              '__MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_cover' => 'cover',
              '075678342424_01_ODP/075678342424_AEC' => 'AEC',
              '__MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_booklet' => 'booklet',
              '075678342424_01_ODP/075678342424_AEC/075678342424_booklet' => 'booklet',
              '__MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_disc' => 'disc',
              '__MACOSX/075678342424_01_ODP/075678342424_AEC/075678342424_tray' => 'tray',
              '075678342424_01_ODP/075678342424_AEC/075678342424_cover' => 'cover'
            };
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo