Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Parse data from a line to get 2 variables

by jwkrahn (Abbot)
on May 13, 2006 at 03:53 UTC ( [id://549165]=note: print w/replies, xml ) Need Help??


in reply to Parse data from a line to get 2 variables

Based upon your specifications you probably want something like this:
use warnings; use strict; open LABELS, '<', 'labellist.txt' or die "I could not get at labellist +.txt as input: $!"; while ( <LABELS> ) { my ( $lotnumber, $product ) = /^(\w\d{7})\t(\w{6,9})$/ or next; print "^XA^LH30,30^FO110,65^A0,N,100^FD$lotnumber^FS^FO150,160^BY2 +,3.0^B3N,N,125,N,N^FD$lotnumber^FS^FO75,375^A0,N,50,30^FD$product^FS^ +FO30,425^BY2,2.0^B3N,N,50,N,N^FD$product^FS^XZ\n"; } close LABELS;

Replies are listed 'Best First'.
Re^2: Parse data from a line to get 2 variables
by zerogeek (Monk) on May 13, 2006 at 05:54 UTC
    My initial thought was to use 'split' as suggested in an earlier reply. However, reading your solution certainly seems to be a better approach.
    I hadn't thought of using the min/max match. Good call!
      I thought I would show you all the conclusion I came up
      with thanks to your help. I used the solution to
      getting my variables originally posted by Turo, not
      my min/max as I had it. But here is the program in full.

      #!c:\perl\bin use warnings; use strict; my $lotNumber; my $productName; my $Info; my $choice; my $printLocal; my $printEcho; my $ftpobj; my $ftp; &MAIN; sub MAIN { print "Please make sure that you have all labels added into labellist. +txt\nin the correct format.\n\nLot number then a tab and then Product + Name for each line\n\nEXAMPLE\:\nE1234567\t2RZ563DA\nE2345678\t2RZ12 +8CA\nE3456789\t2RS128BB\n\n\n"; print " 1\: East QPS\n 2\: East TC\n 3\: West Etest\n 4\: West TC\n 5\ +: Quit\n What is your choice? "; $choice = <STDIN>; chomp ($choice); if ($choice==1) { $printLocal="10\.10\.10\.10"; $printEcho="East QPS Label Printer"; } elsif ($choice==2) { $printLocal="10\.10\.10\.11"; $printEcho="East TC Label Printer"; } elsif ($choice==3) { $printLocal="10\.10\.10\.12"; $printEcho="West TC Label Printer"; } elsif ($choice==4) { $printLocal="10\.10\.10\.13"; $printEcho="West Etest Label Printer"; } elsif ($choice==5) { system "CLS"; print "MultiPrint Has Shut Down."; exit 0; } else { system "CLS"; print "You have chosen an invalid Number, Please try again\n\ +n"; &MAIN; } print "Backing up old test.txt... \n"; rename ("Labels.txt", "test.bac") || die "Cannot rename Labels.txt +: $!"; open (LABELS, "labellist.txt") or die "I could not get at labellist.tx +t as input"; open (OUTPUT, ">>Labels.txt") or die "I could not get at Labels.txt as + ouput"; my %info; while (<LABELS>) { my $printCode = ""; my ($lotNumber,$productName) = m/(\S+)\s+(\S+)/; $info{$lotNumber} = $productName; # print "$lotNumber\n"; # print "$productName\n"; print OUTPUT "\^XA\^LH30,30\n\^FO110,65\^A0,N,100\^FD$lotNumbe +r\^FS\n\^FO150,160\^BY2,3.0\^B3N,N,125,N,N\^FD$lotNumber\^FS\n\^FO75, +375\^A0,N,50,30\^FD$productName\^FS\n\^FO30,425\^BY2,2.0\^B3N,N,50,N, +N\^FD$productName\^FS\n\^XZ\n"; } close LABELS; close OUTPUT; } print "You have chosen to print Labels.txt to the $printEcho ($printLo +cal).\n\n Sending to printer now.\n\n"; use Net::FTP; $ftpobj = Net::FTP -> new("$printLocal", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftpobj -> login("user","") or die "Cannot login ", $ftp->message; $ftpobj -> put ("Labels.txt") or die "put failed ", $ftp->message; $ftpobj -> quit; print `CLS`; &MAIN;
      This gives them a menu option that asks where they want it
      to be printed, then once they chose it is all good.
      It has been tested to work and print the labels
      as wanted. Once again, THANK YOU ALL!


      Of course all confidential info has been changed to
      protect my company :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://549165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-18 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found