For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > perl on strings









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl on strings
Nishi

2007-04-19, 6:58 pm

Hi:

I have a strings such as
Formatting_l_cs.cat
Formatting_l_da.cat
Formatting_l_de.cat
Formatting_l_zh-tw.cat
I need to extract the substring before the "." and after the last occurence
of "_" ie in the above cases, it would return "cs" or zh-tw" etc.

How can I achieve this?

Thanks!

Yitzle

2007-04-19, 6:58 pm

$strings = qw/Formatting_l_cs.cat Formatting_l_da.cat
Formatting_l_de.cat Formatting_l_zh-tw.cat/;
my $partStrings = ( $strings =~ /_.*?\./ )[0]; # Minimun from _ to .

This code is untested. The RegEx should work but the rest... not sure.
It would work in a loop, though.

Can someone explain why everyone's so into using qw/ / opposed to
quotes and commas?

On 4/19/07, Nishi <nishiprafull@gmail.com> wrote:
> Hi:
>
> I have a strings such as
> Formatting_l_cs.cat
> Formatting_l_da.cat
> Formatting_l_de.cat
> Formatting_l_zh-tw.cat
> I need to extract the substring before the "." and after the last occurence
> of "_" ie in the above cases, it would return "cs" or zh-tw" etc.
>
> How can I achieve this?
>
> Thanks!
>

Purl Gurl

2007-04-19, 6:58 pm

Nishi wrote:


> I have a strings such as


> Formatting_l_cs.cat
> Formatting_l_da.cat
> Formatting_l_de.cat
> Formatting_l_zh-tw.cat


> I need to extract the substring before the "." and after the last occurence
> of "_" ie in the above cases, it would return "cs" or zh-tw" etc.


#!perl

while (<DATA> )
{ print substr ($_, 13, rindex ($_, ".") - 13), "\n"; }

__DATA__
Formatting_l_cs.cat
Formatting_l_da.cat
Formatting_l_de.cat
Formatting_l_zh-tw.cat


PRINTED RESULTS:

cs
da
de
zh-tw


Purl Gurl
usenet@DavidFilmer.com

2007-04-19, 6:58 pm

On Apr 19, 2:20 pm, Purl Gurl <purlg...@purlgurl.net> wrote:
> [snip the usual crap code that I think it posts only to annoy people]


The OP is well advised to disregard anything that pg posts. Try
something like this instead:

while (<DATA> ) {
print (/.*_(.*)\./, "\n");
}

__DATA__
Formatting_l_cs.cat
Formatting_l_da.cat
Formatting_l_de.cat
Formatting_l_zh-tw.cat


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Rob Dixon

2007-04-19, 6:58 pm

Nishi wrote:
> Hi:
>
> I have a strings such as
> Formatting_l_cs.cat
> Formatting_l_da.cat
> Formatting_l_de.cat
> Formatting_l_zh-tw.cat
> I need to extract the substring before the "." and after the last occurence
> of "_" ie in the above cases, it would return "cs" or zh-tw" etc.
>
> How can I achieve this?


HTH,

Rob


use strict;
use warnings;

my @files = qw(
Formatting_l_cs.cat
Formatting_l_da.cat
Formatting_l_de.cat
Formatting_l_zh-tw.cat);

foreach (@files) {
my ($country) = /([^_]+)\./i;
print $country, "\n";
}

**OUTPUT**

cs
da
de
zh-tw
John W. Krahn

2007-04-19, 6:58 pm

yitzle wrote:
> $strings = qw/Formatting_l_cs.cat Formatting_l_da.cat
> Formatting_l_de.cat Formatting_l_zh-tw.cat/;


You are assigning a list to a scalar so that is equivalent to:

$strings = 'Formatting_l_zh-tw.cat';

> my $partStrings = ( $strings =~ /_.*?\./ )[0]; # Minimun from _ to .


The expression "$strings =~ /_.*?\./" returns true or false so $partStrings
will either be assigned the value 1 or the value ''.

> This code is untested. The RegEx should work but the rest... not sure.
> It would work in a loop, though.
>
> Can someone explain why everyone's so into using qw/ / opposed to
> quotes and commas?


Less punctuation.



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
John W. Krahn

2007-04-19, 6:58 pm

Nishi wrote:
> Hi:


Hello,

> I have a strings such as
> Formatting_l_cs.cat
> Formatting_l_da.cat
> Formatting_l_de.cat
> Formatting_l_zh-tw.cat
> I need to extract the substring before the "." and after the last occurence
> of "_" ie in the above cases, it would return "cs" or zh-tw" etc.
>
> How can I achieve this?


$ perl -le'
my @strings = qw(
Formatting_l_cs.cat
Formatting_l_da.cat
Formatting_l_de.cat
Formatting_l_zh-tw.cat
);
print /_([^_]+)\./ for @strings;
'
cs
da
de
zh-tw




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Purl Gurl

2007-04-19, 6:58 pm

usenet@DavidFilmer.com wrote:

> Purl Gurl wrote:


[color=darkred]
> The OP is well advised to disregard anything that pg posts.


Your historical and frequent childish trolling of this
discussion group benefits none.

Purl Gurl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com