Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Hex string search all files in directory

by starface245 (Novice)
on Dec 03, 2012 at 20:48 UTC ( #1006948=perlquestion: print w/replies, xml ) Need Help??
starface245 has asked for the wisdom of the Perl Monks concerning the following question:

In my folder I have 8 text files and each of those files I want to do hex string search of "hello"
In my folder:
I would the output
test.txt match
test1.txt match
foo2.txt not match
boo3.txt match
bar4.txt match
fish5.txt not match
cat6.txt notmatch
dog7.txt match
So far, it only does output = "test.txt match" no other ouputs.
#!/usr/bin/perl use strict; my @files =glob ("*.txt"); my $hex_out = "hex.txt"; for my $file(@files) { open (HEX_IN, "$file") or die; open (HEX_OUT, ">$hex_out") or die; binmode(HEX_IN); binmode(HEX_OUT); local $/; my $hex_string = <HEX_IN>; if ($hex_string =~ /\hello/) { print HEX_OUT "$file"; print HEX_OUT " match"; } else { print HEX_OUT "$file"; print HEX_OUT " not match"; } } close(HEX_IN); close(HEX_OUT);
It has to be in hex string format. Thanks!

Replies are listed 'Best First'.
Re: Hex string search all files in directory
by LanX (Bishop) on Dec 03, 2012 at 21:03 UTC
    I think you are overwriting the results for each file, "test.txt" is just the last one in the alphabet.

    What happens if you change

    open (HEX_OUT, ">>", $hex_out") or die "$!";

    to append instead of overwriting? Or even better you move it before the loop?

    Why don't you use warnings?

    BTW: proper indentation would facilitate reading your code =)

    Cheers Rolf

    PS:why do you escape the h in /\hello/?

Re: Hex string search all files in directory
by bdalzell (Sexton) on Dec 03, 2012 at 21:22 UTC
    if you want to match all instances of "hello" wether it is "HELLO","Hello" or "hello" you should also change the line:
    if ($hex_string =~ /\hello/)
    if (lc($hex_string)) =~ /\hello/)
      The more cannonical would be, in my opinion,
      if ($hex_string =~ /hello/i)
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006948]
Approved by LanX
[Discipulus]: indeed marto I dunno. I tested once and many years ago. But I'm not the programmer yardstick. I try to install Padre via cpan now just to add a line about it in the issue. Strawberryperl all life long!
[marto]: yeah, when I worked on Windows I did use Np++ all the time

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-06-25 08:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.