LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Question (https://www.linuxquestions.org/questions/programming-9/perl-question-379705/)

rjcrews 11-03-2005 04:18 PM

Perl Question
 
Everyone loves perl right?

I am trying to learn some perl, so i am using it to parse email (Net::Pop3)

everything is fine, but i cannot figure out how to grab a line with


something something

instead of

something else

so if i have

Code:

if($line =~ m/^something (.*)/){
              $variable =1;
              $variable = substr($variable,0 ,500);
}

what do i do to just get

something something

?

Thanks in advance!

astorm 11-03-2005 06:01 PM

What exactly is the line your'e trying to match? By "something something", do you mean you want to match a string, then whitespace, then the repeated string? If you're trying to match an email address, do
Code:

m/^\S+@\S+\.\S+$/i
which should work. A good explanation of regular expression can be found by googling "perl regular expressions" and clicking the first result.
Also, why did you set $variable to 1 and then do a 500-character substring on it?

Best,
Alek

rjcrews 11-03-2005 07:17 PM

Sorry I was not clear.

I need distinguish between

something something

and

something else

I did that bc that was the example i found. :confused: (set it to 1)

basically i just need to find out how to match a string with a space between the words.

bigearsbilly 11-04-2005 02:27 AM

example code, example data

butters64 11-04-2005 03:36 PM

Quote:

basically i just need to find out how to match a string with a space between the words.
If you want to match the string "something", followed by a space, followed by any word, you can use:

Code:

m/^something ([\w]+)/
Also, I think you want $variable = $1 instead of $variable = 1. $1 is a special variable that means "the stuff matched in the first parentheses".

So the following code:

Code:

$line = "something else";
if($line =~ m/^something ([\w]+)/){
              print $1;
}

will print "else".

Hope this helps.

rjcrews 11-07-2005 01:09 PM

Thanks!

ive gotten this to do everything i need, except for 1 thing.

how do i grab 5 line?

like if i just want to get the first 5 lines under the one i am matching, how do i do this?

currently:
Code:

  elsif( $line =~ m/^\s+\Wdate\W (.*)/)
will get me what i want from the date, is there a simple way to just grab 5 lines under it?

bigearsbilly 11-08-2005 03:17 AM

how about?

Code:

($one, $two, $three, $four, $five) = <STDIN>;
but this also appears to close <STDIN> so any lines after
are discarded. May not be what you want?

EDIT: This is not a good solution.

bigearsbilly 11-08-2005 03:43 AM

This is better,
Code:

for $i (1..5) {
  $line = <STDIN>;
  # do stuff to $line
  print $line;
}


rjcrews 11-08-2005 07:55 AM

thanks!

i think i need to read more about the <STDIN> to understand this, i spent a few hours yesterday learning
Code:

..

bigearsbilly 11-08-2005 08:59 AM

it's easy
$thing = <STDIN>
reads a line
@thing = <STDIN>
slurps up the lot.

of course true for any normal filehandle

rjcrews 11-21-2005 10:53 AM

An update:

I got everything working the way i wanted.

Thanks again for the help.

Perl just keeps getting sweeter and sweeter

rjcrews 12-09-2005 12:21 PM

Another small issue:
Code:

    if($blob =~ m/Front(.*?)Back\n/) {
        $reqt =$1;
}

works with 1 line, how do you make it go over multiple lines?

thanks

keefaz 12-09-2005 01:13 PM

maybe try the s modifier (man perlre for more infos)
Code:

$reqt = $1 if $s =~ /Front(.*)Back\n/s;

bigearsbilly 12-12-2005 04:10 AM

Code:

#!/usr/bin/perl -wn

BEGIN {
undef $/;  # this undefs newline as input record seperator (you need it)
}

print $1 while  m/Front(.*?)Back/sg ;

Code:

billym.>cat 1
Front
lah
dee
dah
Back
billym.>multi-line-match.pl 1
 
lah
dee
dah
billym.>

Note, we have kept the first newline at the end of 'Front'.

I have used the -n switch here for the sed-like implicit
loop, just for terse-ness (?) in my example.
Hence the BEGIN block (otherwise it would undef the $\ for each line)

rjcrews 12-12-2005 06:27 AM

Thanks to both, i added the "s" like above and it worked, i will be checking this example today though.


All times are GMT -5. The time now is 04:18 PM.