The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
[Originally posted by steve@univeg]

Dear Dave,

Trying my best to implement your methods for building a data structure
from a file but getting lost in understanding how to access the data once i
have built the structure. Maybe i'm approaching the problem from the wrong
angle. I have included the program i have been experimenting with here. What i
can't quite grasp is; from the references that the array @sales contains, can
i access the data that each of those references holds ? I hope my end result
will be the ability to print out one line for each code that gives the sales
qty and value for each period ie

TOM001 50 250 30 120 70 300 where 50, 30 & 70 are qtys and 250, 120 & 300 are
the values.

I hope i'm not too far away from building the right structure here but you may
think i've made a right hash (pardon the pun) of things ! I'm determined to
get to grips with this

Best regards


#!/usr/bin/perl -w
use strict;
#use warnings;

my %rec;
my @attrs;
my @sales;
my $ref = @sales;
my %codes;
my $code;

# Initially, the input file to be processed is pipe separated
# and 3 example records from it are shown below. Each code may exist
# up to as many as 12 times

# SVC007|Savoy Cabbage x 8 O/W JS|14307.0|10|2000|42349.0|
# SVC024|Organic Savoy Cabbage x 8 JS|1657.0|10|2000|9283.0|
# TOM001|Organic Tomatoes Bulk|0.51|10|2000|716.0|

open FILE, '/tmp/steve/mytest.txt' or die "Can't open mytest.txt : $!";
open OUT, '>/tmp/steve/sales9.txt' or die "Can't open sales9.txt : $!";
my $outfile = select OUT;

# We begin by reading our input file in which the fields are pipe '|'
# separated and the records are separated by a new line '
# The output file will be TAB separated

while (<FILE>smilie {

# Define the input and list separator variables

local $/ = "
local $" = " ";

# Don't forget to escape the special meaning of the pipe symbol !

my @fields = split(/|/, $_);

print "@fields";

select $outfile;
print "$. records were processed.
close FILE;
close OUT;

# Now we have converted our file to be TAB separated and the records
# now look like this :

# SVC007 Savoy Cabbage x 8 O/W JS 14307.0 10 2000 42349.0
# SVC024 Organic Savoy Cabbage x 8 JS 1657.0 10 2000 9283.0
# TOM001 Organic Tomatoes Bulk 0.51 10 2000 716.0

open OUT, '/tmp/steve/sales9.txt' or die "Can't open sales9.txt : $!";

# We name the six fields of each record and assign them as below

@attrs = qw(code desc qty period year value);

while (<OUT>smilie {

# We split on the TAB
@rec{@attrs} = split / /;
push @sales, \%rec;

# As an exercise, count the instances of each code in the file
# as it arises

$code = (split / /) [0];

# When uncommented, the two print statements shown here, produce
# an identical result, displaying the description field of each
# record as it is processed.

#print $ref->[0]->{desc}, "
#print ${rec}{desc}, "
#sleep 1;
# As i understand things, (not very well as you can see) hash keys
# must remain unique, so i assume as each duplication of a code is
# encountered, it will be discarded. Values for each key, can be multiple
# The statement below seems to prove this;

foreach (sort keys %codes) {
print "For $_,there are $codes{$_} instances of this code in the file

# The print statement below, only prints the description for the last
# record processed. So what's happened to the rest. The array @sales
# contains the same number of elements as there are records in the file
# but how can i get at what data is stored in those referenced elements ?

print "$rec{desc}