I’m looping through a sales_file looking for matches. The file
has a number of entries such as the following:
sales item aaa | m423a
sales item bbb | m423
sales item ccc | m423b
sales item ddd | 423
These refer to sales_item and code respectively.
Here is the code segment:
open FILE, "<$sales_file";
while (<FILE>) {
($sales_item, $code) = split /\|/;
if (index($code, $entered_code) != -1) {
$list .= "<br>" if ($list);
$list .= $sales_item;
}
} # while
close FILE;
The problem is, if the $entered_code is 423 I get matches for all 4
when I would only want matches for the fourth sales item “sales
item ddd” line. Similarly, an $entered_code of m423 would match
the first 3. Any suggestions on how I can get the right matches,
keeping in mind that I would prefer to do it in code, and not alter
the sales_file.
Thanks,
C
|
|
0
|
|
|
|
Reply
|
bay_dar
|
2/9/2004 4:25:07 PM |
|
On Mon, 9 Feb 2004, G wrote:
> I’m looping through a sales_file looking for matches. The file
> has a number of entries such as the following:
>
> sales item aaa | m423a
> sales item bbb | m423
> sales item ccc | m423b
> sales item ddd | 423
>
> These refer to sales_item and code respectively.
>
> Here is the code segment:
>
> open FILE, "<$sales_file";
> while (<FILE>) {
> ($sales_item, $code) = split /\|/;
> if (index($code, $entered_code) != -1) {
> $list .= "<br>" if ($list);
> $list .= $sales_item;
> }
> } # while
> close FILE;
>
> The problem is, if the $entered_code is 423 I get matches for all 4
> when I would only want matches for the fourth sales item “sales
> item ddd” line. Similarly, an $entered_code of m423 would match
> the first 3. Any suggestions on how I can get the right matches,
> keeping in mind that I would prefer to do it in code, and not alter
> the sales_file.
Replace the index() line with
if ($code =~ /^\s*$entered_code\s*$/) {
This will search the $code line for 'beginning of string, possible white
space, the code, possible white space, end of string', rather than just
"the code anywhere within the string" as you're doing now.
Paul Lalli
|
|
0
|
|
|
|
Reply
|
Paul
|
2/9/2004 4:46:26 PM
|
|
"G" <bay_dar@yahoo.com> wrote in message
news:cad04083.0402090825.2eba95a5@posting.google.com...
[problem with index not matching string exactly]
> sales item aaa | m423a
> sales item bbb | m423
> sales item ccc | m423b
> sales item ddd | 423
if there is allways space around the '|' you should
have them in your split
> ...
> ($sales_item, $code) = split /\|/;
($sales_item, $code) = split / \| /;
> if (index($code, $entered_code) != -1) {
if there is no trailing space after the code then
if ($code eq $entered_code) {
if on the other hand, your data is dirty with
whilespace, you are better off with a
regexp match as someone else suggested
or even replace the split with a match:
($sales_item, $code) = /^\s*(.+?)\s*\|\s*(.+?)\s*/;
if ($code eq $entered_code) {
gnari
|
|
0
|
|
|
|
Reply
|
gnari
|
2/9/2004 7:55:11 PM
|
|
G <bay_dar@yahoo.com> wrote:
> open FILE, "<$sales_file";
You should always, yes *always*, check the return value from open():
open FILE, "<$sales_file" or die "could not open '$sales_file' $!";
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
0
|
|
|
|
Reply
|
Tad
|
2/9/2004 9:52:53 PM
|
|
"gnari" <gnari@simnet.is> wrote in message news:<c08oh6$3b9$1@news.simnet.is>...
> "G" <bay_dar@yahoo.com> wrote in message
> news:cad04083.0402090825.2eba95a5@posting.google.com...
>
> [problem with index not matching string exactly]
>
> > sales item aaa | m423a
> > sales item bbb | m423
> > sales item ccc | m423b
> > sales item ddd | 423
>
> if there is allways space around the '|' you should
> have them in your split
>
> > ...
> > ($sales_item, $code) = split /\|/;
> ($sales_item, $code) = split / \| /;
>
> > if (index($code, $entered_code) != -1) {
>
>
> if there is no trailing space after the code then
> if ($code eq $entered_code) {
>
> if on the other hand, your data is dirty with
> whilespace, you are better off with a
> regexp match as someone else suggested
> or even replace the split with a match:
>
> ($sales_item, $code) = /^\s*(.+?)\s*\|\s*(.+?)\s*/;
> if ($code eq $entered_code) {
>
> gnari
Thanks for the suggestions so far, but I now realize I the sample text
file was flawed. For one there is Never white space around the '|'.
Secondly a line could have multiple codes but no duplicates(on that
line only). The sample file should have looked as follows:
sales item aaa|543,m423a
sales item bbb|m423,543 'Note how code 543 is on the 1st 2nd
line.
sales item ccc|m423b
sales item ddd|423,423b,m523,652
Given that the above has changed how could I get a match. e.g. a code
of 423 should return the description in line 4 "sales item ddd" Where
m423 only matches the 3rd line. etc.
Thanks,
C
|
|
0
|
|
|
|
Reply
|
bay_dar
|
2/10/2004 2:22:57 PM
|
|
bay_dar@yahoo.com (G) wrote:
> Thanks for the suggestions so far, but I now realize I the sample text
> file was flawed. For one there is Never white space around the '|'.
> Secondly a line could have multiple codes but no duplicates(on that
> line only). The sample file should have looked as follows:
>
> sales item aaa|543,m423a
> sales item bbb|m423,543 'Note how code 543 is on the 1st 2nd
> line.
> sales item ccc|m423b
> sales item ddd|423,423b,m523,652
>
> Given that the above has changed how could I get a match. e.g. a code
> of 423 should return the description in line 4 "sales item ddd" Where
> m423 only matches the 3rd line. etc.
my $code = 'm423';
while (<>) {
my ($item, $codes) = split /\|/;
my @codes = split /,/, $codes;
print $item if grep { $_ eq $code } @codes;
}
alternatively:
/(.*) \| (?:.*,|) \Q$code\E (?:,|$)/x and print $1 while <>;
Ben
--
$.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
$x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
{$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t # ben@morrow.me.uk
$J::u::s::t, $a::n::o::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
|
|
0
|
|
|
|
Reply
|
Ben
|
2/10/2004 3:02:55 PM
|
|
Ben Morrow <usenet@morrow.me.uk> wrote in message news:<c0arqv$9u4$4@wisteria.csv.warwick.ac.uk>...
> bay_dar@yahoo.com (G) wrote:
> > Thanks for the suggestions so far, but I now realize I the sample text
> > file was flawed. For one there is Never white space around the '|'.
> > Secondly a line could have multiple codes but no duplicates(on that
> > line only). The sample file should have looked as follows:
> >
> > sales item aaa|543,m423a
> > sales item bbb|m423,543 'Note how code 543 is on the 1st 2nd
> > line.
> > sales item ccc|m423b
> > sales item ddd|423,423b,m523,652
> >
> > Given that the above has changed how could I get a match. e.g. a code
> > of 423 should return the description in line 4 "sales item ddd" Where
> > m423 only matches the 3rd line. etc.
>
> my $code = 'm423';
> while (<>) {
> my ($item, $codes) = split /\|/;
> my @codes = split /,/, $codes;
> print $item if grep { $_ eq $code } @codes;
> }
>
I finally gave this code a try, but it only partially works. For
instance a code of m423b will not pull up any results. Neither will
m523. My guess is we are not splitting things out right: my
@codes = split /,/, $codes;
Thanks,
C
|
|
0
|
|
|
|
Reply
|
bay_dar
|
2/12/2004 4:22:58 PM
|
|
bay_dar@yahoo.com (G) wrote:
> Ben Morrow <usenet@morrow.me.uk> wrote in message news:<c0arqv$9u4$4@wisteria.csv.warwick.ac.uk>...
> > my $code = 'm423';
> > while (<>) {
chomp;
> > my ($item, $codes) = split /\|/;
> > my @codes = split /,/, $codes;
> > print $item if grep { $_ eq $code } @codes;
> > }
>
> I finally gave this code a try, but it only partially works. For
> instance a code of m423b will not pull up any results. Neither will
> m523. My guess is we are not splitting things out right: my
> @codes = split /,/, $codes;
Ben
--
The cosmos, at best, is like a rubbish heap scattered at random.
- Heraclitus
ben@morrow.me.uk
|
|
0
|
|
|
|
Reply
|
Ben
|
2/12/2004 6:15:25 PM
|
|
|
7 Replies
51 Views
(page loaded in 0.089 seconds)
Similiar Articles: New features added to development gawk - comp.lang.awkGetting this to work in the presence of regexps that can match the null string was difficult, so ... r 230 350 m 0 1 179{ 1 index ... e. not much) with a few extra ... How best to detect duplicate values in a column? - comp.databases ...And the extra storage needed ... to write code to add an index to the one of the two JOINed string ... maybe a hundred rows matching, that I really want to have an index for ... input & output in assembly - comp.lang.asm.x86... 16 byte) dc.w 0 ; Minimum extra ... backspace)o, world" would clearly not give a "match ... level functions: > > INT 21h, AH = 09h: Print String ... Sampling: What Nyquist Didn't Say, and What to Do About It - comp ...> Getting /almost/ on-topic again, the issue is, I think ... are occasional mistakes in the spacing (such as an extra ... options in how loosely the features must match to be ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... Where did Fortran go? - comp.lang.fortran... hinted that performance when using PDTs will not match ... other languages today take for granted (easier string ... In Matlab, you can't even start the array at an index ... Fast bit-reverse on an x86? - comp.dspFor bit string with multiple of 32 bits and properly ... still need to triple in size to be slow enough to match ... Yes, the move byte's do need an extra instruction or two ... javascript - Getting the closest string match - Stack Overflow... to achieve the greatest number of matches. Fuzzy string matching is ... allows you to search against an index of strings with ... levenshtein to match target string + extra ... String (Java 2 Platform SE 5.0)Tells whether or not this string matches the ... be the smallest such index; then the string whose ... in which they occur in this string. If the expression does not match ... 7/22/2012 7:40:59 PM
|