CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help with string

 



pgovind
New User

Apr 14, 2014, 5:53 AM

Post #1 of 10 (5574 views)
Help with string Can't Post

Hi,

I am first time poster.
I have this issue. I have a field with value,1:11:24:9:5057:0x00D13B0D:0x00D13B45 or 1:15:5:10:3582:0x00D13B24:0x00D13B09. I need to return everything before the 3rd colon, as in 1:11:24 or 1:15:5
I used SUBSTR(field,1,7) but it returns 1:15:5:
I need a function that works for both scenarios,without extra colons.

Thanks,

pg


FishMonger
Veteran / Moderator

Apr 14, 2014, 7:39 AM

Post #2 of 10 (5573 views)
Re: [pgovind] Help with string [In reply to] Can't Post

Please post your code and any errors/warnings that it produces.

As a hint, try using a regex or split with an array slice.


pgovind
New User

Apr 14, 2014, 8:10 AM

Post #3 of 10 (5571 views)
Re: [FishMonger] Help with string [In reply to] Can't Post

Hi,

Thanks for the reply.
There is no error as it returns the data but with a extra colon,when the field length is different.
I just use SUBSTR(field,1,7), in my SQL.
Also,I am not sure about using split in this case.

Thanks,
pg


FishMonger
Veteran / Moderator

Apr 14, 2014, 8:44 AM

Post #4 of 10 (5565 views)
Re: [pgovind] Help with string [In reply to] Can't Post


Quote
Also,I am not sure about using split in this case.


What do you mean?

Are you saying you don't know how to use the split function or are you saying you don't think it can be used to accomplish your needs?

Have you tried using a regex to capture the pattern you need?

I have withheld showing the solution you need because (1) you've posted this in the Intermediate area which we presume you have that level of Perl knowledge and (2) you haven't show your working code. Even if it doesn't produce the desired results, you should always post your code.

What does SQL have to do with your question?


(This post was edited by FishMonger on Apr 14, 2014, 8:53 AM)


Laurent_R
Veteran / Moderator

Apr 14, 2014, 10:16 AM

Post #5 of 10 (5559 views)
Re: [pgovind] Help with string [In reply to] Can't Post

Your task can be done really easily with a very simple regex:


Code
my $field_start; 
$field_start = $1 if $field = ~ /^(\d+:\d+:\d):/;


Edit: Sorry, small mistake, the pattern should really be:

Code
$field_start = $1 if $field = ~ /^(\d+:\d+:\d+):/;



(This post was edited by Laurent_R on Apr 14, 2014, 1:20 PM)


pgovind
New User

Apr 14, 2014, 10:44 AM

Post #6 of 10 (5555 views)
Re: [Laurent_R] Help with string [In reply to] Can't Post

Hi,

$field_start = $1 if $field = ~ /^(\d+:\d+:\d):/;
Not sure what $1 would contain.

To admit honestly, I was using perl a while ago.Been bit out of touch because of non-usage.Slowly remembering stuff.

Thanks,
pg


BillKSmith
Veteran

Apr 14, 2014, 11:32 AM

Post #7 of 10 (5554 views)
Re: [pgovind] Help with string [In reply to] Can't Post

$1 contains the substring which matches the part of the pattern between the first (in this case only) set of parenthesis.

With a minor correction, Laurent's regex will solve your current problem. It can also match invalid things (e.g. '0000000000:00000000000:0:any old text') which could cause you a problem.

The following 'solution' also solves the immediate problem, but is probably to restrictive. My third example fails even if it is valid.


Code
use strict; 
use warnings;
my @fields = (
'1:11:24:9:5057:0x00D13B0D:0x00D13B45',
'1:15:5:10:3582:0x00D13B24:0x00D13B09',
'11:5:5:10:3582:0x00D13B24:0x00D13B09',
);

foreach my $string (@fields) {
if ($string !~ /^(\d:\d\d:\d\d?):/){
warn "No match in '$string'\n";
next;
}
my $before = $1;
print $before, "\n";
}


OUTPUT:

1:11:24
1:15:5
No match in '11:5:5:10:3582:0x00D13B24:0x00D13B09'

We need a better description of your data to write a 'good' regex.
Good Luck,
Bill


pgovind
New User

Apr 14, 2014, 12:19 PM

Post #8 of 10 (5551 views)
Re: [BillKSmith] Help with string [In reply to] Can't Post

Hi Bill,

Thanks a lot, That worked!
One question
How will the pattern be if there is single digit as in the second example.
I mean
'1:11:24:9:5057:0x00D13B0D:0x00D13B45',
'1:1:25:10:3582:0x00D13B24:0x00D13B09',


Thanks,

pg


Laurent_R
Veteran / Moderator

Apr 14, 2014, 1:40 PM

Post #9 of 10 (5547 views)
Re: [pgovind] Help with string [In reply to] Can't Post

Hmm, the best would be for you to try and test, don't you think?

Or, even better, to try to undestand how regexes work.

To give you an easy answer, yes, it should work, because I crafted the regex to tackle that case. But the mere fact that you are asking this question seems to show that you are not really trying to understand regexes. Wrong approach. Regexes are just about as central to Perl as pointers to C, objects to Java or functions to Lisp or Haskell.


FishMonger
Veteran / Moderator

Apr 14, 2014, 2:07 PM

Post #10 of 10 (5543 views)
Re: [pgovind] Help with string [In reply to] Can't Post


Code
use strict; 
use warnings;

my @strings = (
'1:11:24:9:5057:0x00D13B0D:0x00D13B45',
'1:15:5:10:3582:0x00D13B24:0x00D13B09',
':15:5:10:3582:0x00D13B24:0x00D13B09',
'1:11:24:9:5057:0x00D13B0D:0x00D13B45',
'1:1:25:10:3582:0x00D13B24:0x00D13B09',
);

foreach my $string (@strings) {
print $string =~ /^(\d{1,2}:\d{1,2}:\d{1,2}):/
? "Matched: $1 in $string\n"
: "No Match in string $string\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives