Thank you McA. Sorry for the error. I was writing the program with variables in Spanish and decided to change to English before posting here. I ran a search & replace on that variable and replaced without $. All edi_file should be $edi_file.
#!/usr/bin/perl
use strict;
use warnings;
use File::HomeDir;
use List::Util 'first';
use List::MoreUtils 'first_index';
use Text::CSV;
my @po = &open_po;
#my @po_no_headings = &erase_headings(@po);
#my @po_separated = &make_product_store_quantity(@po_no_headings);
#print "@po_separated\n";
my ($li_stores_ref, $ff_stores_ref) = &separate_stores;
my @liv_stores = @$li_stores_ref;
my @ffr_stores = @$ff_stores_ref;
print "Li stores: @liv_stores\n";
print "FF Stores: @ffr_stores\n";
# ****************************************
# 1) open .EDI file
# 2) split into lines through "~" line separator
# 3) Place .EDI file contents into @po_array
sub open_po
{
my ($edi_file, $po_data, @po_array);
$edi_file = File::HomeDir->my_home . "/example/po//example.EDI" or d
+ie ".EDI file not found\n";
open ($po_data, '<', $edi_file) or die "Could not open the file 'edi
+_file' $!\n";
undef $/;
@po_array = split (/\~/, <$po_data>);
close $po_data;
return @po_array;
}
# ****************************************
# Erase all information from purchase order before first PO1 line
sub erase_headings
{
my ($delete_point);
$delete_point = (first_index {/PO1/} @_) - 1; # Find first PO1 line
# Shift array up to first PO1 line
for (0..$delete_point) {
shift @_;
}
@_;
}
# ****************************************
# Return array with product, store, quantity
# (i.e. THSB01 00021 3 00043 1 THSB02...etc.)
sub make_product_store_quantity
{
my (@line, @product_stores, $product, $product_index);
while(scalar (@_) !=0) {
@line = split (/\*/, $_[0]); # Split line into fields
# Find product code (i.e. THSB01) and shift array twice
if ($line[0] eq "PO1") {
$product_index = (first_index {/ST/} @line) + 1;
$product = $line[$product_index];
shift;
shift;
}
# Find stores & quantity for each store.
# Write product code followed by stores & quantity to array @product_s
+tores
if ($line[0] eq "SDQ") {
shift (@line);
shift (@line);
shift (@line);
unshift (@line, $product);
push (@product_stores, @line);
shift;
# Exit when PO line starts with CTT, meaning end of PO1 details
} elsif ($line[0] eq "CTT") {
undef (@_);
}
}
@product_stores;
}
# ****************************************
# 1) Open and read stores.csv
# 2) Place all "li" type store numbers in @li_store array
# 3) Place all "ff" type store numbers in @ff_store array
# 4) Return array references
sub separate_stores
{
my (@li_stores, @ff_stores);
my $stores_file = File::HomeDir->my_home . "/example/data/example.cs
+v";
open my $fh, "<", $stores_file or die "$stores_file: $!";
my $csv = Text::CSV->new ({
binary => 1,
auto_diag => 1,
});
my $count_li = 0;
my $count_ff = 0;
$csv ->getline ($fh);
while (my $row = $csv->getline ($fh)) {
if ($row->[1] eq "li") {
$li_stores[$count_li] = $row->[0];
$count_li ++;
} else {
$ff_stores[$count_ff] = $row->[0];
$count_ff ++;
}
}
close $fh;
return (\@li_stores, \@ff_stores);
}