john wright wrote: > > I have a text file,which contains following type of data, > > my $Data = { > 'Book1-6' => { > 'DESCRIPTION' => 'Book1-6', > 'URLS1-6' => > "http://www.book1.com " . > "http://www.book2.com " . > "http://www.book3.com " . > "http://www.book4.com " . > "http://www.book5.com " . > "http://www.book6.com", > 'Book_PATH' => > "/usr/httpd/Book1-6", > }, > > 'BOOK11-66' => { > 'DESCRIPTION' => 'Book11-66', > 'URLS11-66' => > "http://www.book11.com " . > "http://www.book22.com " . > "http://www.book33.com " . > "http://www.book44.com " . > "http://www.book55.com " . > "http://www.book66.com", > 'Book_PATH' => > "/usr/httpd/Book11-66", > } > } > > > now i want following type of output from the above text file... > > 1.perl myprogram.lp URLS1-6 > > out put will be > "http://www.book1.com " > "http://www.book2.com " . > "http://www.book3.com " . > "http://www.book4.com " . > "http://www.book5.com " . > "http://www.book6.com", > > > 2.if i give perl myprogram.lp URLS11-66 > output will be > > "http://www.book11.com " > "http://www.book22.com " . > "http://www.book33.com " . > "http://www.book44.com " . > "http://www.book55.com " . > "http://www.book66.com",
Hello John Take a look at perldoc -f do. Using this function on your file will throw away the lexical scalar $Data but will return the vaue of the hash reference which is what you need. The code below gives you the desired results (except that it doesn't print the trailing space separators between the URLs, which I presume you don't need). It assumes the data you describe is in a file called mydata.pl. Beware that the contents of this file will be executed faithfully so if they contain Perl code that will destroy your system instead of the data you expect then that is just what will happen! By the way, you may want to consider using an anonymous array for your list of URLs instead of concatenating them into a space-delimited string. HTH, Rob use strict; use warnings; my $key = shift || ''; my $data = do 'mydata.pl'; foreach my $book (values %$data) { next unless my $urls = $book->{$key}; print "$_\n" foreach $urls =~ /\S+/g; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>