LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2010, 10:36 AM   #16
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453

Quote:
Originally Posted by suse_nerd View Post
Yeah, but I was hoping to be able to take input directly from the barcode scanner, not a file.
What is "directly" ?

And, anyway, you need to debug the script, so repeatedly entering data manually doesn't help.
 
Old 04-21-2010, 12:35 PM   #17
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
What is "directly" ?

And, anyway, you need to debug the script, so repeatedly entering data manually doesn't help.
What do you mean by debug the script?

I mean I press the scan button on the barcode scanner and the input comes to STDIN. It works, but only parses the first line of the file:

Code:
$ ./out2.pl < 19_1.txt
Scan barcode and press enter
Got carriage return
Parsing the following input:
 $CENTAUR30298309                            000130287018
     000130318905                            000130295355
     000130295344                            000130295333
     000130209138                            000130210705
     000130217293                            000130273352
     000130292823                            000130292834
     000130293065                            000130293076
     000130293087                            000130293000
     000130293010                            000130293021
     000130292415                            000130292426
     000130292947                            000130292958
     0001$
Wrote a line to data.txt: 30298309,                    ,        ,0001
My script:
Code:
#!/usr/bin/perl
use strict;
use warnings;

print "Scan barcode and press enter\n";
while(defined(my $line = <STDIN>))
{
print "Got carriage return\n";

print "Parsing the following input:\n $line";
#Prompt for $myfile
#While not_end_of_line loop
if($line =~ m/^.{8}(.{8})(.{20})(.{8})(.{4})/)

  {
  open (MYFILE, '>>data.txt');
  print MYFILE "$1,$2,$3,$4\n";
  
  print "\nWrote a line to data.txt: ";
  print "$1,$2,$3,$4\n";
  }
#End not_end_of_line loop
  }

  close (MYFILE);
Obviously I need another loop in there which keeps repeating the operating until we get to the end of the line.
 
Old 04-21-2010, 12:51 PM   #18
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by suse_nerd View Post
What do you mean by debug the script?

I mean I press the scan button on the barcode scanner and the input comes to STDIN. It works, but only parses the first line of the file:

Code:
$ ./out2.pl < 19_1.txt
Scan barcode and press enter
Got carriage return
Parsing the following input:
 $CENTAUR30298309                            000130287018
     000130318905                            000130295355
     000130295344                            000130295333
     000130209138                            000130210705
     000130217293                            000130273352
     000130292823                            000130292834
     000130293065                            000130293076
     000130293087                            000130293000
     000130293010                            000130293021
     000130292415                            000130292426
     000130292947                            000130292958
     0001$
Wrote a line to data.txt: 30298309,                    ,        ,0001
My script:
Code:
#!/usr/bin/perl
use strict;
use warnings;

print "Scan barcode and press enter\n";
while(defined(my $line = <STDIN>))
{
print "Got carriage return\n";

print "Parsing the following input:\n $line";
#Prompt for $myfile
#While not_end_of_line loop
if($line =~ m/^.{8}(.{8})(.{20})(.{8})(.{4})/)

  {
  open (MYFILE, '>>data.txt');
  print MYFILE "$1,$2,$3,$4\n";
  
  print "\nWrote a line to data.txt: ";
  print "$1,$2,$3,$4\n";
  }
#End not_end_of_line loop
  }

  close (MYFILE);
Obviously I need another loop in there which keeps repeating the operating until we get to the end of the line.
Is the input data you are showing a single line or a multi-line file ?
 
Old 04-21-2010, 01:16 PM   #19
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
Is the input data you are showing a single line or a multi-line file ?
The data is on a single line and the header is also - I have updated the original post to make it clearer.
 
Old 04-21-2010, 01:26 PM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by suse_nerd View Post
The data is on a single line and the header is also - I have updated the original post to make it clearer.
So, is the 'if' statement executed ?

What happens if you put

Code:
warn "CHECKPOINT in 'if'";
as the first statement of the 'if' body ?
 
Old 04-21-2010, 01:36 PM   #21
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
So, is the 'if' statement executed ?

What happens if you put

Code:
warn "CHECKPOINT in 'if'";
as the first statement of the 'if' body ?
I believe it is- it outputs one "line" of data successfully in the CVS format but does not continue any further. It does not parse into the next part of the line.

Code:
$ ./out2.pl < 19_1.txt
Scan barcode and press enter
Got carriage return
Parsing the following input:
CHECKPOINT in 'if' at ./out2.pl line 16, <STDIN> line 1.
 $CENTAUR30298309                            000130287018
     000130318905                            000130295355
     000130295344                            000130295333
     000130209138                            000130210705
     000130217293                            000130273352
     000130292823                            000130292834
     000130293065                            000130293076
     000130293087                            000130293000
     000130293010                            000130293021
     000130292415                            000130292426
     000130292947                            000130292958
     0001$
Wrote a line to data.txt: 30298309,                    ,        ,0001
 
Old 04-21-2010, 01:59 PM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by suse_nerd View Post
I believe it is- it outputs one "line" of data successfully in the CVS format but does not continue any further. It does not parse into the next part of the line.

Code:
$ ./out2.pl < 19_1.txt
Scan barcode and press enter
Got carriage return
Parsing the following input:
CHECKPOINT in 'if' at ./out2.pl line 16, <STDIN> line 1.
 $CENTAUR30298309                            000130287018
     000130318905                            000130295355
     000130295344                            000130295333
     000130209138                            000130210705
     000130217293                            000130273352
     000130292823                            000130292834
     000130293065                            000130293076
     000130293087                            000130293000
     000130293010                            000130293021
     000130292415                            000130292426
     000130292947                            000130292958
     0001$
Wrote a line to data.txt: 30298309,                    ,        ,0001

So, why not to increase number on members like this:

Code:
(.{8})(.{20})(.{8})(.{4})
in the 'if' statement and correspondingly why not to print the newly added $N variables into the output file ?
 
1 members found this post helpful.
Old 04-21-2010, 03:06 PM   #23
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
So, why not to increase number on members like this:

Code:
(.{8})(.{20})(.{8})(.{4})
in the 'if' statement and correspondingly why not to print the newly added $N variables into the output file ?

Sorry I'm not really sure what you mean by this.

Can you give an example?

The spec is like this:
#<code_length_8><batch_length_20><expiry_length_8><quantity_length_4>

So do you mean, I should split at the 56th, 76th, 84th character etc, incrementing by a pattern of 8,20,8 then 4 for each iteration?

Is there not a cleaner way of doing this?
 
Old 04-21-2010, 03:12 PM   #24
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by suse_nerd View Post
Sorry I'm not really sure what you mean by this.

Can you give an example?

The spec is like this:
#<code_length_8><batch_length_20><expiry_length_8><quantity_length_4>

So do you mean, I should split at the 56th, 76th, 84th character etc, incrementing by a pattern of 8,20,8 then 4 for each iteration?

Is there not a cleaner way of doing this?
Who else but you knows character positions ? Is there info in the input stream allowing to recognize fields other than by their positions ?
 
1 members found this post helpful.
Old 04-21-2010, 03:15 PM   #25
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
By the way, the thread name doesn't appear to be relevant. I.e. I think you are dealing with a particular file format parsing issue, not specifically with PDF417.
 
1 members found this post helpful.
Old 04-21-2010, 03:29 PM   #26
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
By the way, the thread name doesn't appear to be relevant. I.e. I think you are dealing with a particular file format parsing issue, not specifically with PDF417.
Well it is a PDF417 barcode which needs to be parsed, that is the data you could get from a barcode with corresponds to the PDF417 standard.

No there is nothing else other than the positions in the data. There is nothing else to split the individual fields apart other than the length
So the header is
$CENTAUR = 8 characters
First item of data starts at 9th Character
First record ends at 8+8+20+8+4 = 48th character

Second field would look like this with regard to character numbers:
# 49 50 51 52 53 54 55 56
# 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
# 77 78 79 80 81 82 83 84
# 85 86 87 88
So split at the 56, 76 ,84 and 88


#Input spec PDF417 label
#Header: $CENTAUR
#Record format:
#<code_length_8><batch_length_20><expiry_length_8><quantity_length_4>
#Footer: $
#Fields contain white space if empty
#Max 22 records
#No spaces between fields if values present
#Example complete barcode input
Code:
 #$CENTAUR30292426                            0001$
 #$CENTAUR48347498                            0001483934739                            0001$
 
Old 04-21-2010, 03:35 PM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by suse_nerd View Post
...
No there is nothing else other than the positions in the data.
...
Then why do you ask ? I.e. the only (lame) optimization available is not to write manually '.', '{', '(', ')', '}'. Yes, this can be trivially done, i.e. the regular expression can be taken from a variable, and the variable contents can be generated in a loop - each position number will be enclosed into '.', '{', '(, ')', '}'.

But you will have to manually fill array of position numbers.
 
Old 04-21-2010, 03:43 PM   #28
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Something like this is working, but is messy as hell
Code:
#!/usr/bin/perl
use strict;
use warnings;

print "Scan barcode and press enter\n";
while(defined(my $line = <STDIN>))
{
print "Got carriage return\n";

print "Parsing the following input:\n $line";
#Prompt for $myfile


if($line =~ m/^.{8}(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})/)

  {
  warn "CHECKPOINT in 'if'";
  open (MYFILE, '>>data.txt');
  print MYFILE "$1,$2,$3,$4\n";
  print MYFILE "$5,$6,$7,$8\n";
  print MYFILE "$9,$10,$11,$12\n";
print MYFILE "$13,$14,$15,$16\n";
print MYFILE "$17,$18,$19,$20\n";
print MYFILE "$21,$22,$23,$24\n";
print MYFILE "$25,$26,$27,$28\n";
print MYFILE "$29,$30,$31,$32\n";
print MYFILE "$33,$34,$35,$36\n";
print MYFILE "$37,$38,$39,$40\n";
print MYFILE "$41,$42,$43,$44\n";
print MYFILE "$45,$46,$47,$48\n";
print MYFILE "$49,$50,$51,$52\n";
print MYFILE "$53,$54,$55,$56\n";
print MYFILE "$57,$58,$59,$60\n";
print MYFILE "$61,$62,$63,$64\n";
print MYFILE "$65,$66,$67,$68\n";
print MYFILE "$69,$70,$71,$72\n";
print MYFILE "$73,$74,$75,$76\n";
print MYFILE "$77,$78,$79,$80\n";

  
  
  }
  

  }

  close (MYFILE);
But this doesnt look for the end of the file "$" so it could throw up errors for data with fewer records than the spec max of 22

Last edited by suse_nerd; 04-21-2010 at 03:45 PM.
 
Old 04-21-2010, 03:49 PM   #29
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by suse_nerd View Post
Something like this is working, but is messy as hell
Code:
#!/usr/bin/perl
use strict;
use warnings;

print "Scan barcode and press enter\n";
while(defined(my $line = <STDIN>))
{
print "Got carriage return\n";

print "Parsing the following input:\n $line";
#Prompt for $myfile


if($line =~ m/^.{8}(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})(.{8})(.{20})(.{8})(.{4})/)

  {
  warn "CHECKPOINT in 'if'";
  open (MYFILE, '>>data.txt');
  print MYFILE "$1,$2,$3,$4\n";
  print MYFILE "$5,$6,$7,$8\n";
  print MYFILE "$9,$10,$11,$12\n";
print MYFILE "$13,$14,$15,$16\n";
print MYFILE "$17,$18,$19,$20\n";
print MYFILE "$21,$22,$23,$24\n";
print MYFILE "$25,$26,$27,$28\n";
print MYFILE "$29,$30,$31,$32\n";
print MYFILE "$33,$34,$35,$36\n";
print MYFILE "$37,$38,$39,$40\n";
print MYFILE "$41,$42,$43,$44\n";
print MYFILE "$45,$46,$47,$48\n";
print MYFILE "$49,$50,$51,$52\n";
print MYFILE "$53,$54,$55,$56\n";
print MYFILE "$57,$58,$59,$60\n";
print MYFILE "$61,$62,$63,$64\n";
print MYFILE "$65,$66,$67,$68\n";
print MYFILE "$69,$70,$71,$72\n";
print MYFILE "$73,$74,$75,$76\n";
print MYFILE "$77,$78,$79,$80\n";

  
  
  }
  

  }

  close (MYFILE);
But this doesnt look for the end of the file "$" so it could throw up errors for data with fewer records than the spec max of 22
Rather than $N you may have normal names - start from

perldoc perlretut

and look there for

named capture
.

And extend your regular expression to match '$' at the end - it's trivial, and document I'm suggesting to read contains the needed info.
 
Old 04-21-2010, 04:48 PM   #30
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
Rather than $N you may have normal names - start from

perldoc perlretut

and look there for

named capture
.

And extend your regular expression to match '$' at the end - it's trivial, and document I'm suggesting to read contains the needed info.
Care to give me a little more guidance? I am not that proficient at regex or perl.
I got that I need to do something like this
Code:
$header='~m/^.{8}(.{8})(.{20})(.{8})(.{4})';
$body='(.{8})(.{20})(.{8})(.{4})';
$foot='{2}';
But how to put this into a loop I am not sure. Also still don't really know what you mean by $N. Do you mean the nth ($N) character I split?

How do you detect the end footer? Look ahead by one character and see if it is a $?

Last edited by suse_nerd; 04-21-2010 at 05:03 PM.
 
  


Reply

Tags
awk, barcode, barcodes, bash, decode, perl, process, processing, script, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Looking for experience to make a script more efficient. aSingularity Programming 7 04-03-2010 01:41 AM
Next step to make this script more efficient...? redlinuxxx Programming 2 10-01-2009 09:42 AM
parsing script sthompson Linux - Server 14 06-10-2008 09:39 AM
LXer: Memory-efficient XML parsing in PHP with XMLReader LXer Syndicated Linux News 0 02-04-2007 05:33 PM
Help with Text Parsing Script NiallC Programming 6 06-24-2006 03:28 PM


All times are GMT -5. The time now is 01:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration