Re: Parsing a string for sorting
by btrott (Parson) on Aug 08, 2000 at 01:19 UTC
|
Would this work for you? The validation check would
be in a subroutine "is_good".
my $value = "001003051";
my $three;
my $start = 0;
while ($three = substr $value, $start, 3) {
## Do validation check on $three
break unless is_good($three);
$start += 3;
}
| [reply] [d/l] |
Re: Parsing a string for sorting
by BlueLines (Hermit) on Aug 08, 2000 at 01:28 UTC
|
How about something like this:
my $string="001003051";
@values=($string=~m/(\G\d{3})+/g);
foreach $value (@values) {
&verify($value);
}
BlueLines
Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary. | [reply] [d/l] |
Re: Parsing a string for sorting
by extremely (Priest) on Aug 08, 2000 at 03:39 UTC
|
| [reply] |
RE: Parsing a string for sorting
by grasshopper (Initiate) on Aug 08, 2000 at 05:21 UTC
|
Hey Guys...
Thanks for all the help, I really need it. Hey Eak I'm sorry to disappoint you
but this is not a homework assignment, it's for my job. I am a VB programmer, but
since I have been trained in "C" and in "C++" I have been asked to do some maintenance.
Again I would like to Thank everyone for the help. | [reply] |
Re: Sorting
by BlueLines (Hermit) on Aug 08, 2000 at 01:33 UTC
|
my $string="001003051";
@values=($string=~m/(\G\d{3})+/g);
foreach $value (@values) {
&verify($value);
}
BlueLines
Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary. | [reply] [d/l] |
Re: Parsing a string for sorting
by eak (Monk) on Aug 08, 2000 at 04:12 UTC
|
How about this one liner.
my $string="001003051";
&verify($_) for(split /(\d{3})/, $string);
If you are worried about non-numbers creeping into your string, you would have to add a 'tr'.
--eric | [reply] [d/l] |
|
|
I've really fallen in disfavor with using the "delimiter capturing" mechanism
of split, when the list-context m//g is so much more direct
and controllable:
verify($_) for $string =~ /\G(\d{3})/g;
There. Now there's no chance that "012xxx345xxx678" will try to
verify the 345 and 678, as it would for the split example (not to mention
passing the xxx parts to verify.
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
Re: Parsing a string for sorting
by fundflow (Chaplain) on Aug 08, 2000 at 11:34 UTC
|
Just wondering,
Isn't there a nice way to use unpack?
It sounds like it should be faster. | [reply] |
Re: Sorting
by tilly (Archbishop) on Aug 08, 2000 at 01:23 UTC
|
If your string is in $expect_data, try the following:
while ($expect_data =~ /(\d{1,3})/g) {
my $next_chunk = $1;
# Validate here
}
The idea of this is quite simple. In Locate char in a string I gave an
explanation of the principle from which you can loop over
a match. All that is left is to explain the match. \d,
of course, is a digit. \d{1,3} is short-hand for saying
"1-3 digits". By default it matches as much as possible,
so that will grab 3 digits at a time. I assume that you
would want the final digit or two to be grabbed as well.
Putting parens around it causes the matched section to be
popped into a variable, $1.
If this sounds like a bit much, well I just threw a lot at
you. Try some examples though and it shouldn't take long
to get a sense how it works. What you are looking for is
anything on "regular expressions". Any introductory book
has material on this, and the standard reference that I
have to recommend is Mastering Regular Expressions by
Friedl.
As always, /tell me if you have problems figuring out how
to use this advice. | [reply] [d/l] |
Re: Sorting
by Adam (Vicar) on Aug 08, 2000 at 01:05 UTC
|
Are you looking for a way to get the first three digits off of a value? Something like this would work: my $numeric_string = '0010030511';
my $first_three_digits;
$first_three_digits = $1 if $numeric_string =~ s/^(\d{3})//;
You could also convert the string to an array:
my @numeric_string = split//, '0010030511';
my $first_three_digits = join '', splice @numeric_string, 0, 3;
Update: Foolish me. I forgot the power of substr oh well. TIMTOWTDI.
| [reply] [d/l] [select] |