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: Beginner:
string compare?

 



rekcut01
Novice

May 15, 2014, 9:16 AM

Post #1 of 6 (3040 views)
string compare? Can't Post

Friends,



I'm having a string compare issue, can someone guide me as to what I am doing wrong?

while (1)
{
my $input = <STDIN>; #<------ getting from a hand scanner.
#chomp ($input);

my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('C:\Users\Rtucker\Desktop\TstReport.xls');

if ( !defined $workbook )
{
die $parser->error(), ".\n";
}

for my $worksheet ( $workbook->worksheets() )
{

my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();

for my $row ( $row_min .. $row_max )
{
for my $col ( $col_min .. $col_max )
{

my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;

my $cmp_string = $cell->unformatted();
print "Row, Col = ($row, $col)\n";
print "Value = ", $cell->value(), "\n";
print "Unformatted = ", $cell->unformatted(), "\n";
print "cmp_string = $cmp_string \n";
print "\n";

#my $cmp_string = $cell->value();
#chomp $cmp_string;
if ($cmp_string eq $input) #<--------- when I print out both variables they appear to be the same, yet it never hits my if statement
{
print "Same";
# last;
}
#else
#{
# print "$cmp_string $input \n";
# #print "different";
#}
#print $cmp_string;
}
}
}
}



I know it's some little thing I'm doing wrong but for the life of me I can't see it....



As always, Thanks for the help

Rick


FishMonger
Veteran / Moderator

May 15, 2014, 9:19 AM

Post #2 of 6 (3038 views)
Re: [rekcut01] string compare? [In reply to] Can't Post

Use Data::Dumper to inspect the values of those vars.

I suspect that $input has a trailing \n char.


rekcut01
Novice

May 15, 2014, 9:54 AM

Post #3 of 6 (3022 views)
Re: [FishMonger] string compare? [In reply to] Can't Post

Thank you once again my friend, you was correct in your thinking!!!

I ended up using:

$input =~ s/\r\n//; and it worked, I now hit my if statement!!



I can't thank you enough!

-Rick


Laurent_R
Veteran / Moderator

May 15, 2014, 10:03 AM

Post #4 of 6 (3015 views)
Re: [rekcut01] string compare? [In reply to] Can't Post

There is a built-in function for removing end-of-lines characters: chomp. It would tend to be more portable across different platforms and slightly fastest than your regex, if that matters.


rekcut01
Novice

May 15, 2014, 11:11 AM

Post #5 of 6 (2989 views)
Re: [Laurent_R] string compare? [In reply to] Can't Post

Thank you Laurent_R, I tried to get chomp to work and it didn't seem to...

I have another issue now, man this has been a learning experience I must tell you! But I do appreciate the help.

I'm trying to add a cell to indicate when a visitor has checked out via the barcode number, it finds the cell containing the number, I do a string compare from the input of the scanner, but when I try to add a cell via $worksheet->AddCell( blah blah blah); I get the error "cant locate object method "AddCell" via package "Spreadsheet::ParseExcel::Worksheet", but I know its there I see it!!! Any suggestions?



Now here is my code as it stands now:



#!c:/strawberry/perl/bin/perl.exe
# PERL MODULES WE WILL BE USING
use strict;
use warnings;
use integer;
use CGI;
use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Variant;
use Win32::OLE::Const 'Microsoft Excel';
use Spreadsheet::ParseExcel::Worksheet;
use Spreadsheet::ParseExcel::SaveParser;
use Spreadsheet::WriteExcel;
use Time::localtime;
use Data::Dumper;


print "Content-type: text/html\n\n";

while (1)
{
my $input = <STDIN>;
$input =~ s/\r\n//;
#chomp ($input);
#my $input1 = chomp ($input);

my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook = $parser->parse('/Users/Rtucker/Desktop/TstReport.xls');
if ( !defined $workbook )
{
die $parser->error(), ".\n";
}



my $worksheet = $workbook->worksheet(0);
for my $worksheet ( $workbook->worksheets() )
{

my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();

for my $row ( $row_min .. $row_max )
{
for my $col ( $col_min .. $col_max )
{

my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;

my $cmp_string = $cell->value();

if ($cmp_string eq $input)
{
my $out_time = localtime;

$worksheet->AddCell( $row, $col + 1, $out_time ); # <---------------------Fails right here...
# Write over the existing file or write a new file.
$workbook->SaveAs('/Users/Rtucker/Desktop/TstReport.xls');
#last;
}
}
}
}
}



I have to admit I wish I could have used KSH and AWK, I would have been finished by now :-)

Thanks for any help

Rick


rekcut01
Novice

May 15, 2014, 1:18 PM

Post #6 of 6 (2947 views)
Re: [rekcut01] string compare? [In reply to] Can't Post

Friends,

I figured it out, I just needed to break out of the worksheet loop and call a sub routine passing the row and column number... and it worked, thanks for everyones help!!!

Rick

 
 


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

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