in reply to perl regular expressions

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 $

Replies are listed 'Best First'.
Re^2: perl regular expressions
by iamsachin (Initiate) on Jun 23, 2014 at 23:29 UTC
    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.
        Hi can you please help me out with reading the data from file. { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; instead of specifying the above value in the code. i want to read it from the text file..I tried but couldn't do it.

        Sorry ,I didn't understand what you said. Could you please be more specific ? My requirement is i need to parse a file for certain strings and if they are not present, the script should throw an error. data::dumper is used to read the file and print the contents of the file.But I'd like to verify if the file contains some specific strings and throw an error if the strings are not present.

        First of all,Thank you so much for explaining in detail.Appreciate it.

        Is there a way to combine these two scripts into one single script

        #!usr/bin/perl use feature qw/say/; use Data::Dumper; $Data::Dumper::Terse = 1; $hash = { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; say Dumper($hash); undef $/; $data = <>; $undump = undump($data); die "'testing' not found!" unless($undump->{'testing'}); say $undump->{'testing'}->{'link'} // (die "'link' not found!"); say $undump->{'testing'}->{'bandwidth'} // (die "'bandwidth not found! +");
        I tried to do it ,but looks likes the 2nd part didn't work. Any suggestions please ?
        Thanks a ton. Appreciate it !!
        Is there a way to read the hash value from a txt file ?