Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching Problem 1

Status
Not open for further replies.

nogs

Technical User
Aug 3, 2001
89
GB
I am trying to strip 2 strings from a file and then add select parts of them together, for some reason the output is being duplicated any ideas??
Here is some of the script I am using;

open (TMP, "c:/mysql/data/sbmtmp.txt") or die "Cannot open output.txt for read :$!";

while (<TMP>)
{
chomp;
if ($_ =~ /^Product Code :/)
{
($product_code,$prod_des) = unpack(&quot;x23 A10 x7 41&quot;, $_);
}
if ($_ =~ /PALLETS /)
{
$plt_qty = unpack(&quot;x78 A3&quot;, $_);
}
printf &quot;$product_code, $prod_des, $product_code, plt_qty\n&quot;;

} Nogs
[ponder]
 
Hi Nogs,

Post a few lines of sample input, and then the output you *want* (as opposed to what you're getting :)) Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Hello Mike
good to hear from you again..
here is some of the input file I am using;
Company Code : PY ANY CO LIMITED
Product Code : 3400025 NIBLETS ORIGINAL 12X198G Product Type : GG BLUE DWARF
Shelf Life : 208 Weeks Replenish Level : N/A Pallets Per Area : N/A
Min Shelf Life : 52 Product Value : N/A Pref Receipt Unit : PALLETS
Std Storage Media : APR Pref Report Unit : CASES
Picking Locations : 11126A Requires Deboxing : No
Units of Order : CASES
Configurations : CASES Include Pallet : No Total Quantity : N/A Weight : 3.200 kg
Pallet Type : N/A Quantity/Level : N/A Height : 85 mm
Storage Media : Standard Max Stack Ht :
PALLETS Include Pallet : Yes Total Quantity : 288 CASES Weight : 921.600 kg
Pallet Type : GKN Quantity/Level : 18 Height : 1360 mm
Storage Media : Standard Max Stack Ht : 0

and here is what I would like to see;
3400025, NIBLETS ORIGINAL 12X198G, 3400025, 288 Nogs
[ponder]
 
Hiya Nogs, try something like this

[tt]
while(<>){
next unless /^Product Code/;
/:\s/; $_ = $'; /\s/; $prod_code = $`;
$_=$'; /^\s+/; $_ = $'; /\s+Product Type/; $prod_desc = $`;
while(<DATA>){
next unless /^\s+PALLETS/;
/Total Quantity : /; $_ = $'; /\s+/; $qty = $`;
last;
}
print &quot;pc=$prod_code, pd=$prod_desc, pc=$prod_code, q=$qty\n&quot;;
}
[/tt]
Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Thanks Mike
had to change a few little things but that works great, my only question is how does your script pick out the chunks of data from the selected lines?? Nogs
[ponder]
 
By Magic :)

Full details in perlvar documentation, but here's what I used (plus a bit).

$_ = &quot;Perl's Magic Pattern Matching&quot;;

# find the string &quot; Magic&quot; in $_;
/\s+Magic/;
# after a match, some variables are set for you

# built-in variable $` contains what was *before* the matched string
$first_word = $`;

# built-in variable $& contains what was actually matched
$matched_string = $&;

# built-in variable $' contains what was *after* the match
$rest_of_line = $';

Powerful but cryptic... I should have commented my answer :)
There's a module called English which gives more readable names to these variables and more - but I always forget to use it.

Try the example above, print out the variables etc. I love it and if they ever take it out of Perl I'm sunk. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top