Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

separating two sententences

by Anonymous Monk
on Sep 16, 2011 at 03:14 UTC ( #926294=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am comlpletly new in perl and thank you for your help.i have a file

117nolasvegasnv@fitness19.com;"117nolasvegasnv" 15tofit@comcast.net;"Patrea" 1800noda@gmail.com;"1800noda" 188rivervalenj@fitness19.com;"188rivervalenj" 199pittsburghpa@fitness19.com;"199pittsburghpa" 1advocare@msn.com;"1advocare" 1mbabic@gmail.com;"Milos"

i have written this code for separating the second part but it doesnt work,please help me

#!/usr/bin/perl -w open(IN,"<file") ; while(<IN>){ if(/("/w+/")/){ print "$1\n"; }}

Comment on separating two sententences
Select or Download Code
Re: separating two sententences
by dreadpiratepeter (Priest) on Sep 16, 2011 at 03:35 UTC

    When you say "doesn't work" it tells us nothing. Try telling us what your expected output and actual output were.

    For starters, I would add:

    use strict; use warnings;
    to your script make coding errors more obvious. and I would hazard a guess that you are not opening the file properly. checking the return code of the open would find that:
    open IN,"<","file" or die "error opening file: $!";


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

      thank you for your paying attention .i want to create an excel file that shows emails in one column and name in another column.

      17nolasvegasnv@fitness19.com 117nolasvegasnv 15tofit@comcast.net Patrea 1800noda@gmail.com 1800noda 188rivervalenj@fitness19.com 188rivervalenj 199pittsburghpa@fitness19.com 199pittsburghpa 1advocare@msn.com 1advocare" 1mbabic@gmail.com Milos
        and you still have not told us what is not working. What happens when you run it? I spend all day with a QA department that thinks "doesn't work" is a legitimate bug report. I don't need it on the monks too ;)


        -pete
        "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
        i want to create an excel file ...

        This tells us your ultimate goal, but does not describe what you expect the code of the OP to do and just what is going wrong. dreadpiratepeter was trying to get details of this kind.

        It seems that the OP code is a first attempt at reading the contents of a file and processing and printing the contents. This is a good first step to your final goal. As it stands, I can make a guess at what is happening. The regex  /("/w+/")/ has extraneous / (forward slash) characters in it that are interpreted as premature terminations of the regex (see muba's analysis below). Try something like this (untested):

        use warnings; use strict; my $filename = 'myfile'; open my $fh, '<', $filename or die "opening '$filename': $!"; while (<$fh>) { if (m{ ("\w+") }xms) { print "$1 \n"; } } close $fh;
Re: separating two sententences
by muba (Priest) on Sep 16, 2011 at 04:13 UTC

    One thing that's not doing what you want is the if(/("/w+/")/){ line. Let's analyze it.

    # if. Nothing weird yet. # | # | Okay. A regex. But where's the ")"? # | | # | | What's this? ;) # | | | # | | | End of condition part. # | | | | # | | | | A random slash # | | | | | # | | | | | A random closing paren. # | | | | | | if( /("/ w+/" ) / ) {

    What are those inner slashes doing there anyway?

      yes, you are right.i changed it to this but still nothing

      #!/usr/bin/perl -w open IN,"<","file" or die "error opening file: $!"; while(<IN>){ if(/"\(\w+\)"\/){ print "$1\n"; }}
        /"\(\w+\)"\/

        In this regex, the '(' and ')' (parenthesis) characters, which would normally be regex metacharacters marking the start and end of a capture group, are escaped by \ (backslash) characters that return them to being literal '(' and ')' characters. Again, see muba's analysis above Update: ...and reply below. (Missed that last \. Bad case of backslashitis.).

        You need to reread dreadpiratepeter's node again: Re^5: separating two sententences. Your input file just needs a split on the semicolon to get the two columns, then a regular expression to remove the quotes. Print those out separated by a comma or tab, and then Excel will read the file

Re: separating two sententences
by pvaldes (Chaplain) on Sep 16, 2011 at 21:03 UTC
    simply read line to line and split by ";", then print the second variable

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-07-28 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls