Complete.Org: Mailing Lists: Archives: discussion: April 2000:
[aclug-L] Re: Another perl question!
Home

[aclug-L] Re: Another perl question!

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: discussion@xxxxxxxxx
Subject: [aclug-L] Re: Another perl question!
From: Steven Saner <ssaner@xxxxxxxxxxxxxxx>
Date: Sun, 16 Apr 2000 19:37:03 -0500
Reply-to: discussion@xxxxxxxxx

On Sun, Apr 16, 2000 at 05:29:42PM -0500, Mohammad Islam wrote:
> Hello,
> 
> I have got another perl question if you guys don't mind.
> 
> Say I have a gif file named kswichita12345.gif
> 
> Is there a way i can split it up into four part?
> 
> To elaborate....
> 
> After splitting kswichita12345.gif, i should have..
> 
> ks -->1st part
> wichita -->second part
> 12345 -->third part
> gif -->fourth part
> 
> I can split it into two part as below:
> 
> $filename = "kswichita12345.gif";
> 
> ($name, $ext) = split(/\./, $filename);
> 
> So i have $name = kswichita12345 and $ext = gif.
> 
> Now how can I break $name into three separate string (i.e. ks, wichita,
> 12345)?
> 
> 
> Any pointers would be very much appreciated.
> 
> Thanks.
> 
> Mohammed.


Well, it depends a little bit on why you choose to break the name up
where you did. If you can come up with a pattern for where you want to
split it, then you can use a feature of pattern matching. For example
if you want the first token to be the first two characters of the
word, the second token is the remaining letters, and the last one is
any numbers at the end of the word, you could do:


$var =~ m/^(.{2})([A-Za-z]+)([0-9]+)$/
$t1 = $1;
$t2 = $2;
$t3 = $3;

This is actually exactly what we are covering in the perl class this
week. If you have a copy of the Learning Perl book, take a look at
chapter 7.

There would be other ways of doing this as well.


> 
> -- This is the discussion@xxxxxxxxx list.  To unsubscribe,
> visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi

-- This is the discussion@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]