Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Why is it that when I use split without any arguments (which should operate on whitespace and $_), the whitespace is still in the strings that are returned? Is there ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942

    split in Perl


    Why is it that when I use split without any arguments (which should operate on whitespace and $_), the whitespace is still in the strings that are returned? Is there any way to make sure that the whitespace is fully eliminated?

    Thanks in advance!

  2. #2
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Presently, I can think of only two things you can do:

    1. Inform [Debian?] of the problem/bug/error/etc.
    2. Put the arguments in.

  3. #3
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    I tried using the arguments (split(/\s/, $_), but the same results occur.
    =(

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Can you give us an example?

    Code:
    #!/usr/bin/perl
    
    $_ = "speak friend and enter";
    
    @words = split;
    
    for(@words)
    {
        print;
    }
    Should print:
    speakfriendandenter

    Code:
    alex@tux ~/test $ ./perl_test
    speakfriendandenter

  5. #5
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Quote Originally Posted by Javasnob
    I tried using the arguments (split(/\s/, $_), but the same results occur.
    =(
    Interesting...

    Yes - like Cabhan said - Can you give us an example? Also, what version of PERL?

  6. #6
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Here's the full code:

    Code:
    #!/usr/bin/perl
    # lfanalyser.pl
    # This script analyses a text file for letter counts.
    
    # Declare and initialize totals
    $total = 0;
    $initial = 0;
    $medial = 0;
    $final = 0;
    # Initialize the hash
    %frequencies = ();
    while&#40;<>&#41; &#123;
    	chomp;
    	@words = split&#40;/\s/, $_&#41;; #Why doesn't this work correctly?
    	foreach $word &#40;@words&#41; &#123;
    		for&#40;$i = 0; $i < length; $i++&#41; &#123;
    			$character = substr&#40;$_, $i, 1&#41;;
    			next if&#40;ord&#40;$character&#41; == 9&#41;;
    			next if&#40;ord&#40;$character&#41; == 32&#41;;
    			unless&#40;exists $frequencies&#123;$character&#125;&#41; &#123;
    				$frequencies&#123;$character&#125; = &#91;0, 0, 0, 0&#93;;				
    			&#125;
    			$total++;
    			$frequencies&#123;$character&#125;->&#91;3&#93; = $frequencies&#123;$character&#125;->&#91;3&#93; + 1;
    			if&#40;$i == 0&#41; &#123;
    				$initial++;
    				$frequencies&#123;$character&#125;->&#91;0&#93; = $frequencies&#123;$character&#125;->&#91;0&#93; + 1;
    			&#125; elsif&#40;&#40;$i == &#40;length&#40;$_&#41; - 1&#41;&#41; and &#40;&#40;length&#40;$_&#41; != 1&#41;&#41;&#41; &#123;
    				$final++;
    				$frequencies&#123;$character&#125;->&#91;2&#93; = $frequencies&#123;$character&#125;->&#91;2&#93; + 1;
    			&#125; else &#123;
    				$medial++;
    				$frequencies&#123;$character&#125;->&#91;1&#93; = $frequencies&#123;$character&#125;->&#91;1&#93; + 1;
    			&#125;
    		&#125;
    	&#125;
    &#125;
    
    print "Total&#58; $total \n";
    print "Initial&#58; $initial \n";
    print "Medial&#58; $medial \n";
    print "Final&#58; $final \n";
    foreach $key &#40;sort &#123;$a cmp $b&#125; keys&#40;%frequencies&#41;&#41; &#123;
    	@array = @&#123;$frequencies&#123;$key&#125;&#125;;
    	$array&#91;0&#93; /= $initial;
    	$array&#91;0&#93; *= 100;
    	
    	$array&#91;1&#93; /= $medial;
    	$array&#91;1&#93; *= 100;
    	
    	$array&#91;2&#93; /= $final;
    	$array&#91;2&#93; *= 100;
    	
    	$array&#91;3&#93; /= $total;
    	$array&#91;3&#93; *= 100;
    	printf "%s %d %5.2f %5.2f %5.2f %5.2f \n", $key, ord&#40;$key&#41;, $array&#91;0&#93;, $array&#91;1&#93;, $array&#91;2&#93;, $array&#91;3&#93;;
    &#125;
    It's just a program I'm writing to get used to Perl, and perl --version says it's v5.8.7. Thanks again for your help guys!

  7. #7
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Try something -- place the following line in your code just after the 'split' line:

    printf("^%s^\n", join('^', @words));

    and run/post.

  8. #8
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    If this weren't my first Perl program, I'd feel incredibly stupid.

    split wasn't the problem; this section of code was:
    Code:
    for&#40;$i = 0; $i < length; $i++&#41; &#123;
             $character = substr&#40;$_, $i, 1&#41;;
             next if&#40;ord&#40;$character&#41; == 9&#41;;
             next if&#40;ord&#40;$character&#41; == 32&#41;;
             unless&#40;exists $frequencies&#123;$character&#125;&#41; &#123;
                $frequencies&#123;$character&#125; = &#91;0, 0, 0, 0&#93;;            
             &#125;
             $total++;
             $frequencies&#123;$character&#125;->&#91;3&#93; = $frequencies&#123;$character&#125;->&#91;3&#93; + 1;
             if&#40;$i == 0&#41; &#123;
                $initial++;
                $frequencies&#123;$character&#125;->&#91;0&#93; = $frequencies&#123;$character&#125;->&#91;0&#93; + 1;
             &#125; elsif&#40;&#40;$i == &#40;length&#40;$_&#41; - 1&#41;&#41; and &#40;&#40;length&#40;$_&#41; != 1&#41;&#41;&#41; &#123;
                $final++;
                $frequencies&#123;$character&#125;->&#91;2&#93; = $frequencies&#123;$character&#125;->&#91;2&#93; + 1;
             &#125; else &#123;
                $medial++;
                $frequencies&#123;$character&#125;->&#91;1&#93; = $frequencies&#123;$character&#125;->&#91;1&#93; + 1;
             &#125;
          &#125;
    Mistakes:
    -The for loop incremented $i until it reached the length of $_, not $word.
    -$character contained substrings of $_, which obviously contained whitespace, not substrings of $word.
    -The incrementing of the final count worked based on the length of $_.

    Thanks for all of your help; I feel stupid, but I'm glad I made this mistake because now I'll probably never make it again.

  9. #9
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Heh, don't worry.

    As a note, if you're interested in learning Perl, pick up "Learning Perl" from O'Reilly. A GREAT book.

  10. #10
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Hey - don't feel too bad - everyone has to start somewhere. Besides, if this is your first PERL program, I think you're probably doing very well - I imagine most people start with "Hello World"... (Of course, I understand you going for something "with a little more meat in it" if you have written any kind of code before now.)

    I have been writing code for years, and I still occasionally find myself doing something "stupid" -- at work I write code in multiple programming/scripting languages (PERL is one of them) - sometimes [I guess I forget] which one I am writing, and use the syntax of one of the other ones... Fun, fun, fun... But I love writing code and have a blast with it.

    The point is, what matters is: Learn from your mistakes, Improve, Move on... (And, when you have the occasion, learn from other people's mistakes - before you have to make them yourself. Albeit, you will probably learn more from your own mistakes.)

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •