Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

perl regular expressions

by iamsachin (Initiate)
on Jun 23, 2014 at 21:18 UTC ( [id://1090967]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I'm new to perl. I searched online,reffered to a couple of links and i have written a perl script to match multiple sting in a file. this is the file :

{ 'testing' => {

link => "http://www.espn.com",,

bandwidth => "100",

r => "2",

}, };

10 15 * * * cd /tmp; /path/to/file --timeslot 'name'

Now,I need to write a perl script that matches the string in the file.

find_string { my ($file, $string) = @_; open my $fh, '<', $file; while (<$fh>) { return 1 if /\Q$string/; } die "Unable to find string: $string"; } find_string('filename', "testing");

The above script searches the file for string "testing" in the file and fails if the string is not found. Now I'd like to search multiple strings in that file like bandwidth,rate. I guess i need to use regular expressions.But i'm not sure how to implement it. any suggestions.

Replies are listed 'Best First'.
Re: perl regular expressions
by kennethk (Abbot) on Jun 23, 2014 at 21:44 UTC
    If you want to return a binary found/not found, you can just modify your code to use an alternator:
    find_string { my ($file, @strings) = @_; open my $fh, '<', $file; my $re = join "|", map quotemeta, @strings; while (<$fh>) { return 1 if /$re/; } die "Unable to find string: $string"; }
    or, if you're not really comfortable with join and map,
    find_string { my ($file, @strings) = @_; open my $fh, '<', $file; while (<$fh>) { for my $string (@strings) { return 1 if /\Q$string/; } } die "Unable to find string: $string"; }

    As a side note, it's usually good to wrap input in <code> tags as well, since HTML and perlmonks will collude to mangle your offering.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Can i find multiple strings with the code that you mentioned. find_string('filename', "testing,link,rd"); I need to search multiple strings in the same function

        Rather than calling the code as
        find_string('filename', "testing,link,rd");
        for as I've written it, you'd invoke it with
        find_string('filename', "testing","link","rd");
        Alternatively, you could add a split to my proposed solution.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: perl regular expressions
by AppleFritter (Vicar) on Jun 23, 2014 at 22:06 UTC

    Since the input file you posted appears to contain structured data (of the kind produced by e.g. Data::Dumper), perhaps the best approach would be to parse that data into a hash (of hashes) and then extract the pieces you're interested in from that.

    A quick look at CPAN finds a Data::Undump, which appears to be written for this sort of situation (though it warns of being an "early release"). It's failing some of its tests for me (may be worth bugging demerphq about that), but for your data, it's working:

    #!/usr/bin/perl use feature qw(say); use Data::Dumper; use Data::Undump; $hash = { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; $Data::Dumper::Terse = 1; $undump = undump(Dumper($hash)); say $undump->{'testing'}->{'link'}; say $undump->{'testing'}->{'bandwidth'};

    Output:

    $ perl 1090975.pl http://www.espn.com 100 $
      I guess dumper that you specified above is to display the content of the file..But i need to check if the string is present in the file if not thrown an error. I need the script to check if the bandwidth is 100, link is http://www.espn.com .If not the the script should throw an error
        Yes. You'd have to read the data from your file and then use the undump function from Data::Undump on it -- or something similar. Alternatively, you could perhaps roll your own parser; whether that's worth the effort depends on how much processing you intend to do with your data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1090967]
Approved by Laurent_R
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found