Your SlideShare is downloading. ×
Python&Perl: Searching, Joining, & Splitting Strings; Mapping & Grepping
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Python&Perl: Searching, Joining, & Splitting Strings; Mapping & Grepping

604
views

Published on

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
604
On Slideshare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python & Perl Searching, Joining, & Splitting Strings Mapping & Grepping Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Searching Strings ● Joining Strings ● Splittings Strings ● Mapping & Greppingwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. String Searchwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. String Search ● The string search problem is a common prob- lem in computer science ● The problem can be formulated as follows: – Fix a finite alphabet (a finite set of symbols such as UNICODE) – Take a string or a pattern over that alpha- bet and a text over that alphabet and find some/all places in the text where that pat- tern occurswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Common Algorithms ● Brute-force algorithm ● Rabin-Karp algorithm ● Knuth-Morris-Pratt algorithm ● Boyer-Moore algorithm ● Finite State Automata (aka Regular Ex- pressions)www.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. String Search in Python find(), rfind()www.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. PY find() ● Let text and substr be strings text.find(substr[,start [,end]]) → int ● Search text left to right for substring substr from start to end when start and end are provided ● Return position of substr in text or -1 if not foundwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. PY find(): Examples >>> txt = the sail just needs to open >>> txt.find(sail) 4 >>> txt.find(just) 9 >>> txt.find(sail sail) ­1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. PY find(): Examples ##Negative indexes work so long  ## as start <= end >>> sail sail.find(i, ­9, ­5) 2 ## This call does not find anything  ## because ­5 ## > ­9 so ­1 is returned >>> sail sail.find(i, ­5, ­9) ­1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. PY rfind() ● Let text and substr be strings text.rfind(substr,[,start [,end]]) → int ● Search text right to left for substr from start to end when start and end are provided ● Return position of substr in text or -1 if not foundwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. PY rfind(): Examples ##Text sail sail is searched from the right and i is  ##found at position 7 >>> sail sail.rfind(i) 7 ##This  is  somewhat  counterintuitive:  although  we  specify  the  start  as  6  the  ##search  starts  from  the  right  end  of  sail  ##sail;  if  the  end  index  is  ##omitted, it is assumed to be ##the last valid index in sail sail, i.e., 8 >>> sail sail.rfind(i, 6) 7 >>> sail sail.rfind(i, 6, 8) 7www.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. PY rfind(): Examples ## Negative indexes can be used so long as start <= end >>> sail sail.find(i, ­8, ­1) 2 ## Nothing is found (­1 is returned) when start > end >>> sail sail.rfind(i, ­5, ­9) ­1 >>> sail sail.find(i, 7, 5) ­1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. String Search in Perl index(), rindex() source at index_rindex.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. PL index() ● Let $text and $substr be strings index($text, $substr) → scalar int index($text, $substr, start) → scalar int ● Search $text left to right for $substr from start when start is provided ● Return position of $substr in $text or -1 if not foundwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. PL index(): Examples my $i; my $txt = the sail just needs to open; $i = index($txt, sail); print "$in"; ## prints 4 ## sail is not found if we start searching at position  ## 8 $i = index($txt, sail, 8); print "$in"; ## prints ­1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. PL rindex() ● Let $text and $substr be strings rindex($text, $substr) → scalar int rindex($text, $substr, start) → scalar int ● Search $text right to left for $substr from start when start is provided ● Return position of $substr in $text or -1 if not foundwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. PL rindex(): Examples my $txt = sail sail sail; print rindex($txt, sail), "n";  ## prints 10 print rindex($txt, sail, 9), "n";  ## prints 5 print rindex($txt, open), "n";  ## prints ­1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. Joining Strings in Pythonwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. PY join() ● Let separator and seq be a string and a sequence of strings, respectively separator.(seq) → string ● Concatenate each element string in seq into a result string so that each element string is separated from the next one by separator and return the result string ● separator is not added at the end of the result string ● Each element in a seq must be a stringwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. PY join(): Examples >>> lst = [the, sail, just, needs, open] >>> **.join(lst) the**sail**just**needs**open >>> tpl = (11, 12, 13, 14, 15) >>>  ||| .join(tpl) 11 ||| 12 ||| 13 ||| 14 ||| 15 >>> tpl02 = (11, 12, 13, 14, 15) >>>  ||| .join(tpl) ## Error, elements are not stringswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. Joining Strings in Perl source at join_examples.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. PL join() ● Let $separator and @list be a string and a list of ele- ments, respectively join($separator, @list) → $string ● Takes @list, converts its elements into string scalars, and concatenates them into a result string so that each string is separated from the next one by the $separa- torwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. PL join(): Examples my @lst = qw(the sail just needs to open); ## str is the**sail**just**needs**to**open; my $str = join(**, @lst); print "$strn"; ## str is 1 /// 2 /// 3 /// 4 /// 5 my $str_02 = join( /// , (1..5)); print "$str_02n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. PL join(): What Does This Code Do? my $input; my @array_of_inputs = (); while ( $input = <STDIN> ) {   chomp($input);   if ($input eq end) {     last; ## break   }   else {     push(@array_of_inputs, $input);   } } print join(; , @array_of_inputs), "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. Splitting Strings in Pythonwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. PY split() ● Let text and separator be strings text.(separator) → list of strings ● Split text by separator and return a list of split elements (strings) elementwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 27. PY split(): Examples >>> text_01 = word01 word02 word03 >>> text_01.split( ) [word01, word02, word03] >>> word01, word02, word03.split(,) [word01,  word02,  word03] >>> separator = , >>> text = word01, word02, word03 >>> separator.join(text.split(separator)) word01, word02, word03www.youtube.com/vkedco www.vkedco.blogspot.com
  • 28. PY split(): Examples >>> text = word01, word02, word03 >>> separator.join(text.split(separator)) word01, word02, word03 >>> text == separator.join(text.split(separator)) True >>> splits = text.split(separator) >>> splits [word01,  word02,  word03] >>> splits == separator.join(splits).split(separator) Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 29. PY split(): Examples >>> text = word01, word02, word03 >>> separator.join(text.split(separator)) word01, word02, word03 >>> text == separator.join(text.split(separator)) True >>> splits = text.split(separator) >>> splits [word01,  word02,  word03] >>> splits == separator.join(splits).split(separator) Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 30. Splitting Strings in Perl source at split_examples.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 31. PL split() ● Let re be a regular expression, string a string scalar and n a number scalar split(regular_expression, string, n) Split string into non-overlapping matches of regular_expression and return at most n of them as a listwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 32. PL split(): Examples sub split_test_01 {   print "SPLIT 01n";   print text:  . $text_01 . "n";   my @split_01 = split(/ /, $text_01, 3);   ## @split_01 == (word01, word02, word03);   ## get the length of @split_01   my $len_of_split_01 = @split_01;   print we have  . $len_of_split_01 . " split elementsn";   print "$_n" foreach (@split_01);   print "n"; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 33. PL split(): Examples #there are 2 split elements #word01 #word02 word03 sub split_test_02() {   print "SPLIT 02n";   print text:  . $text_01 . "n";   my @split_02 = split(/ /, $text_01, 2);   my $len_of_split_02 = @split_02;   print we have  . $len_of_split_02 . " split elementsn";   print "$_n" foreach (@split_02);   print "n"; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 34. PL split(): Examples #we have 3 split elements #word01 #word02 #word03 sub split_test_03 {   print "SPLIT 03n";   my $text_02 = word01, word02; word03;   print text:  . $text_02 . "n";   ## split on ,; or space   my @split_03 = split(/[,;] /, $text_02, 3);   my $len_of_split_03 = @split_03;   print we have  . $len_of_split_03 . " split elementsn";   print "$_n" foreach (@split_03);   print "n"; }www.youtube.com/vkedco www.vkedco.blogspot.com
  • 35. Mapping & Grepping source at map_01.py, map_01.pl, grep_examples.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 36. References ● www.python.org ● http://docs.python.org/2/ ● www.perl.org ● http://perldoc.perl.org/www.youtube.com/vkedco www.vkedco.blogspot.com