473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
11 1330
rajiv07
141 100+
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
410 Expert 256MB
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
410 Expert 256MB
Well...what Rajiv has posted is even more crisp and very well do the job.
Mar 10 '08 #4
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
141 100+
This is not working.help plz
Its working fine for me.Have u getting any error.
Mar 10 '08 #6
nithinpes
410 Expert 256MB
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
971 Expert 512MB
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
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
410 Expert 256MB
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
Please check my comments and code wrapped in code tags..
Mar 18 '08 #11
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

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

Similar topics

1
by: Ole Kramer, SFS GmbH | last post by:
We have installed PHP 5.0.1 on a Windows 2003 Server with IIS6 in ISAPI mode. The processing of .php files works fine with our basis version of the php.ini file. But, as soon as we edit the php.ini...
2
by: Dirk McCormick | last post by:
I need to transform something that looks like this <!-- %%%%%%%%%%%%%%%%%%%%%%% --> <MyStructure> <Packages> <Package> <PkgBenefitCode>1</PkgBenefitCode> <PkgBenefitCode>2</PkgBenefitCode>...
5
by: Abby Lee | last post by:
I ask the user to enter a time in the formatio 12:30 PM. onChange I send the string to this function. I'm using alert boxes to test it...and am always getting the "Does not work" alert box. What am...
3
by: nnorwitz | last post by:
If you don't write or otherwise maintain Python Extension Modules written in C (or C++), you can stop reading. Python 2.5 alpha 1 is in the process of being released later today. There are...
0
by: Neal Norwitz | last post by:
If you don't write or otherwise maintain Python Extension Modules written in C (or C++) or embed Python in your application, you can stop reading. Python 2.5 alpha 1 was released April 5, 2006. ...
4
by: freefly_xml | last post by:
I want to test to see if I am on the last page of a document. In this example it is an invoice. I want to print a different table in REGION AFTER when I am on the last page. I have tried many...
3
by: nickyeng | last post by:
I wanna know whether xhtml extension needed for xhtml file? if i have xhtml code in a file, can i save that file as index.html ?
7
by: laura | last post by:
Hi, I have a variable of type double. I need to know if there is an integer number store there. How can I test that ? I also have a default precision for doing this operation. Many thanks,...
6
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly...
4
by: smartic | last post by:
how to test string whether it contains special characters -------------------------------------------------------------------------------- please help dear experts on how to test string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.