Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Interactive Session on Project 2 & Assorted Topics Copyright, 1996 © Dale Carnegie & Associates, Inc. By Surendra Singhi Spring 2005.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
1 More About Lisp. 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Input and Output in Scheme CS 480/680 – Comparative Languages.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Control in LISP More on Predicates & Conditionals.
Files Tutor: You will need ….
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Exam Preparation PrologLISP. Final Exam n Wednesday, April 10, 9am, BAC 132 check for changes the night beforecheck for changes the night before n Covers:
21-October-2002cse IO © 2002 University of Washington1 Input / Output CSE 413, Autumn 2002 Programming Languages
PRACTICAL COMMON LISP Peter Seibel 1.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Strings (Continued) Chapter 13
Strings Part 1 Taken from notes by Dr. Neil Moore
Structured Programming (Top Down Step Refinement)
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Chapter 8 Character Arrays and Strings
Topics Introduction to File Input and Output
Reading from and Writing to Files
Data Structures in Lisp
Data Structures in Lisp
Python Strings.
Topics Introduction to File Input and Output
Reading from and Writing to Files
Presentation transcript:

Input/Output Chapters 8 & 9

Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both use same extra arguments as read > (list (read-char) (read-line)) This is my input (#\T “his is my input”)

Characters n Single characters printed with #\ in front –#\Y is the character capital Y –#\* is the character asterisk n Special characters have names –#\Space is the space character –#\Newline & #\Tab likewise –#\Asterisk = #\*

Character Input Return Values n Read-char and read-line return two values –first is the string/character read –second (T or NIL) says whether there’s more data after that in the file n Normally only the first value is used –need special function to get other value(s) –but both values print

Character Equality n Characters are equal only if same case n Char-equal does case-insensitive compare > (equal #\A #\A) T > (equal #\a #\A) NIL > (char-equal #\a #\A) T

String Equality n Strings are equal only if same case n String-equal does case-insensitive compare > (equal “ABC” “ABC”) T > (equal “abc” “ABC”) NIL > (string-equal “abc” “ABC”) T Actually just non-NIL

Substrings n Search looks for substring –returns position of substring (0 = 1 st position) –returns NIL if it’s not there –(search “abc” “abcdef”) => 0 –(search “cde” “abcdef”) => 2 –(search “fgh” “abcdef”) => NIL –(search “BC” “abcdef”) => NIL –(search “BC” “abcdef” :test #’char-equal) => 1

Converting to String n Search requires string arguments –won’t find a character in a string n Convert character to string using string (search (string #\d) “bad dog”) => 2 n String also works on atoms (string ‘fred) => “FRED” –doesn’t work on lists

Strings & Lists n Many list functions work on strings –(length “abc”) => 3 –(reverse “abcd”) => “dcba” –(elt “abcd” 1) => b n Nth works on lists, but not strings –(nth 1 ‘(a b c d)) => b –(nth 1 “abcd”) => ERROR

Detecting End-of-File n Can get and use second value (pp. 411–3) –tells you before you get there n Can do it the same way as read –add extra arguments for return value (read-line inStr nil ‘EOF) => EOF if inStr empty (read-char inStr nil ‘end) => END if inStr empty

Exercise n Write a function that scans a text file & prints out the lines containing the string “his” (or “His”, or “HIS”, …) –the name of the text file is given –prefix each line printed with its line number Hello. I’m writing to tell you his story. This may take a while. 3: his story. 4: This may take a while. (scan-for-his “input.txt”) input.txt

Solution Loop n Keep a counter for the line number –initialized to 1, updated by adding 1 each time n Read a line of input into a variable –update each time by reading next line –loop stops when we get end-of-file n Search it for “his” –if found, write out the line # and line

Solution (defun print-his-lines-from-file (fileName) (with-open-file (in fileName :direction :input) (do ((line-num 1 (+ 1 line-num)) (line (read-line in nil ‘eof) (read-line in nil ‘eof))) ((eql line ‘eof)) (when (search “his” line :test #’char-equal) (format t “~a: ~a~%” line-num line) ))))

Character Output n Write-char and write-line output characters and lines as you would normally like them > (write-char #\a) aNIL > (write-line “This is a string.”) This is a string. NIL Write-char just puts out the character Write-line adds a trailing new-line

Next Time n Repetition controls in LISP