For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > Problem compare string with array









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 compare string with array
John Doe

2004-05-14, 5:30 am

Hello all,
i have problem, and i don't know where is my mistake.

---
#!/usr/bin/perl

@myarra = ("test","error","block");

$file = 'sorted'; # File contain records xxx.xxx.xxx
# i need last symbols after last -> .
open(FILE, $file);
while ($data = <FILE> ) {
$backupdata = $data;
$data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2 or 3 symbols
after last -> .
$data = lc($data); # make $data to lowercast
for ($i = 0; $i < 3; ++$i)
{
if ($data eq $myarra[$i]) # no any outputs :(
# if ($data =~ /$myarra[$i]/) # this not work
# $data contain -> est
# $myarra[$i] -> test
# and script say equal but not.
{
print "------\n";
print "$data\n";
print "$i\n";
print "$myarra[ $i ]\n";
print "$backupdata\n"; # print $backupdata if
# $data equal to @myarra[$i], in this case test = test, error = error
# etc.
print "------\n";
}
}
}

close (FILE);
-----

Any body can help me ? i read some documentation but unable
to fix problem.

Regards,
John
Durai

2004-05-14, 6:30 am

Corrceted One:

@myarra = ("test","error","block");

$file = 'sorted'; # File contain records xxx.xxx.xxx
# i need last symbols after last -> .
open(FILE, $file);
while ($data = <FILE> ) {
$backupdata = $data;
$data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2 or 3 symbols
#chomp($data);

$data = lc($data); # make $data to lowercast
for ($i = 0; $i < 3; ++$i)
{
if ($data =~ /$myarra[$i]/){ # this not work
# $data contain -> est
# $myarra[$i] -> test
# and script say equal but not.
print "------\n";
print "$data\n";
print "$i\n";
print "$myarra[ $i ]\n";
print "$backupdata\n"; # print $backupdata if
# $data equal to @myarra[$i], in this case test = test, error = error
# etc.
print "------\n";
}
}
}

close (FILE);


----- Original Message -----
From: "John Doe" <john@vcable.net>
To: <beginners@perl.org>
Sent: Friday, May 14, 2004 2:13 AM
Subject: Problem compare string with array


> Hello all,
> i have problem, and i don't know where is my mistake.
>
> ---
> #!/usr/bin/perl
>
> @myarra = ("test","error","block");
>
> $file = 'sorted'; # File contain records xxx.xxx.xxx
> # i need last symbols after last -> .
> open(FILE, $file);
> while ($data = <FILE> ) {
> $backupdata = $data;
> $data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2 or 3 symbols
> after last -> .
> $data = lc($data); # make $data to lowercast
> for ($i = 0; $i < 3; ++$i)
> {



> if ($data eq $myarra[$i]) # no any outputs :(


if ($data eq $myarra[$i]) { # no any outputs :(

> # if ($data =~ /$myarra[$i]/) # this not work
> # $data contain -> est
> # $myarra[$i] -> test
> # and script say equal but not.


remove the following brace.

> {


> print "------\n";
> print "$data\n";
> print "$i\n";
> print "$myarra[ $i ]\n";
> print "$backupdata\n"; # print $backupdata if
> # $data equal to @myarra[$i], in this case test = test, error = error
> # etc.
> print "------\n";
> }
> }
> }
>
> close (FILE);
> -----
>
> Any body can help me ? i read some documentation but unable
> to fix problem.
>
> Regards,
> John
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 4/23/2004
Rob Dixon

2004-05-14, 7:30 am

Hi John.

See my reply in line.

John Doe wrote:
>
> Hello all,
> i have problem, and i don't know where is my mistake.
>
> ---
> #!/usr/bin/perl


use strict; # always
use warnings; # usually

which means that all your variables need to be declared with 'my'.

> @myarra = ("test","error","block");


You may prefer

my @myarra = qw( test error block );

which is a lot cleaner

> $file = 'sorted'; # File contain records xxx.xxx.xxx
> # i need last symbols after last -> .
> open(FILE, $file);


Always check the success of an open:

open(FILE, $file) or die $!;

> while ($data = <FILE> ) {
> $backupdata = $data;


This is better written

while (<FILE> ) {
$backupdata = $_;

> $data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2 or 3 symbols
> after last -> .
> $data = lc($data); # make $data to lowercast


Here is your problem. $data will still have the trailing newline
from the input file. A simple chomp() would help, but I think
your regex may also be wrong. Your comment says "i need last symbols
after last -> ." but your regex looks only up to the first whitespace
in the record. A $data value of "aaa.bbb ccc.ddd\n" will be changed
to "bbb ccc.ddd\n", and anything with leading spaces won't be changed
at all. Also you don't need the brackets which are capturing part of
the string you're deleting.

Solve both of these at once with:

my ($data) = /.*\.(\S+)/

which will capture the last sequence of non-space characters (so
eliminating the newline) after the last dot.

> for ($i = 0; $i < 3; ++$i)
> {


for my $i (0 .. $#myarra) {

> if ($data eq $myarra[$i]) # no any outputs :(
> # if ($data =~ /$myarra[$i]/) # this not work
> # $data contain -> est
> # $myarra[$i] -> test
> # and script say equal but not.
> {
> print "------\n";
> print "$data\n";
> print "$i\n";
> print "$myarra[ $i ]\n";
> print "$backupdata\n"; # print $backupdata if
> # $data equal to @myarra[$i], in this case test = test, error = error
> # etc.
> print "------\n";
> }
> }
> }
>
> close (FILE);


The rest of your program should work. But I should also add that some
indenting and whitespace would help the readability of your work by an
enormous amount. I've added a composite of all the changes below.

HTH,

Rob


#!/usr/bin/perl

use strict;
use warnings;

my @myarra = qw/ test error block /;

my $file = 'sorted';

open(FILE, $file) or die $!;

while (<FILE> ) {

my $backupdata = $_;
my ($data) = /.*\.(\S+)/;
$data = lc($data);

for my $i (0 .. $#myarra) {

if ($data eq $myarra[$i]) {

print "------\n";
print "$data\n";
print "$i\n";
print "$myarra[ $i ]\n";
print "$backupdata\n";
print "------\n";
}
}
}

close (FILE);


Charles K. Clarkson

2004-05-14, 8:30 am

John Doe <john@vcable.net> Wrote:
:
: Hello all,
: i have problem, and i don't know where is my mistake.

Your first mistake is not describing the problem in
your message. A good programmer realizes that help comes
best if the problem is stated clearly.

Your second mistake was not to include at least a
partial listing of the source file ('sorted'). I had to
make up a file to find out if your problem might have
been in the regex. This slowed me down as I had to
wonder if my guess about the file structure was correct.

Your short answer, assuming I have guessed at the
correct problem, is that you are not removing the line
ending from $data before comparing it to each element
of @myarra.

: while ($data = <FILE> ) {
chomp $data;

: $backupdata = $data;
: $data =~ s/^(\S+)\.//g;


Long answer:


: ---
: #!/usr/bin/perl

strict and warnings enforce good programming practices.

use strict;
use warnings;


: @myarra = ("test","error","block");
: $file = 'sorted'; # File contain records xxx.xxx.xxx

Declare variables lexically using 'my'. 'myarra' is
not very descriptive. Don't get bagged down on naming
things, but avoid names like 'array' and 'hash'. They
give no clue to others what the data represents.

my @fields = qw/test error block/;
my $file = 'sorted';


: # i need last symbols after last -> .
: open(FILE, $file);

How do you know it opened?
Always test that a file opens properly.

open FILE, $file or die qq|Cannot open file "$file": $!|;


: while ($data = <FILE> ) {

chomp $data;


: $backupdata = $data;
: $data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2
: # or 3 symbols after last -> .
: $data = lc($data); # make $data to lowercast


Split would work without destroying the original:
Also, $data is not very descriptive. Every input in
computer programming is considered data. $line is a
more common name for the lines in a file.

I assumed the text you were retrieving was a field.
Since $line is not destroyed in the code blow, it can
be used in the report.


while ( my $line = <FILE> ) {
chomp $line;
my $field = lc( ( split /\./, $line )[-1] );


: for ($i = 0; $i < 3; ++$i)

I prefer:

foreach my $i ( 0 .. 2 )


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

John Doe

2004-05-14, 3:30 pm

Charles K. Clarkson wrote:
> John Doe <john@vcable.net> Wrote:
> :
> : Hello all,
> : i have problem, and i don't know where is my mistake.
>
> Your first mistake is not describing the problem in
> your message. A good programmer realizes that help comes
> best if the problem is stated clearly.
>
> Your second mistake was not to include at least a
> partial listing of the source file ('sorted'). I had to
> make up a file to find out if your problem might have
> been in the regex. This slowed me down as I had to
> wonder if my guess about the file structure was correct.
>
> Your short answer, assuming I have guessed at the
> correct problem, is that you are not removing the line
> ending from $data before comparing it to each element
> of @myarra.
>
> : while ($data = <FILE> ) {
> chomp $data;
>
> : $backupdata = $data;
> : $data =~ s/^(\S+)\.//g;
>
>
> Long answer:
>
>
> : ---
> : #!/usr/bin/perl
>
> strict and warnings enforce good programming practices.
>
> use strict;
> use warnings;
>
>
> : @myarra = ("test","error","block");
> : $file = 'sorted'; # File contain records xxx.xxx.xxx
>
> Declare variables lexically using 'my'. 'myarra' is
> not very descriptive. Don't get bagged down on naming
> things, but avoid names like 'array' and 'hash'. They
> give no clue to others what the data represents.
>
> my @fields = qw/test error block/;
> my $file = 'sorted';
>
>
> : # i need last symbols after last -> .
> : open(FILE, $file);
>
> How do you know it opened?
> Always test that a file opens properly.
>
> open FILE, $file or die qq|Cannot open file "$file": $!|;
>
>
> : while ($data = <FILE> ) {
>
> chomp $data;
>
>
> : $backupdata = $data;
> : $data =~ s/^(\S+)\.//g; # Remove xxx.xxx. get last 2
> : # or 3 symbols after last -> .
> : $data = lc($data); # make $data to lowercast
>
>
> Split would work without destroying the original:
> Also, $data is not very descriptive. Every input in
> computer programming is considered data. $line is a
> more common name for the lines in a file.
>
> I assumed the text you were retrieving was a field.
> Since $line is not destroyed in the code blow, it can
> be used in the report.
>
>
> while ( my $line = <FILE> ) {
> chomp $line;
> my $field = lc( ( split /\./, $line )[-1] );
>
>
> : for ($i = 0; $i < 3; ++$i)
>
> I prefer:
>
> foreach my $i ( 0 .. 2 )
>
>
> HTH,
>
> Charles K. Clarkson


Thanks for all people that answer me via nntp and email.
The promlem is resolved and has been that i not use
chomp($data);

Thanks for help

Regards,
John
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com