How to iterate through file line by line? - python-2.7

I have files with lines of input which I compare to eachother. I have simplified my code alot and obviously it isnt in working python but the main bits of important are the first line for iteration and the else clause, both of which are the ones used. The rest are just to show I wish to continue on with the data if it passes the Comparison code.
for read1, read2 in itertools.izip(input_file1, input_file2):
{CODE FOR COMPARISON}
if matched:
Worked = read1+read2
else:
print to output_file2
break
{CONTINUE ANALYSIS OF 'Worked'}
print Worked to output_file1
I assumed adding a print to file and break would solve the issue however it doesn't break the for loop so the next iteration occurs, it just breaks it. Is there anyway to use a command like break to move onto the next iteration of lines in both input files?
Thanks,
Tom

Is there anyway to use a command like break to move onto the next iteration of lines in both input files?
You are looking for the continue statement rather than the break statement:
break exits the loop whereas continue just moves onto the next iteration
for read1, read2 in itertools.izip(input_file1, input_file2):
{CODE FOR COMPARISON}
if matched:
Worked = read1+read2
else:
print to output_file2
continue
{CONTINUE ANALYSIS OF 'Worked'}
print Worked to output_file1
See
https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

Related

COBOL-COMPILATION ERROR In If statement :A scope-delimiter did not have a matching verb and was discarded

Error in if statement all statements are ended properly
One obvious error is the line
GIVING AR04-STATUS.
The . ends the previous if.
Cobol Style suggestions
Code one . per paragraph/ section and put the . on a line by itself just prior to I-EXIT. On the mainframe you can do x all; x p'#$' all (I think its been a long time) that will find . at the end of the line.
END-IF
END-IF
.
I-EXIT.
You do not need then - remove
Indent your code properly
IF NOT SUCCESS IN AR04-STATUS
AND NOT DL-KEY-NOTFOUND-S IN AR04-STATUS
SET CTB-MSG-ROUTINE-ERROR TO TRUE
MOVE "I-MTS-ACCOUNT-CHK" TO CTB-SECTION-NAME
MOVE "DCS_GT_SUBS_COUNT" TO CTB-ROUTINE-NAME
MOVE AR04-STATUS TO CTB-ROUTINE-STATUS
PERFORM X-HANDLE-ERROR
PERFORM Z-FINISH
END-IF
Makes it easier to read

Need to keep duplicates and delete all unique lines

I have two large text files
Looking for something that compares the two files for lines containing the same string and deleting all the rest. Hope that makes sense.
Example:
list1.txt
number1:1010:1020:1030
number2:1010:1020:1030
number3:1010:1020:1030
number4:1010:1020:1040
list 2.txt
number1
number2
number3
number100
output=
number1:1010:1020:1030
number2:1010:1020:1030
number3:1010:1020:1030
Anything that can do this? I would really appreciate help, thank you.
I'm going to sketch what you should program.
Read file1.txt into a List<String> line by line.
Same for file2.txt.
Loop over the lines of file 1, and check if the line .contains() any of the lines you read in file 2. (By nesting another loop).
If it does contain something of file 2, immediately go to the next line of file 1 (by continue-ing the outer loop). If it does not, then delete that element from the list (don't forget to decrease the loop variable, as you deleted the item at the current index).

Can you disable all print statements in SML

I currently have a lot of print statements in SML code, and I'm traversing a very large tree, so it takes a while for all the print statements to be printed, but right now I don't want to see any print statements and just want to see it run as fast as possible. But I don't want to comment all the prints because I later need them again to debug something else.
So I just want to be able to temporarily disable them for this run of the code.
I'm using the SML/NJ compiler.
As the first line in your code put the definition
fun print x = ();
Then -- your code will still work but the prints will do nothing.
Delete that line when you want to re-enable print.

vim regex : multi-line edit and controlling the cursor position

A text file is formatted like this:
Section 4 Area B Unit 20
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
Section 589 Area C Unit 1005
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
and these sections repeat by the hundreds. The "stuff i don't need" lines are actually about 30 or so. I need to keep the association of the "Section..." line, "Title..." line and "line of text I need to keep" related to each other. So I was hoping to first destruct the text document down (linewise) to the stuff I need before operating on it further (character-wise). So I wrote this:
g!/\Section\s\d*\sArea\s\h\sUnit\s\d*\n\|^\s\{3}\zs\d*\s-\_.*\ze2010-11/d
After deleting I get the "Section.." line and the "Title..." line, but never the subsequent lines underneath the "Title.." line. Those subsequent lines vary from 4 to 8 lines, but the "2010-11" line is consistent and always what I no longer want.
You can see I tried using zs and ze to select what I do not want deleted. I think the selection is working because if I change the command to "2011-12" then there is no match and the (OR) half of the command does not return a result.
I think the fault might be the cursor position(?), but I'm not sure and my effort to fix that has failed.
Can anyone see my error?
Thanks!
Give this a whirl.
:silent! g/^Section/+ , /^\s\+\d\+ -/- d
:g/^\s\+2010/ , -/\nSection\|\%$/ d
:g finds every line matching start of the pattern, ! will revert the selection and command will get applied to these lines.
Would something like g/^Section.../normal! j2dd3jd} do?
If not you can use a search for the Title line inside normal!
You may need to enclose it in "exec" but may be much simpler to write a function.
Do you really need to use vim? Seems like job for Perl to me.
There are many ways to do t, I'm sure. I think this sequence of commands should work (ignoring comment lines that begin with double quote):
" global delete of line below 'Section' to line before 'Title'
g/^\s*Section/+1;/Title/-1delete
" global delete from date line to line before 'Section'
g/^\s*\d\d\d\d-\d\d/;/^\s*Section/-1delete
" go to top line of buffer
gg
" delete last chunk, from final date to last line
/^\s*\d\d\d\d-\d\d/;$delete

Can I use gdb to skip a line without having to type line numbers?

I know I can use jump to set the program counter to a specific line and so I can skip one or more lines (or execute some lines again). Can I easily just skip the next line without having to enter line numbers?
This would be very convenient to "comment out" something at run time.
jump +1
jumps to the next line line i.e. skipping the current line. You may also want to combine it with tbreak +1 to set a temporary breakpoint at the jump target.
See http://sourceware.org/gdb/current/onlinedocs/gdb/Specify-Location.html for more ways of expressing locations with gdb.
Note that without a breakpoint gdb is likely to continue execution normally instead of jumping. So if jumping doesn't seem to work, make sure you set a breakpoint at the destination.
I have the following in my .gdbinit config file:
define skip
tbreak +1
jump +1
end
So just type skip in gdb to skip a line.
To Skip Any Numbers of Lines during Execution:
[Current Position -- in GDB] Line N
.......... // Lines To Skip
..........
..........
[Line To Execute - After Jumping] Line M
Put a Breakpoint on Line M:
gdb$b M
Jump To Line M:
gdb$jump M