Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
re-arranging file info and written into another fi

Posted by ralph07 (ralph07), 13 June 2007
- <Die XPos="-2" YPos="11">
 <FinalValue="1" />
 <C4Value="65" />
 <LFValue="95" />
 <FMValue="98" />
 <ISValue="1.9" />
 </Die>
- <Die XPos="-2" YPos="12">
 <FinalValue="1" />
 <C4Value="75" />
 <LFValue="95" />
 <FMValue="9.1" />
 <ISValue="1.0" />
 </Die>
- <Die XPos="-2" YPos="11">
 <FinalValue="1" />
 <C4Value="68" />
 <LFValue="99" />
 <FMValue="99" />
 <ISValue="1.0" />
 </Die>
- <Die XPos="-5" YPos="12">
 <FinalValue="1" />
 <C4Value="70" />
 <LFValue="99" />
 <FMValue="9.3" />
 <ISValue="1.0" />
 </Die>
THE ABOVE ARE SOME DATA FROM THE FILE AND I WILL LOVE TO HAVE THE DATA IN THE ORDER BELOW.  AFTER THAT, I WANT TO WRITE IT IN ANOTHER FILE AND RE-NAME THE FILE WITH THE NAME OF THE OLD ONE.

#BELOW IS THE ORDER I WANT THEM TO BE(THE CHANGES HAS TO AFFECT THIS FOUR DATA AND THE REMAINING DATA ON THE FILE REMAINS THE SAME.) THE CHANGES IS TO SWITCH THE POSITION AND NOTHING ELSE.
 <FMValue="87" />
 <ISValue="0.8" />
 <LFValue="95" />
 <C4Value="75" />


#BELOW IS WHAT I HAVE SO FAR
 
open (IN, "lots_info") or die "can't: $!";
open (OUT, ">new.txt") or die "can't: $!";
while(<IN>){
 if (/(FinalValue="[1-6]")/) {
     print OUT "<$1/>\n";
     my $C4 = <IN>;
     my $LF = <IN>;
     my $FM = <IN>;
     my $IS = <IN>;
     print OUT map{$_}($FM,$IS,$LF,$C4);
     next;
  }
}
close(IN);
close(OUT);


Posted by admin (Graham Ellis), 14 June 2007
Here's modified (and simplified) code to print out the file with the switch made - the map was redundant, a print without parameters just reprints the incoming lines.

while(<IN>){
print OUT ;
 if (/(FinalValue="[1-6]")/) {
     my $C4 = <IN>;
     my $LF = <IN>;
     my $FM = <IN>;
     my $IS = <IN>;
     print OUT $FM,$IS,$LF,$C4;
  }
}

You then need a rename command to overwrite the old file with the new after you have closed them. Check the output file contents first before you run this extra command for the first time!

Posted by ralph07 (ralph07), 14 June 2007
Thank you so much. When I tried the code you gave me, it works perfectly except that this line is in this form.
<ISValue="1.0" />  </Die>
The above line suppose to be only   <ISValue="1.0" /> and the </Die> suppose to be the last line. The data suppose to be in the format below.  
<Die XPos="-2" YPos="12">
 <FinalValue="1" />
 <FMValue="9.1" />
  <ISValue="1.0" />
  <LFValue="95" />  
  <C4Value="75" />
   </Die>
Thank you so much for your help so far.  It helps a lot.


Posted by admin (Graham Ellis), 15 June 2007
I am very surprised at the new reported behaviour and can't see how that can happen from the sample data and sample program.  Do you have any extra information or any differences in what you did to what has been posted?

Posted by ralph07 (ralph07), 15 June 2007
The full file looks like this:
- <Map WId="GH72L279YZA0">
- <Wales>
 <IPO Value="604.3" />
 </Wales>
- <Die XPos="-2" YPos="11">
 <FinalValue="1" />  
 <C4Value="65" />  
 <LFValue="95" />  
 <FMValue="98" />  
 <ISValue="1.9" />  
 </Die>
- <Die XPos="-2" YPos="12">
 <FinalValue="1" />  
 <C4Value="75" />  
 <LFValue="95" />  
 <FMValue="9.1" />  
 <ISValue="1.0" />  
 </Die>
- <Die XPos="-2" YPos="11">
 <FinalValue="1" />  
 <C4Value="68" />  
 <LFValue="99" />  
 <FMValue="99" />  
 <ISValue="1.0" />  
 </Die>
- <Die XPos="-5" YPos="12">
 <FinalValue="1" />  
 <C4Value="70" />  
 <LFValue="99" />  
 <FMValue="9.3" />  
 <ISValue="1.0" />  
 </Die>
TO BE ABLE TO VIEW THE RESULT BEFORE RENAMING THE FILE, I USE A LOOP TO LOOP THROUGH THE DATA IN THE <OUT> FILE. THAT WAS WHAT I DID.
WHICH OTHER WAY CAN I PREVIEW THE DATA BEFORE RENAMING THE FILE AND WHAT RENAMING CODE CAN I USE.  SORRY FOR ASKING TOO MANY QUESTIONS.
I USE THIS TO PRINT OUT THE DATA:
@array= <OUT>;
foreach $array(@array){
print "$array\n";}

Posted by admin (Graham Ellis), 15 June 2007
I ran my code with your data, as follows:

Code:
while(<DATA>){  
print ;  
 if (/(FinalValue="[1-6]")/) {  
my $C4 = <DATA>;  
my $LF = <DATA>;  
my $FM = <DATA>;  
my $IS = <DATA>;  
print $FM,$IS,$LF,$C4;  
  }
}  
__END__
- <Map WId="GH72L279YZA0">
- <Wales>
 <IPO Value="604.3" />  
 </Wales>
- <Die XPos="-2" YPos="11">  
 <FinalValue="1" />  
 <C4Value="65" />  
 <LFValue="95" />  
 <FMValue="98" />  
 <ISValue="1.9" />  
 </Die>  
- <Die XPos="-2" YPos="12">  
 <FinalValue="1" />  
 <C4Value="75" />  
 <LFValue="95" />  
 <FMValue="9.1" />  
 <ISValue="1.0" />  
 </Die>  
- <Die XPos="-2" YPos="11">  
 <FinalValue="1" />  
 <C4Value="68" />  
 <LFValue="99" />  
 <FMValue="99" />  
 <ISValue="1.0" />  
 </Die>  
- <Die XPos="-5" YPos="12">  
 <FinalValue="1" />  
 <C4Value="70" />  
 <LFValue="99" />  
 <FMValue="9.3" />  
 <ISValue="1.0" />  
 </Die>  


Here is the result

Code:
- <Map WId="GH72L279YZA0">
- <Wales>
 <IPO Value="604.3" />  
 </Wales>
- <Die XPos="-2" YPos="11">  
 <FinalValue="1" />  
 <FMValue="98" />  
 <ISValue="1.9" />  
 <LFValue="95" />  
 <C4Value="65" />  
 </Die>  
- <Die XPos="-2" YPos="12">  
 <FinalValue="1" />  
 <FMValue="9.1" />  
 <ISValue="1.0" />  
 <LFValue="95" />  
 <C4Value="75" />  
 </Die>  
- <Die XPos="-2" YPos="11">  
 <FinalValue="1" />  
 <FMValue="99" />  
 <ISValue="1.0" />  
 <LFValue="99" />  
 <C4Value="68" />  
 </Die>  
- <Die XPos="-5" YPos="12">  
 <FinalValue="1" />  
 <FMValue="9.3" />  
 <ISValue="1.0" />  
 <LFValue="99" />  
 <C4Value="70" />  
 </Die>  


Which looks correct to me.

If you can tell me what is wrong with the order in my answer, or what is different about your code to cause it to perform in a different way, I may be able to help a bit further



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho