FAQ
I have a reference table that I want to put in a hash and then when I read in a given line with a matching key,
be able to pick up the array and assign the elements and manipulate the elements (not while they are part of hash.

Here is how I set up the hash:

%TITLES = ();
open(RTITLE, '/common/lib/rtitles') || die "Cant open rtitles: $!\n";
while (<RTITLE>) {
unless (/^#/) {
chomp;
($title, $rest) = split ' ', $_, 2;
@fields = split ' ',$rest;
$TITLES{$title} = [ @fields ];
}
}

The records in rtitles look like this:

TAPE_EXTRACT_REPORT 0 0 0 0 0 0 0 0 0

I can print them out correctly with this:

for $line (keys %TITLES) {
print "$line: @{ $TITLES{$line} }\n";
}

PROBLEM: I want to take the zeros which shortly will take on other values into variables when I get a key match. Here is my code to
do that:

foreach $keytitle (keys %TITLES) {
if ( grep /$keytitle/, $lookup ) {
($im, $rptid, $edate, $rdate, $unused) = split ' ', @{ $TITLES{$keytitle} }, 5 [1 2 3 4 5];
print "LookUp: $lookup\n";
print "Report Name: $rptname\n";
print "Receive Date: $rcvdate\n";
print "Process Date: $procdate\n";

print "IMAGE: $im RPTID: $rptid EFFDATE: $edate RUNDATE: $rdate UNUSED: $unused\n";
exit;

What I get out of the last print line is:

IMAGE: 9 RPTID: EFFDATE: RUNDATE: UNUSED:

9 just happens to be the count of elements in an entry. My statement after the grep is patterned after the statement above PROBLEM that prints out the hash array but now doesn't work to pull out the elements. I obviously am getting a count. How do I get the elements of the string?

Man is this cool stuff, BUT it really hurts my poor brain. I feel frazzeled after a days worth of Perling. Don't get me wrong, I love every minute of it, but at 60+ I think I need ear plugs 'cause it must be leaking out of my ears. :-)

TIA


--
-------------------------------------------------------------------------
"They that can give up essential liberty
to obtain a little temporary safety
deserve neither liberty nor safety."

-- Benjamin Franklin
-------------------------------------------------------------------------
RRRRR Gary Luther
RR RR SAF
RR RR UTABEGAS 2500 Broadway
RR RR Helena, MT 59602
RRRR gluther@safmt.org
RR RR ULE !!
RR RR Visit our website at
RR RR http://www.safmt.org

Search Discussions

  • Me at Jun 28, 2001 at 12:47 am
    ($title, $rest) = split ' ', $_, 2;
    @fields = split ' ',$rest;

    last two lines === ($title, @fields) = split ' ';
    which is almost ($title, @fields) = split;

    foreach $keytitle (keys %TITLES) {
    if ( grep /$keytitle/, $lookup ) {

    $lookup is an array ref?

    ($im, $rptid, $edate, $rdate, $unused) = split ' ',
    @{ $TITLES{$keytitle} }, 5 [1 2 3 4 5];

    wazzat?!?

    @{ $TITLES{$keytitle} } is an array.
    split expects a string scalar there.
    an array in such a context returns its length.

    as for:

    5 [1 2 3 4 5]

    i have absolutely no idea what is going on with that.
    how on earth does that compile?

    exit;

    exit?

    where's the closing }s (curly braces) for the foreach and if?
    this stuff can't possibly compile. are you cutting and pasting
    code to the list selectively?

    anyhoo, I suggest something like:

    foreach $keytitle (keys %TITLES) {
    if ( grep /$keytitle/, $lookup ) {
    ($im, $rptid, $edate, $rdate, $unused) = @{
    $TITLES{$keytitle} };

    Man is this cool stuff, BUT it really hurts my poor brain.

    But it's fun, ain't it!?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 27, '01 at 11:10p
activeJun 28, '01 at 12:47a
posts2
users2
websiteperl.org

2 users in discussion

Gary Luther: 1 post Me: 1 post

People

Translate

site design / logo © 2021 Grokbase