Rob Dixon <[EMAIL PROTECTED]> wrote: john wright wrote: > > Rob Dixon wrote: john wright wrote: >> >> 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; >> } >> > > Hi Rob, > Thanks for the reply,but when i am running your code like this > perl myprogrem.pl Boo1-6 > i am getting following error : > "Can't use string ("1") as a HASH ref while "strict refs" in use at myprogrem.pl" at line "foreach my $book (values %$data) " > > Could you please help me.. > Thanks
Then your data isn't properly represented by the example you gave us. Please post a couple of dozen lines from your actual data so we can see what you're dealing with as there's little point in me trying to guess. By the way, please bottom-post your replies as it makes longer threads much easier to understand. Thanks. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Hi Rob, Below is the file sample data,this file is not .pl it is a module file you can say as mydata.pm. package Sites; @ISA = ("test::Module"); use strict; use test::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", } }; sub function1 { } sub function 2 { } 1; Thanks john --------------------------------- Do you Yahoo!? Next-gen email? Have it all with the all-new Yahoo! Mail Beta.