LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2005, 04:18 PM   #1
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Rep: Reputation: 30
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!
 
Old 11-03-2005, 06:01 PM   #2
astorm
LQ Newbie
 
Registered: Nov 2005
Distribution: Slackware
Posts: 23

Rep: Reputation: 15
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
 
Old 11-03-2005, 07:17 PM   #3
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
Sorry I was not clear.

I need distinguish between

something something

and

something else

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

basically i just need to find out how to match a string with a space between the words.
 
Old 11-04-2005, 02:27 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
example code, example data
 
Old 11-04-2005, 03:36 PM   #5
butters64
LQ Newbie
 
Registered: Nov 2005
Distribution: Mandrake LE2003 64
Posts: 13

Rep: Reputation: 0
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.
 
Old 11-07-2005, 01:09 PM   #6
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
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?
 
Old 11-08-2005, 03:17 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.

Last edited by bigearsbilly; 11-08-2005 at 03:43 AM.
 
Old 11-08-2005, 03:43 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
This is better,
Code:
for $i (1..5) {
   $line = <STDIN>;
   # do stuff to $line
   print $line;
}
 
Old 11-08-2005, 07:55 AM   #9
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
thanks!

i think i need to read more about the <STDIN> to understand this, i spent a few hours yesterday learning
Code:
..
 
Old 11-08-2005, 08:59 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

of course true for any normal filehandle
 
Old 11-21-2005, 10:53 AM   #11
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
An update:

I got everything working the way i wanted.

Thanks again for the help.

Perl just keeps getting sweeter and sweeter
 
Old 12-09-2005, 12:21 PM   #12
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
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
 
Old 12-09-2005, 01:13 PM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
maybe try the s modifier (man perlre for more infos)
Code:
$reqt = $1 if $s =~ /Front(.*)Back\n/s;
 
Old 12-12-2005, 04:10 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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)

Last edited by bigearsbilly; 12-12-2005 at 04:16 AM.
 
Old 12-12-2005, 06:27 AM   #15
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
Thanks to both, i added the "s" like above and it worked, i will be checking this example today though.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl question nooodles Programming 5 09-22-2005 02:01 AM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
Perl Question cbarone Linux - Software 1 12-17-2003 10:25 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Perl question oulevon Programming 12 05-31-2001 06:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration