Home > Archive > PERL Miscellaneous > March 2004 > problem with index not matching string exactly
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 |
problem with index not matching string exactly
|
|
|
| I'm looping through a sales_file looking for matches to $code. Note
the file contains no spaces after the "|". Duplicates are allowed on
different lines, but not the same line. The file has a number of
entries such as the following (sample $sales_file):
sales item aaa|543,m423a
sales item bbb|m423,543 #Note how code 543 is on the 1st 2ndline.
sales item ccc|m423b
sales item ddd|423,423b,m523,652
- Also note we have entires of m423, m423a and m423b. The problem with
my code, is that it matches the $entered_code of m423 to m423, m423a,
and m423b but I want it to only match up to m423. Of course 423 should
only match up to 423 and so on. Anyone have a suggestion to fix this?
Here is the code segment:
open FILE, "<$sales_file" or die "could not open '$sales_file' $!";
while (<FILE> ) {
chomp;
my ($sales_item, $code) = split /\|/;
my @code = split /,/, $exits;
if (index($code, $entered_code) != -1) {
$list .="<ul>" if !($list);
$list .= "<li><p>$sales_item</li>";
}
}
Thanks,
| |
| Anno Siegel 2004-03-27, 12:01 am |
| G <bay_dar@yahoo.com> wrote in comp.lang.perl.misc:
> I'm looping through a sales_file looking for matches to $code. Note
> the file contains no spaces after the "|". Duplicates are allowed on
> different lines, but not the same line. The file has a number of
> entries such as the following (sample $sales_file):
>
> sales item aaa|543,m423a
> sales item bbb|m423,543 #Note how code 543 is on the 1st 2ndline.
> sales item ccc|m423b
> sales item ddd|423,423b,m523,652
>
> - Also note we have entires of m423, m423a and m423b. The problem with
So, are these equivalent or not? You're not saying.
> my code, is that it matches the $entered_code of m423 to m423, m423a,
> and m423b but I want it to only match up to m423. Of course 423 should
> only match up to 423 and so on. Anyone have a suggestion to fix this?
>
> Here is the code segment:
>
> open FILE, "<$sales_file" or die "could not open '$sales_file' $!";
> while (<FILE> ) {
> chomp;
> my ($sales_item, $code) = split /\|/;
> my @code = split /,/, $exits;
>
> if (index($code, $entered_code) != -1) {
> $list .="<ul>" if !($list);
> $list .= "<li><p>$sales_item</li>";
> }
>
> }
Your code and your verbal description don't agree. Don't you want
to split $code in the second split? If not, what's in $exit?
In any case, what's in $entered_code?
Your description leaves open too many possibilities. What, exactly, are
the "codes" you speak of, and what are the strings you want to compare
them too?
Anno
| |
| Uri Guttman 2004-03-27, 12:01 am |
| >>>>> "G" == G <bay_dar@yahoo.com> writes:
G> - Also note we have entires of m423, m423a and m423b. The problem with
G> my code, is that it matches the $entered_code of m423 to m423, m423a,
G> and m423b but I want it to only match up to m423. Of course 423 should
G> only match up to 423 and so on. Anyone have a suggestion to fix this?
G> my @code = split /,/, $exits;
G> if (index($code, $entered_code) != -1) {
why are you using index there? index only tells where the string is if
found. a simple eq is all you need there.
uri
| |
|
| anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<c3vcn5$a0d$2@mamenchi.zrz.TU-Berlin.DE>...
> G <bay_dar@yahoo.com> wrote in comp.lang.perl.misc:
>
> So, are these equivalent or not? You're not saying.
They are not equivilent.
>
[color=darkred]
>
> Your code and your verbal description don't agree. Don't you want
> to split $code in the second split? If not, what's in $exit?
>
> In any case, what's in $entered_code?
$entered_code would be an entry such as m423 or 423.
The program should work as follows:
I want to add the text($sales_item) to the left of "|" in the sales
file to my $list if I find a match.
For instance an $entered_code of 423 would add "sales item ddd" to the
list. An $entered_code of m423b would add "sales item ddd" and "sales
item ddd" to the list.
>
> Your description leaves open too many possibilities. What, exactly, are
> the "codes" you speak of, and what are the strings you want to compare
> them too?
my ($sales_item, $code) is the array that reads in the $sales file and
associated codes.
e.g.
sales item aaa,543
sales item aaa,m423a
sales item bbb,m423
sales item bbb,543
....
I hope I made myself a little clearer.
Thanks
>
> Anno
| |
| Anno Siegel 2004-03-27, 12:01 am |
| G <bay_dar@yahoo.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message
> news:<c3vcn5$a0d$2@mamenchi.zrz.TU-Berlin.DE>...
>
> They are not equivilent.
>
>
>
> $entered_code would be an entry such as m423 or 423.
>
> The program should work as follows:
>
> I want to add the text($sales_item) to the left of "|" in the sales
> file to my $list if I find a match.
>
> For instance an $entered_code of 423 would add "sales item ddd" to the
> list. An $entered_code of m423b would add "sales item ddd" and "sales
> item ddd" to the list.
>
>
> my ($sales_item, $code) is the array that reads in the $sales file and
> associated codes.
>
> e.g.
>
> sales item aaa,543
> sales item aaa,m423a
> sales item bbb,m423
> sales item bbb,543
> ...
>
> I hope I made myself a little clearer.
I think so.
You don't need to go through the input file each time. Read the file
once and build a hash that maps each code to the list of sales items
associated with it:
my %table;
while ( <FILE> ) {
chomp;
my ( $sales_item, $codes) = split /\|/;
push @{ $table{ $_}}, $sales_item for split /,/, $codes;
}
Now %table lets you retrieve the list of sales items associated with
any code in a single access (no searching).
So, to compile the list of sales items, do this:
my $list = '<ul>';
$list .= "<li><p>$_</li>" for @{ $table{ $entered_code}};
I guess you must close the "<ul>" tag now, but me no speakee HTML.
Building the %table for each retrieval may look like overkill, because
you will only be using one entry each time. But it helps the overall
program structure by separating the input step from processing, and
that's always a gain.
Anno
|
|
|
|
|