By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

To test the pattern whether it is.jpg or .xml by comparing with the extension

P: 12
I need to write the image files in one file and (the xml and html) in another file from a common file. I am trying to compare the extensions,like if it is .jpg or .bmp it will be written to the image file and if it is .html or .xml it will be written to the app file.But I am not able to do so.My code is given below
Expand|Select|Wrap|Line Numbers
  1. open (MyFileHandle,"<C\:\\home\\commonfilelist.txt");
  2. open (MyWebFileHandle,">$imagefile") or die("File Open Error");
  3. open (MyAppFileHandle,">$Appfile") or die("File Open Error");
  4.  
  5. # Read till end of file.
  6. until((eof(MyFileHandle)) && (seek(MyFileHandle,0,0) != eof(MyFileHandle)))
  7. {
  8. # Check if data exists. If empty exit with appropriate error.        
  9.     $Text = readline(MyFileHandle);
  10.     if(defined($Text)) 
  11.     {
  12.         #@ArrayTemp = split("/",$Text);
  13.             #$Ch = substr($ArrayTemp[0],0);
  14.             $result=rindex($Text,".");
  15.             $pattern=substr($Text,$result);
  16.             if($pattern eq (("jpg")||("bmp")))
  17.             {
  18.                 print MyWebFileHandle $Text;
  19.                 print MyWebFileHandle "\n";
  20.             }
  21.             elsif ($pattern eq (("xml")||("html")))
  22.             {
  23.                 print MyAppFileHandle $Text;
  24.                 print MyAppFileHandle "\n";
  25.             }
  26.  
  27.             else
  28.             {
  29.                 print "Not an image and Html.\n";
  30.             }
  31.     }
  32. }
Mar 10 '08 #1
Share this Question
Share on Google+
11 Replies


rajiv07
100+
P: 141
Expand|Select|Wrap|Line Numbers
  1. open(COMMON,"c:/perl/common.txt") or die $!;
  2.  
  3. open(IMAGE,">c:/perl/image.txt") or die $!;
  4.  
  5. open(HTML,">c:/perl/html.txt") or die $!;
  6.  
  7. my @commonFile=<COMMON>;
  8.  
  9. print IMAGE grep(/\.(jpg|bmp)$/,@commonFile);
  10.  
  11. print HTML grep(/\.(xml|html)$/,@commonFile);
Hopes it help

Regards
Rajiv
Mar 10 '08 #2

nithinpes
Expert 100+
P: 410
You can simplify your search script to this:
Expand|Select|Wrap|Line Numbers
  1. open (MyFileHandle,"<C\:\\home\\commonfilelist.txt");
  2. open (MyWebFileHandle,">$imagefile") or die("File Open Error");
  3. open (MyAppFileHandle,">$Appfile") or die("File Open Error");
  4. while(<MyFileHandle>)  {
  5. if((/\.jpg/)||(/\.bmp/)) {
  6.  print MyWebFileHandle $_; }
  7. elsif((/\.html?/)||(/\.xml/)) {
  8.  print MyAppFileHandle $_; }
  9. else {
  10. chomp;
  11. print "$_  is not an image or Html.\n"; }
  12. }
  13.  
  14.  
Mar 10 '08 #3

nithinpes
Expert 100+
P: 410
Well...what Rajiv has posted is even more crisp and very well do the job.
Mar 10 '08 #4

P: 12
Expand|Select|Wrap|Line Numbers
  1. open(COMMON,"c:/perl/common.txt") or die $!;
  2.  
  3. open(IMAGE,">c:/perl/image.txt") or die $!;
  4.  
  5. open(HTML,">c:/perl/html.txt") or die $!;
  6.  
  7. my @commonFile=<COMMON>;
  8.  
  9. print IMAGE grep(/\.(jpg|bmp)$/,@commonFile);
  10.  
  11. print HTML grep(/\.(xml|html)$/,@commonFile);
Hopes it help

Regards
Rajiv
This is not working.help plz
Mar 10 '08 #5

rajiv07
100+
P: 141
This is not working.help plz
Its working fine for me.Have u getting any error.
Mar 10 '08 #6

nithinpes
Expert 100+
P: 410
This is not working.help plz
If so, provide a sample of contents in commonfilelist.txt .

This may also happen if your filelist has some other data along with filename in each line or spaces inserted at the end of each line. In such cases, modify those two lines to:
Expand|Select|Wrap|Line Numbers
  1. print IMAGE grep(/\.(jpg|bmp)/,@commonFile);
  2.  
  3. print HTML grep(/\.(xml|html)/,@commonFile);
  4.  
Just removed end of line($) in pattern.
Mar 10 '08 #7

eWish
Expert 100+
P: 971
This is not working.help plz
Please be more descriptive of your problem. We will need more information to be able to offer assistance. In the future posts such as this will be deleted.

Moderator
Mar 10 '08 #8

P: 12
If so, provide a sample of contents in commonfilelist.txt .

This may also happen if your filelist has some other data along with filename in each line or spaces inserted at the end of each line. In such cases, modify those two lines to:
Expand|Select|Wrap|Line Numbers
  1. print IMAGE grep(/\.(jpg|bmp)/,@commonFile);
  2.  
  3. print HTML grep(/\.(xml|html)/,@commonFile);
  4.  
Just removed end of line($) in pattern.
After executing the code ,the Appfile and webfile are created but they remain empty.The data in the common file are
a.html
b.html
falls.html
html\test.html
templatedata\category4
templatedata\Library
templatedata\Nik
templatedata\Page
templatedata\poc
templatedata\zz_tst_sakina_munshi_1_manifest
Test1.html
folwer.jpg
I need to write the html file to appfile and the image file to webfile,rest i need to neglect
Mar 11 '08 #9

nithinpes
Expert 100+
P: 410
After executing the code ,the Appfile and webfile are created but they remain empty.The data in the common file are
a.html
b.html
falls.html
html\test.html
templatedata\category4
templatedata\Library
templatedata\Nik
templatedata\Page
templatedata\poc
templatedata\zz_tst_sakina_munshi_1_manifest
Test1.html
folwer.jpg
I need to write the html file to appfile and the image file to webfile,rest i need to neglect
For this data, the script worked perfectly fine for me!! Don't know what is happening with you. Did you try the other method that was posted?
If that doesn't work, post the most recent code that you used.
Mar 11 '08 #10

kalyanrajsista
P: 7
Please check my comments and code wrapped in code tags..
Mar 18 '08 #11

kalyanrajsista
P: 7
I've tested the following code on unix box with the sample data provided in common.txt and found to be working fine.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  
  5. open (COMMON,"common.txt") or die $!;
  6. open (IMAGE,">image.txt") or die $!;
  7. open (HTML,">html.txt") or die $!;
  8. my @commonFile = <COMMON>;
  9. print IMAGE grep(/\.(jpg|bmp)$/, @commonFile);
  10. print HTML grep(/\.(xml|html)$/, @commonFile);
  11.  
Please note that i've removed the path of the files for my convenience as they got created from where i ran my script.

Kalyan
Mar 18 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.