Parsing line of text into words 
Author Message
 Parsing line of text into words

I have a text file. It simply has two words on a line
seperated by a space. How can I go about splitting
the words into seperate named variables?

thanks,
Steve



Wed, 18 Jun 1902 08:00:00 GMT  
 Parsing line of text into words

Quote:

>I have a text file. It simply has two words on a line
>seperated by a space. How can I go about splitting
>the words into seperate named variables?

in such a simple case you could use split().  for more complex
cases, Text::ParseWords is handy :)

--
brian d foy
Perl Mongers <URI:http://www.perl.org>
CGI MetaFAQ
  <URI:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>



Wed, 18 Jun 1902 08:00:00 GMT  
 Parsing line of text into words
ok, split() sounds like the way to go.
Now, I supposed I can split the line of text into variables....
How exactly does that work? syntax wise....

thanks for the help.



:
: >I have a text file. It simply has two words on a line
: >seperated by a space. How can I go about splitting
: >the words into seperate named variables?
:
: in such a simple case you could use split().  for more complex
: cases, Text::ParseWords is handy :)
:
: --
: brian d foy
: Perl Mongers <URI:http://www.perl.org>
: CGI MetaFAQ
:   <URI:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
:

--

----------------------------------------------
Steve Higgins EMT-CT                         |
Radford University EMS                       |
*********************************************|
Systems/Technical Analyst for VTLS Inc.      |
        Customer Support Div.                |
----------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Parsing line of text into words

[ please put your comments *following* the quoted text that you
  are commenting on.

  Jeopardectomy performed.
]


Quote:


>:
>: >I have a text file. It simply has two words on a line
>: >seperated by a space. How can I go about splitting
>: >the words into seperate named variables?
>: in such a simple case you could use split().  for more complex
>: cases, Text::ParseWords is handy :)
>ok, split() sounds like the way to go.

   So then you typed

      perldoc -f split

   to find about how to use the function. Right?

Quote:
>Now, I supposed I can split the line of text into variables....
>How exactly does that work? syntax wise....

--------------
#!/usr/bin/perl -w
use strict;

$_ = "first second\n";

my($word1, $word2) = split; # default pattern, default string.
                            # See perlfunc.pod for how to use
                            # other than the defaults

print "word1 '$word1'  word2 '$word2'\n";
--------------

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Wed, 18 Jun 1902 08:00:00 GMT  
 Parsing line of text into words
: --------------
: #!/usr/bin/perl -w
: use strict;

: $_ = "first second\n";

: my($word1, $word2) = split; # default pattern, default string.
:                             # See perlfunc.pod for how to use
:                             # other than the defaults

: print "word1 '$word1'  word2 '$word2'\n";
: --------------
               /\   Can you please tell me
               ||   why you're using the extra
                    single quotes around the
                    variables in the print?
                         thanks.  --thelma
: --
:     Tad McClellan                          SGML Consulting

:     Fort Worth, Texas



Wed, 18 Jun 1902 08:00:00 GMT  
 Parsing line of text into words



:
: : print "word1 '$word1'  word2 '$word2'\n";
: : --------------
:                /\   Can you please tell me
:                ||   why you're using the extra
:                     single quotes around the
:                     variables in the print?

It's a common technique to make invisible characters like whitespace
show up better.  For instance, consider

    $a = "foo";
    $b = "foo   ";
    print "\$a = $a\n",
          "\$b = $b\n";

You wouldn't be able to see the difference between the two lines unless
you happen to use a font that displays whitespace in some visible way.
Now consider

    $a = "foo";
    $b = "foo   ";
    print "\$a = `$a'\n",
          "\$b = `$b'\n";

See the difference?

Greg
--
Must one first batter their ears, that they may learn to hear with
their eyes? Must one clatter like kettledrums and penitential
preachers? Or do they only believe the stammerer?
    -- Nietzsche



Wed, 18 Jun 1902 08:00:00 GMT  
 Parsing line of text into words



: :
: : : print "word1 '$word1'  word2 '$word2'\n";
: : : --------------
: :                /\   Can you please tell me
: :                ||   why you're using the extra
: :                     single quotes around the
: :                     variables in the print?

: It's a common technique to make invisible characters like whitespace
: show up better.  For instance, consider

:     $a = "foo";
:     $b = "foo   ";
:     print "\$a = $a\n",
:           "\$b = $b\n";

: You wouldn't be able to see the difference between the two lines unless
: you happen to use a font that displays whitespace in some visible way.

  Yes--I'm just terribly slow.  I realized about an hour
  after I posted that the inner quotes were there for the
  readability of the text, and not as something necessary to
  the action of the program.

                    thanks.  --thelma
: Greg
: --



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Parsing Lines for Words?

2. parsing text over several lines

3. Text Parsing - How to Multi-Line?

4. Parsing ascii comma delimited text lines

5. Parsing through Multiple text lines

6. Word count/ word position in text

7. word-by-word Text wrapping problem

8. Searching a WORD 6 DOC by line line number

9. Text Parsing - Parse::RecDescent or another method?

10. Parsing line by line

11. running perl at command line with arguments: script.cgi?text=text

12. Search a file (line by line) for String/text

 

 
Powered by phpBB® Forum Software