Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: $/ question

by kyle (Abbot)
on Jan 13, 2008 at 03:47 UTC ( #662151=note: print w/replies, xml ) Need Help??


in reply to Re^3: $/ question
in thread $/ question

You're setting a local value to $/ only inside the read loop. That is, the value gets set after the first record is read. At the end of the loop, it's set back to its default value ("\n"), and the next record is read with that value. Then inside the loop, you set the local value again. Try it this way:

sub slurpie { local $/ = ''; # paragraph mode while (<DATA>) { next unless /^\w\w.+(\w\w).+(\d+).+(\w\w)/sg; #should only ma +tch first paragraph print "$_\n"; } }

Replies are listed 'Best First'.
Re^5: $/ question
by convenientstore (Pilgrim) on Jan 13, 2008 at 03:55 UTC
    I understand what you are saying but that didn't solve it for some reason
    
               local $/ = "";   # put into paragraph mode (separated by one or more blank lines
    #!/usr/bin/perl -w
    
    use strict;
    
    sub slurpie {
          local $/ = "";   # put into paragraph mode (separated by one or more blank lines
          while (<DATA>) {
               next unless /^\w\w.+(\w\w).+(\d+).+(\w\w)/sg;  #should only match first paragraph
               print "$_\n";
          }
    }
    
    
    slurpie();
    
    __DATA__
    hi
    hi
    234
    hi
    
    hoi
    sdfsdfsdf23423
    hi
    
    hi
    hi
    1234
    
    1
    
    :!././././././././././perl_slurp.pl
    hi
    hi
    234
    hi
    
    
    hoi
    sdfsdfsdf23423
    hi
    
    
    hi
    hi
    1234
    

      Your pattern (/^\w\w.+(\w\w).+(\d+).+(\w\w)/sg), which "should only match the first paragraph", actually matches them all (except the one that's only "1").

      /^\w\w.+(\w\w).+(\d+).+(\w\w)/sg h i\n h i \n 234 \n h i /^\w\w.+(\w\w).+ (\d+) .+(\w\w)/xsg h o i\ns d fsdfsdf 23423\n h i /^\w\w.+(\w\w).+(\d+).+(\w\w)/sg h i \n h i \n 1 2 3 4

      Try just enumerating the paragraphs:

      sub slurpie { local $/ = ''; my $n = 1; while (<DATA>) { printf "*** paragraph %d ***\n", $n++; print; } } slurpie(); __END__ *** paragraph 1 *** hi hi 234 hi *** paragraph 2 *** hoi sdfsdfsdf23423 hi *** paragraph 3 *** hi hi 1234 *** paragraph 4 *** 1
        You are right
        thanks

        #!/usr/bin/perl -w
        
        use strict;
        
        sub slurpie {
              local $/ = '';   # put into paragraph mode (separated by one or more blank lines
              while (<DATA>) {
                   next unless /^\w\w.+(\w\w).+(\b\d\d\d\b).+(\w\w)/sg;  #should only match first paragraph
                   print "$_\n";
              }
        }
        
        
        slurpie();
        
        __DATA__
        hi
        hi
        234
        hi
        
        hoi
        sdfsdfsdf23423
        hi
        
        hi
        hi
        1234
        
        1
        
        
        :!././././././././././perl_slurp.pl
        hi
        hi
        234
        hi
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://662151]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2018-11-15 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (189 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!