print map question

2010-11-13 Thread Mike McClain
mike@/deb40a:~/perl perl -v
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

mike@/deb40a:~/perl perl -we '
@list=qw/Perl is cool./;print( list=\t, map { $_, } @list, \n);
'
list=   Perl,is,cool.,
,mike@/deb40a:~/perl

Could someone tell me why there is a comma printed after the newline?
Thanks,
Mike
-- 
Satisfied user of Linux since 1997.
O ascii ribbon campaign - stop html mail - www.asciiribbon.org

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: print map question

2010-11-13 Thread Uri Guttman
 MM == Mike McClain mike.j...@nethere.com writes:

  MM mike@/deb40a:~/perl perl -we '
  MM @list=qw/Perl is cool./;print( list=\t, map { $_, } @list, \n);
  MM '
  MM list=   Perl,is,cool.,
  MM ,mike@/deb40a:~/perl

  MM Could someone tell me why there is a comma printed after the newline?

because you put it there. the \n is input to the map, not the print!
map's last arg is a list and it takes @list AND anything else after
it. you need to either put the map in parens or make it a func like call
like this: map( $_,, @list ) so it can't eat the \n.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Perl, pattern matching, substitution

2010-11-13 Thread Zachary Brooks
Hello,

I'm taking a PhD course that requires the use of Perl and pattern matching.
I've taken on the motto divide and conquer, but it hasn't quite worked. I
appreciate anyone's help.

The task is to extract sentences from a relatively large text file (928K,
ca. 300 pages). But of course, the text file is messy. I've tried two
approaches.

1. My first approach was to use substitute to get rid of a range of things
between DOC and /DATELINE. A short version looks like this.

$hello = DOC man at the bar order the /DATELINE;
$hello =~ s/DOC.*\/DATELINE//gi;
print $hello\n;

This works until the code comes across a quotation mark (). So then I
replace double quotation marks () with single quotation marks ('). But then
as, I put more text under $hello, the code seems to break. For example,
running the substitution against the code below simply re-shows me the code.

DOC DOCNO WSJ890728-0079 /DOCNO
DD = 890728 /DD
AN 890728-0079. /AN
HL Major Deficit
@  Signaled by Sun /DATELINE


It doesn't remove everything between DOC and /DATELINE.


2. My second approach was to simply find what I wanted using matching and
ignoring deleting. A short version looks like this.

$mystring = TEXTSun Microsystems Inc. said it will post a
larger-than-expected fourth-quarter loss of as much as $26 million and may
show a loss in the current first quarter, raising further troubling
questions about the once high-flying computer workstation maker.   /TEXT
.;

if($mystring =~ m/TEXT(.*?)\/TEXT/) {
 print $1;
}


This works. Again I change the double quotation marks for the single
quotation marks. But once again when I include more data with line breaks,
the code breaks. This is the first part of a 5-part question. Very
frustrating. Every university should have Perl tutors just as they have (or
should have) language tutors.

Cheers,

Zach
-- 
--
Zachary S. Brooks
PhD Student in Second Language Acquisition and Teaching (SLAT)
The University of Arizona - http://www.coh.arizona.edu/slat/
Graduate Associate in Teaching - Department of English
M.A. Applied Linguistics -  University of Massachusetts Boston
---


Re: Perl, pattern matching, substitution

2010-11-13 Thread Shawn H Corey

On 10-11-13 01:42 PM, Zachary Brooks wrote:

1. My first approach was to use substitute to get rid of a range of things
betweenDOC  and/DATELINE. A short version looks like this.

$hello = DOC  man at the bar order the/DATELINE;
$hello =~ s/DOC.*\/DATELINE//gi;
print $hello\n;


I was about to say that you should use a module to parse your files but 
from what little you posted, it does not look like it's XML or even 
SGML.  Do you know what format your data is in?



--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early  often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Perl, pattern matching, substitution

2010-11-13 Thread Sheppy R
I've had similar issues and the \Q \E flags didn't fix it.

One thing I've done to fix an issue where regex metacharacters are being
caught is to do a replace on all of the characters to include a \ right in
front.

Something like this:

open (my $FILE, , $file) or die $!\n;
my @lines = $FILE;
for (@lines)
{
  s|(['\+\$\(\)])|\$1|g;
}

*go back to regular coding.*

The ( ) sets this block to the $1 variable (From your examples you are
already familiar with this).
The [ ] gives a list of characters that might be caught in the flag.
The \ in the set are just there as escape characters.

It is an ugly brute force method, but it has worked for me before.

On Sat, Nov 13, 2010 at 2:08 PM, Shawn H Corey shawnhco...@gmail.comwrote:

 On 10-11-13 01:42 PM, Zachary Brooks wrote:

 1. My first approach was to use substitute to get rid of a range of things
 betweenDOC  and/DATELINE. A short version looks like this.

 $hello = DOC  man at the bar order the/DATELINE;
 $hello =~ s/DOC.*\/DATELINE//gi;
 print $hello\n;


 I was about to say that you should use a module to parse your files but
 from what little you posted, it does not look like it's XML or even SGML.
  Do you know what format your data is in?


 --
 Just my 0.0002 million dollars worth,
  Shawn

 Programming is as much about organization and communication
 as it is about coding.

 The secret to great software:  Fail early  often.

 Eliminate software piracy:  use only FLOSS.

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/





Re: Perl, pattern matching, substitution

2010-11-13 Thread Rob Dixon

On 13/11/2010 18:42, Zachary Brooks wrote:

Hello,

I'm taking a PhD course that requires the use of Perl and pattern matching.
I've taken on the motto divide and conquer, but it hasn't quite worked. I
appreciate anyone's help.

The task is to extract sentences from a relatively large text file (928K,
ca. 300 pages). But of course, the text file is messy. I've tried two
approaches.

1. My first approach was to use substitute to get rid of a range of things
betweenDOC  and/DATELINE. A short version looks like this.

$hello = DOC  man at the bar order the/DATELINE;
$hello =~ s/DOC.*\/DATELINE//gi;
print $hello\n;

This works until the code comes across a quotation mark (). So then I
replace double quotation marks () with single quotation marks ('). But then
as, I put more text under $hello, the code seems to break. For example,
running the substitution against the code below simply re-shows me the code.

DOC  DOCNO  WSJ890728-0079/DOCNO
DD  = 890728/DD
AN  890728-0079./AN
HL  Major Deficit
@  Signaled by Sun/DATELINE


It doesn't remove everything betweenDOC  and/DATELINE.


2. My second approach was to simply find what I wanted using matching and
ignoring deleting. A short version looks like this.

$mystring = TEXT Sun Microsystems Inc. said it will post a
larger-than-expected fourth-quarter loss of as much as $26 million and may
show a loss in the current first quarter, raising further troubling
questions about the once high-flying computer workstation maker./TEXT
.;

if($mystring =~ m/TEXT(.*?)\/TEXT/) {
  print $1;
}


This works. Again I change the double quotation marks for the single
quotation marks. But once again when I include more data with line breaks,
the code breaks. This is the first part of a 5-part question. Very
frustrating. Every university should have Perl tutors just as they have (or
should have) language tutors.


Hi Zach

First of all, it seems that you are processing XML files. The best way 
by far to achieve this is to use one of the Perl XML libraries such as 
XML:LibXML or XML:Twig. However, if the changes you are making are 
minimal and simple then it is possible that an approach using regex 
substitutions is valid.


Also note that if you delete everything between and including a DOC 
tag and a /DATELINE tag then what you would be left with is not valid XML.


The code you have shown is failing because /./ matches all characters 
except \n. Employing the /s modifier changes this behaviour to match 
any character at all, so


  $hello =~ s/DOC.*?\/DATELINE//gis;

and

  $mystring =~ m/TEXT(.*?)\/TEXT/s;

will have the effect you intend.

I think your second approach is more likely to provide the better 
solution, but it is difficult to judge without knowing more about your 
data and the requirement.


Remember that, once you are reading data from real XML files, your 
problem with double quotes will disappear: it applies only when you are 
putting string data into your program for test purposes. Also, remember 
the q() and qq() operators to avoid problems with the string delimiter 
appearing within the string. Look for Quote and Quote-like Operators in


  perldoc perlop

HTH,

- Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Help me learn Closures.

2010-11-13 Thread Randal L. Schwartz
 Matthew == Matthew Young mab...@gmail.com writes:
Matthew What are closures? How are they used? When should they be used? Where
Matthew can I learn more about them?

If you can borrow (or buy :) a copy of Intermediate Perl, I have an
entire section on closures in there.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
mer...@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Bit testing

2010-11-13 Thread C.DeRykus
On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote:
 I'm almost embarrassed to ask this, but I can't figure out a simple way to 
 construct a switch ('given') statement where the 'when' clauses involve 
 bit-testing.

 Here's the only way I've figured out to build a switch statement that does 
 the trick.  It seems unusually wordy, which makes me think there must be a 
 simpler way to test for certain bit combinations.  Any suggestions?

 Thanks,
 Chap

 #!/usr/bin/perl                                                               
                                                                               
                                              

 use strict;
 use warnings;
 use feature :5.10;

 # Here are masks for various bit combos of interest:                          
                                                                               
                                               

 my $one_three        = 0b1010; # bits 1 and 3 (counting from 0, right to 
 left)                                                                         
                                                
 my $zero_four        = 0b00010001; # bits 0 and 4                             
                                                                               
                                              
 my $five             = 0b0010; # bit 5                                    
                                                                               
                                               

 # Here we will test several bit fields for bit combos of interest:            
                                                                               
                                               

 for my $flags ( 0b10111010, 0b10111000, 0b10010010) {

     my $asbits = sprintf(0b%08b, $flags); # prepare bits for 
 pretty-printing                                                               
                                                              

     given ( $flags ) {
         when ( ($_  $one_three) == $one_three ) {  # bits one and three are 
 on                                                                            
                                               
             say $asbits has bits 1 and 3;
         }
         when ( ($_  $zero_four) == $zero_four ) { # bits zero and four are 
 on                                                                            
                                                 
             say $asbits has bits 0 and 4;
         }
         when ( ($_  $five) == $five ) { # bit five is on                     
                                                                               
                                              
             say $asbits has bit 5;
         }
         default {
             say $asbits has no interesting bit patterns.;
         }
     }

 }

Not lots shorter but you could use a closure to hide
the calculation:

my $mask;
for my $flags ( ... ) {
 $mask = sub { return ($flags  $_[0]) == $_[0] }
unless $mask;
 given( $flags ) {
when ( $mask-($one_and_three)  ) { ... }
when ( $mask-($zero_and_four)   ) { ... }
...
 }
}

--
Charles DeRykus