Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

How to retrieve the string after the space???

by sam_0056 (Initiate)
on Sep 07, 2006 at 12:05 UTC ( #571666=perlquestion: print w/replies, xml ) Need Help??

sam_0056 has asked for the wisdom of the Perl Monks concerning the following question:

i have a file which contents this data.
To:sanju <sip:san@> From: paka<sip:sam@> Contact: sanjay<sip:san@>

Using file read i read each line.
my $data='ink.txt'; open DATA, "$data" or die "can't open $data $!"; my @array_of_data = <DATA>; foreach my $line (@array_of_data) { if ($line =~ m/To:/i) { $to=$line; print "To is: $to\n"; } if ($line =~ m/From:/i) { $from=$line; print "From is: $From\n"; } }

So that i retive the paticular line.Now i want from the To line=To:sanju <sip:sanjay@> it just retive the <sip:san@> which is after the space.Plz give some hint do the same.

Replies are listed 'Best First'.
Re: How to retrieve the string after the space???
by davorg (Chancellor) on Sep 07, 2006 at 12:17 UTC

    Looks like you're parsing email messages. Have you considered using Email::Simple?

    Alternatively, you could change your code to something like this (untested):

    if ($line =~ m/^To:\s+(\S+)/i) { $to = $1; print "To is: $to\n"; }

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How to retrieve the string after the space???
by rsriram (Hermit) on Sep 07, 2006 at 13:03 UTC

    Hi, You just need to have a small correction in you if condition.

    if ($line =~ m/To:/i) { if ($line =~ m/<([^>]+)>/i) {print "<$1>\n"} }

    In the above statement, after checking whether the line contains To:, I am using a regex for searching the text within open and close delimiters and printing the searched text within < and >.

Re: How to retrieve the string after the space???
by McDarren (Abbot) on Sep 07, 2006 at 14:30 UTC
    Just for the sake of TIMTOWTDI, here is an alternative approach that you could take to this task.
    #!/usr/bin/perl -w use strict; chomp(my @lines = <DATA>); my %results; for my $line (@lines) { if (my ($field, $data) = $line =~ m/^(\w+).+?(<.+?>)$/) { $results{$field} = $data; } } for my $field (keys %results) { print "$field is $results{$field}\n"; } __DATA__ To:sanju <sip:san@> From: paka<sip:sam@> Contact: sanjay<sip:san@>
    Which prints..
    Contact is <sip:san@> To is <sip:san@> From is <sip:sam@>

    The advantage of an approach such as this is that you do away with your if/else conditionals, so it becomes more scalable. For example, if you suddenly had to extract 100 sets of data instead of three, you wouldn't need to alter the code at all. But using your original approach, you'd need to add a separate conditional statement for each of the 100 possibilities.

    Explaining the pattern match in that code...

    ($field, $data) = $line =~ m/^(\w+).+?(<.+?>)$/
    • match the beginning of the string , and then start capturing (  ^()
    • continue to capture as long as there are "word" characters (  \w)
    • assign whatever has been captuted to $field
    • match one or more of anything until we see a "<", and then start capturing again
    • match one or more of anything, and keep capturing until we see a ">"
    • assign this second capture to $data
    • match the end of the string (  $)

    The anchors (^ and $) are probably not completely necessary in this case, however it's a good habit to anchor your pattern matches where ever possible, as it speeds things up.

    Hope this helps,
    Darren :)

Re: How to retrieve the string after the space???
by DentArthurDent (Monk) on Sep 07, 2006 at 16:07 UTC

    if ($line =~ m/To:/i) { ($header,$to) = split(/ /,$line); print "To is: $to\n"; }
    The split function splits the line apart into an array in this case by spaces. Note that this could cause you pain if your addresses contain spaces!
    My mission: To boldy split infinitives that have never been split before!
Re: How to retrieve the string after the space???
by graff (Chancellor) on Sep 08, 2006 at 01:27 UTC
    Maybe you want to use a hash to store your address values... that would tend to make things simpler overall:
    my %address; while (<DATA>) { if ( /^(To|From|Contact):.*?(<.*?>)/ ) { $address{$1} = $2; } } for my $field ( qw/To From Contact/ ) { if ( exists( $address{$field} )) { print "The $field address is $address{$field}\n"; } }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://571666]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-02-04 06:10 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.