KroKite has asked for the wisdom of the Perl Monks concerning the following question:

Below Code, Actually prints the value, as many time as many folders and files are present in the directory from where the file has been executed... Is there any way, that i can print the value only once.. ? What i am trying to do with my code is ... I am trying to scan each file, in directory to check whether file is written in c++, php or perl. But in Below Code, I have replaced grep option with simple print... Thanks
#!/usr/bin/perl use strict; use warnings; use File::Find; my $dir = shift || './'; find(\&test, $dir); sub test{ my %alphabet = ( php => ["<?php", "?>", "echo", "print", "print_r"], perl => ["#!", "usr", "bin", "perl", "print", "stdin"], cplusplus => ["#include", "iostream", "stdio", "namespace", "strings", + "cout", "cin"]); print $alphabet{php}->[1]; }
Thanks
  • Comment on I have Values stored in array within Hashes, Can't really call one @ a time
  • Download Code

Replies are listed 'Best First'.
Re: I have Values stored in array within Hashes, Can't really call one @ a time
by aaron_baugher (Curate) on May 11, 2012 at 18:34 UTC

    If you're in a Unix-type system, you may find that the file utility is much easier than grepping files for strings, especially strings like "print" that aren't that unique to a particular type of file. For instance:

    $ file aa.php aa.php: PHP script text $ file test.pl test.pl: a perl script text executable $ file hello.c hello.c: ASCII C program text $ file package.sh package.sh: POSIX shell script text executable

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: I have Values stored in array within Hashes, Can't really call one @ a time
by Khen1950fx (Canon) on May 11, 2012 at 22:09 UTC
    This will do what you want. I used File::LibMagic and File::Find::Rule. You'll need to download and install libmagic first.
    #!/usr/bin/perl -l BEGIN { $| = 1; } use strict; use warnings; use File::Find::Rule; use File::LibMagic ':easy'; my(@files) = shift @ARGV; foreach my $file (@files) { @files = File::Find::Rule->file() ->name('*.*') ->in(@files); print $file . MagicFile $file; }
    Update: added $file . to print
Re: I have Values stored in array within Hashes, Can't really call one @ a time
by Anonymous Monk on May 11, 2012 at 13:34 UTC

    my( @cpp , @php, @perl );

    find ( ... \&test );

    print join "\n", @cpp, "\n";

    sub test {

    push @cpp, $File::Find::name if is_cpp($File::Find::name);

    push @perl, $File::Find::name if is_perl($File::Find::name);

    push @php, $File::Find::name if $File::Find::name if is_php( $File::Find::name );

    ...

    or

    ack --help type

    my @perl = qx{ack --perl "$startdir"};

    my @php = qx{ack --php "$startdir"};

    my @cpp = qx{ack --cpp "$startdir"};

      Could not really get how i can utilise with push,.. ack is useful with -w, but doesnot really fulfill my criteria.... Actually I would prefer grep, ... Just finding way to restrict the result..

        Could not really get how i can utilise with push,..

        what do you mean? You write a function  is_cpp that takes a filename as argument, and then it performs checks on the filename to determine if its cpp

        ack --perl --files-with-matches . /path/