develooper Front page | perl.beginners | Postings from April 2010

Re: Extract substring from offset to space or full stop

Thread Previous | Thread Next
From:
Akhthar Parvez K
Date:
April 18, 2010 12:10
Subject:
Re: Extract substring from offset to space or full stop
Message ID:
201004190040.00866.akhthar@sysadminguide.com
>    $str =~ m{ \A ( .{0,15} .*? ) \s }msx;

Yeah, this would do. I talked about the scenario where you didn't put "{0,15}", but just "{15}".  In that case, it wouldn't work if the value given in the match string (15 as per above eg.) is greater than the character count of the particular string (36).

<Code>
my $str = "The black cat climbed the green tree";
print "string: $str\n";

$str =~ m{ \A ( .{50} .*? ) \s }msx;
my $extracted = $1;
print "extracted: $extracted\n";
<?Code>

Result:
string: The black cat climbed the green tree
Use of uninitialized value in concatenation (.) or string at scripts/test/test2.pl line 14.
extracted:

But it worked for the OP (the above condition may not have been required for him) and that's what is important :-)

Regards,
Akhthar Parvez K
http://Tips.SysAdminGUIDE.COM
UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity - Dennie Richie
On Sunday 18 Apr 2010, Shawn H Corey wrote:
> Akhthar Parvez K wrote:
> > Hi Shawn,
> > 
> >> $str =~ m{ \A ( .{15} .*? ) \s }msx;
> > 
> > I don't think this would work if the value given in the match string (15 as per above eg.) is greater than the character count of the particular string. Right?
> 
> No, it will fail if $str is less than 15 characters.  Try:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> for my $str ( "The black cat climbed the green tree", 'A test' ){
>    my $extracted = extract( $str );
>    print "string: $str\n";
>    print "string: $extracted\n";
> }
> 
> sub extract {
>    my $str = shift @_;
> 

>    my $extracted = $1;
> 
>    return $extracted;
> }
> 
> __END__
> 
> -- 
> Just my 0.00000002 million dollars worth,
>    Shawn
> 
> Programming is as much about organization and communication
> as it is about coding.
> 
> I like Perl; it's the only language where you can bless your
> thingy.
> 
> Eliminate software piracy:  use only FLOSS.
> 



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About