Hi,

I am trying to create a hash and one element is an array. The hash
comes from a csv file.

I use $/ = ",\"\"\n" because there is a new line in each record. The
first 4 elements of each record are always the same and these are
easily assigned to the  hash. After that there are 20 other fields
(keywords) for the record, some blank (that I'd rather discard).

I have 2 problems. One is looping through each record and managing to
loop through $fields[4-20] while assigning them to an array. Two, is
the reverse, accessing the array elements while printing/looping
through the hash. I know a reference is needed here but my attempt at
using the cookbook have failed me.

Below is what I have done so far and some sample data. If anyone has
the time any advice would be appreciated.

Thanx,
Dp.


============== perl script
#!/bin/perl

use strict;

# Open a cvs file and put contents into images.
# File should have fields in this following order:
# 1, SPLNUM 2, Photographer, 3, Title, 4, Credit and captions,
# 5, keywords.

my ($l,$i,$k,$v,$w);    # Predefined global variables;
my %headers;
my (@words);

$/ = ",\"\"\n";         # This defines the record separator as
                        # ,""\n. Note: the csv file will have a newline
                        # between the credit line and the caption.

my $infile = shift;     # Input file

open(FH,"$infile") or die "Can't open $infile: $!\n";

# Read in the file, a record at a time.

while (defined($i = <FH>) ) {

        #print "Record $. = $i\n";

# parse_csv (stolen from a book) takes care of any
# unusual characters that might appear in the keywords
# such as commas, &, [ ...etc.

        my @lines = parse_csv($i);

#       print "\$lines[0] = $lines[0]\n";

# Fields 0,1,2,3 are all pre-defined. 0 => number, 1 => photog,
# 2 => title, 3 => credit and caption, 4 and on => keywords.

        $headers{'splnum'} = $lines[0];

        $headers{'photog'} = $lines[1];

        $headers{'title'} = $lines[2];

        ($headers{'credit'}) = ($lines[3] =~ /CREDIT: (.*\/.*RARY)/);

        (my $cap) = ($lines[3] =~ /.RARY.(.*)/s);
        (my $caption = $cap) =~ s/\n//g;        # Throw away any extra newlines.

        ($headers{'caption'}) = ($caption);

# The rest of the fields in the records will be keywords and these
may
# vary in number but there shouldn't be more than 20.

        my $sizeof = 4;
        my ($d,$keyw_ref);
        my @keywords;

        for ($d = 4; $d <= 20; ++$d ) {
                print "\$lines = $lines[$d] \$sizeof = $sizeof\n";
                push(@keywords,$lines[$sizeof]);

        }
        $keyw_ref = [EMAIL PROTECTED];

        $headers{'keywords'} = $keyw_ref;


# Print the hash keys and values so I can see if everything has been
# assigned correctly

while ( ($k,$v) = each %headers ) {
        if ( $k =~ /keywords/ ) {
                my $item;
                foreach $item (@$keyw_ref) {
                        print "$item\n";
                } i                             # End of foeach $item
        }                                       # End of if

        print "$k => $v\n";
}                                               # End of while

}                       # End of mail while.



# ==== SUBS =====

sub parse_csv {

my $text = shift;
my @new = ();
push(@new, $+) while $text =~ m{
        "([^\"\\]*(?:\\.[^\"\\]*)*)",?
        | ([^,]+),?
        | ,
        }gx;

#push(@new, undef) if substr($text, -1,1) eq ',';  # omitted this as
I hoped to discard the
                                                   # empty fields.

return @new;
}



========== Sampe data ========
"A020/002","JCO","Moulded resistors in synthetic resin","CREDIT: JEAN
COLLOMBET/SCIENCE PHOTO LIBRARY  

Moulded resistors in synthetic resin, achain of which forms the link
between the power supply & in the electrostatic separator.","MOULDED
RESISTORS","RESISTOR, MOULDED","PHYSICS","ATOMIC PHYSICS
RESEARCH","","","","","","","","","","","","","","","",""
"E779/061","DSC","False-colour SEM of a mite attacking a
whitefly","CREDIT: DAVID SCHARF/SCIENCE PHOTO LIBRARY

False-colour scanning electron micrograph (SEM) of a predator mite
attacking a whitefly. Played out on the hairy surface of a leaf, this
predatorial species of mite (at top) has effectively immobili- zed
the whitefly lying beneath it in its grasp, and has begun to devour
the whitefly flesh. In warmer countries, many species of whitefly are
of economic significance as despoilers of plants and fruit. As a
control, research into their natural predators is being conducted,
rather than using chemical insecticides. Predator mites, related to
the spider, may be bred specifically to combat the problem of
whitefly infestations of economically important plants.
Magnification: x18 at 35mm size.","WHITEFLY, ATTACKED BY MITE","MITE,
ATTACKING WHITEFLY","PREDATOR, MITE ON WHITEFLY","BIOLOGICAL PEST
CONTROL","PEST CONTROL, BIOLOGICAL","WHITEFLY, PEST CONTROL","MITE,
PEST CONTROL","","","","","","","","","","","","",""
"E779/007","MDO","Orange predatory mite attacks spider mite","CREDIT:
MARTIN DOHRN/SCIENCE PHOTO LIBRARY

Biological pest control: orange predatory mite (Phytoseiulus
persimilis) attacks spider mite (Tetranychus urticae), a pest of bean
plants. The predatory mites are commercially bred for the biological
control of the pest mites. (7/84)","MITE","RED SPIDER MITE","SPIDER
MITE","BIOLOGICAL PEST CONTROL","PEST CONTROL,
BIOLOGICAL","PHYTOSEIULUS PERSIMILIS","TETRANYCHUS
URTICAE","","","","","","","","","","","","",""
"T395/024","PMZ","SEM of microresonator with mite
(Acarimetaseiulus)","CREDIT: PETER MENZEL/SCIENCE PHOTO LIBRARY

Scanning electron micrograph (SEM) of a mite (Acarimetaseiulus
occidentalis) on the surface of a silicon micro-resonator 'chip'. The
micro- resonator, or 'semaphore structure', is a product of
micromechanics, where mechanical devices are manufactured by
successive deposition of thin layers of silicon. Such micro-
resonators are used in engineering to make tiny vibration sensors.
Only the comb-like detector end of the micro- resonator is seen here,
the thin strand of silicon running out of frame in the top left is
attatched to alarge resonant mass. To give an idea of scale, the
silicon strand is 2 microns thick and 2 microns
wide.","MICROMECHANICS","MICRO-RESONATOR","MITE, ON SILICON
CHIP","ACARIMETASEIULUS OCCIDENTALIS","NANOTECHNOLOGY","MITE, TYPE,
ACARIMETASEIULUS","TECHNOLOGY, NANOTECHNOLOGY","MICROMECHANICS, NANO,
MICRO","MICROMECHANICAL, MECHANICAL","","","","","","","","","","",""
"A850/189","AFM","Biodegradable plastic parts in compost","CREDIT:
ASTRID & HANNS-FRIEDER MICHLER/SCIENCE PHOTO LIBRARY

Biodegradable plastic parts  (red    and    white) decomposing  in  a
compost.   These  plastics are made mainly from plants.
 Biodegradable  materials can   be  broken  down  by  organisms  like
 soil bacteria.","HORIZONTAL","BIODEGRADABLE","PLASTIC PART, PARTS,
PLASTICS","COMPOST","PLANT","ENVIRONMENTALLY-
FRIENDLY","MATERIAL","DECOMPOSING, DECOMPOSITION","EARTH,
SOIL","CHEMISTRY, CHEMICAL","COMPOUND, COMPOUNDS","SYNTHETIC
MATERIAL, MATERIALS","","","","","","","",""
"B250/773","OME","Colour SEM of fungal fruiting body in compost
heap","CREDIT: EYE OF SCIENCE/SCIENCE PHOTO LIBRARY

Compost fungi.    Coloured    scanning    electron micrograph  of
 the  fruiting  bodies  of a fungus growing in a compost heap.
  Compost  is  made  by allowing  a heap  of  vegetable  trimmings,  
non- seeding weed plants and grass mowings to  degrade. The
 resulting brown,  crumbly compost is dug into soil to provide plants
with nutrients. Saprophytic fungi play an important role in the
degradation of the plant material.   Here,    hyphae  (long)  and
fruiting  bodies  (round)  of a saprophytic fungus are seen.  The
fruiting bodies contain the spores. Magnification: x1,000 at 6x6cm
size.","COMPOST, FAUNA AND FLORA","SAPROPHYTE, COMPOST HEAP","FUNGUS,
COMPOST HEAP","MOULD, COMPOST HEAP","SAPROPHYTIC FUNGI,
COMPOST","HYPHAE, MOULD, COMPOST HEAP","SEM","SCANNING ELECTRON
MICROGRAPH","NATURE, BOTANY, MYCOLOGY","FUNGUS, FUNGI,
FUNGAL","","","","","","","","","",""
"Z340/416","OME","Colour SEM of dipteran fly maggot in compost
heap","CREDIT: EYE OF SCIENCE/SCIENCE PHOTO LIBRARY

Maggot in compost.    Coloured  scanning  electron micrograph  of  a
dipteran fly maggot in a compost heap. Compost is made by stacking
plant remains in a  heap.   The  remains degrade to a crumbly brown
compost which when dug into  soil  provides  plant nutrients.   The
maggot is the larval stage in the metamorphosis of a fly from egg to
adult. Eggs are laid  in  compost  heaps as the rotting vegetation
provides the hatching maggots with abundant  food. The  maggot  is
 segmented;   the  mouth end (dark brown) tapers into a thin  wedge
 with  two  mouth hooks to stabilise the maggot on its food. After 2
moults the maggots pupate.  Magnification  x70  at 6x6cm
size.","COMPOST, FAUNA AND FLORA","LARVA, FLY, COMPOST HEAP","MAGGOT,
FLY, COMPOST HEAP","DIPTERA, LARVA, COMPOST HEAP","FLY, LARVA,
COMPOST HEAP","FLY, MAGGOT, COMPOST HEAP","COMPOST, MAGGOT,
FLY","ZOOLOGY, WILDLIFE, NATURE","ANIMAL, INSECTA,
INSECT","","","","","","","","","","",""
"T820/136","XHM","Technician checking temperature of compost
heap","CREDIT: HANK MORGAN/SCIENCE PHOTO LIBRARY

A technician checking the temperature of an urban waste compost heap.
This compost heap at a plant in Minnesota USA is being used to
research recycling of disposable nappies. The nappies are added to
the normal compost waste, taking about 7 days to degrade. The
temperature of the compost heap must remain between 57 and 65 Celsius
during that time. At the end of the process, the compost is used as a
fertiliser by a forestry growing Christmas trees.","RECYCLING,
NAPPIES","NAPPIES, RECYCLING","PAPER PRODUCTS, RECYCLING","COMPOST,
URBAN","URBAN WASTE RECYCLING","INDUSTRY, RECYCLE,
RECYCLING","","","","","",""
~~
Dermot Paikkos * [EMAIL PROTECTED]
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668

The following section of this message contains a file attachment
prepared for transmission using the Internet MIME message format.
If you are using Pegasus Mail, or any other MIME-compliant system,
you should be able to save it or view it from within your mailer.
If you cannot, please ask your system administrator for assistance.

   ---- File information -----------
     File:  test.csv
     Date:  13 Oct 2003, 14:43
     Size:  6333 bytes.
     Type:  Unknown

Attachment: test.csv
Description: Binary data

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to