Loading ...
Sorry, an error occurred while loading the content.

Re: Iteration over a string

Expand Messages
  • Victor
    ... cipher. ... ... ... Actually that is not true. Roman s original first line of code (i.e. my @original_text = ;) will read the
    Message 1 of 5 , Nov 23, 2004
      > >
      > > Hello all,
      > > I'm trying to write a script that takes a text file from the
      > > command line, then "encrypts" it using a simple substitution
      > The script as you wrote it does not take a text file, but takes
      > text input from the keyboard.
      > If you really do want to take a text file in the input and work
      > on that file, then we need to do an open and read the file.
      <snip some more>

      Actually that is not true. Roman's original first line of code (i.e.
      my @original_text = <>;) will read the whole file specified in the
      command line and slurp all of its lines into @original_text, including
      newline characters("\n").

      This is usually not recommended because you may run out of memory if
      the file is too big. The best approach is to read a line at a time and
      process it as you go.


      # read a line at a time. Each line ends up in
      # the default variable $_
      while ( <> ) {
      changetext($_); # process the line


      You can I remove newline characters if you want "clean lines" instead.
      You need to remove the print command in your changetext sub and
      include it in your calling loop:

      while ( <> ) {
      chomp; # removes newline char
      changetext($_); # process the line, no printing
      print "$_\n"; # print changed line with newline

      Best of luck
    Your message has been successfully submitted and would be delivered to recipients shortly.