Hello World,

Ok, so I did some Perl back while I was still in school but it has been a LONG time. So I guess you could call me a newbie to Perl.

I have this program (code will be below) where the Perl file reads in data from a txt file, assigns it to a string then an array, then manipulates the array (removing stuff and adding stuff etc, basic Perl stuff...) then outputs the data to a file.

The txt file contains a header and a Queue. The Queue contains a list of users waiting to use a specific file that is locked by another user.
I only care about the section of the text file under ":LIST.QUEUE"
Below is the text file:

"script_input_3.txt"
UniData Release 7.2 Build: (3786) (c) Copyright Rocket Software, Inc. 1988-2009. All rights reserved. Current UniData home is /usr/udthome/. Current working directory is /usr/local/rfs/udt. :TERM ,0 :UDT.OPTIONS 20 ON :LOGTO /ud/JWP :LIST.QUEUE FILENAME RECORD_ID M OWNER UNBR UNO TTY TIME DA +TE /prod-data/J 00151120273 X jmorg 3584038 247 ts/49 13:38:12 Ju +l 20 ---------------------------------------------------------------------- +---- FILENAME RECORD_ID M WAITING UNBR UNO TTY TIME DA +TE /prod-data/J 00151120273 X jmorg 2015244 134 s/109 13:48:32 Ju +l 20 /prod-data/J 00151120273 X gdavi 1359996 62 ts/20 13:54:22 Ju +l 20 + FILENAME RECORD_ID M OWNER UNBR UNO TTY TIME DA +TE /prod-data/J 001!L!311895 X jmorg 2015244 134 s/109 13:48:32 Ju +l 20 ---------------------------------------------------------------------- +---- FILENAME RECORD_ID M WAITING UNBR UNO TTY TIME DA +TE /prod-data/J 001!L!311895 X jmorg 5713932 191 ts/46 14:01:42 Ju +l 20 + FILENAME RECORD_ID M OWNER UNBR UNO TTY TIME DA +TE /prod-datahi 001!10274882 X rfuse 3354796 61 ts/43 13:39:02 Ju +l 20 ---------------------------------------------------------------------- +---- FILENAME RECORD_ID M WAITING UNBR UNO TTY TIME DA +TE /prod-datahi 001!10274882 X jmorg 3584038 247 ts/49 13:39:22 Ju +l 20 + :


So basically in the "queue" part of the text file it shows that there are 3 locked files. You can tell there are 3 because the lines with only dashes "---", seperate the locked user from the waiting users. Also, they will have the same "RECORD_ID".

Now in my script below, I'm sure there is probably a ton more ways to do this more efficiently but I kinda just chugged through it one problem at a time.
My question has to do with the SUB Routine called mapElements().
Now after I run mapElements() is there a way I can use those "keys" that each piece of the array's elements point to, as you would the same as a variable? I want to be able to pull stuff out easier by using something like a key or reference.

mapQueue.pl

#!/usr/bin/perl use POSIX; use strict; use warnings; use YAML; #readLine is a temp variable to hold the current line of the file. my $readLine = ""; #queue is a string that holds all the data in <INFILE>. my $queue; #array will hold the data from $queue. my @array; #Variable holds the number of locked sessions found by counting lines + with dashes, i.e.--> "--------" my $numLocked = 0; #################### Temporary input test files acting as the real LIS +T.QUEUE #################### #input_1 --- has 1 locked session and 1 user waiting. #open (FILEIN1, "< /home/mmartin/Documents/Prelude_Kill-Sessions/scrip +t_input_1.txt") or exit 2; #input_2 --- has 1 locked session and 3 users waiting. #open (FILEIN2, "< /home/mmartin/Documents/Prelude_Kill-Sessions/scrip +t_input_2.txt") or exit 2; #input_3 --- has 3 locked sessions and at least one user waiting. open (FILEIN3, "< /home/mmartin/Documents/Prelude_Kill-Sessions/script +_input_3.txt") or exit 2; #input_4 --- has 0 locks --> EMPTY Queue #open (FILEIN4, "< /home/mmartin/Documents/Prelude_Kill-Sessions/scrip +t_input_4.txt") or exit 2; ###################################################################### +############################ #Read in file and append each line to a string call $string. my $x = 0; #temp variables to hold counts. my $i = 0; while (<FILEIN3>) { $readLine = $_; if ($x >= 10) { $queue .= $readLine; $i++; } $x++; } close(FILEIN3); #SUB clipEnd () --- loops through array and deletes uneeded lines from + the end of the # array of the specified length and offset. sub clipEnd { for (my $y = length($queue); $y > 0; $y--) { if (substr($queue, $y, 1) =~ ':' || substr($queue, $y, 1) =~ ' + ' || substr($queue, $y, 1) =~ /\n/) { substr($queue, $y, 1, ""); } elsif (substr($queue, $y, 1) =~ /[A-Za-z0-9]/) { last; } } } #SUB buildArray () --- builds an array using the value of $queue, and +removes empty lines, # uneeded lines, and extra whitespace. sub buildArray { #Split $string on the newline char and assign to $array. @array = split /\n/, $queue; #Run sub routine addLock(). addLock(); #Splice out the header lines throughout the array. for (my $y = 0; $y <= $#array; $y++) { if (defined $array[$y]) { if ($array[$y] =~ /^FILENAME/) { splice (@array, $y, 1); } } } #Splice out lines that contain all dashes. i.e. "---" for (my $y = 0; $y <= $#array; $y++) { if (defined $array[$y]) { if ($array[$y] =~ /^-+/) { splice (@array, $y, 1); } } } #Splice out empty lines. for (my $y = 0; $y <= $#array; $y++) { if (defined $array[$y]) { if ($array[$y] =~ /^\s{4,}/) { splice (@array, $y, 1); } } } } #SUB mapElements() --- takes the array of data and maps each element o +f a # single array element to a key. sub mapElements { @array = map { my ($file, $recordID, $M, $owner, $pid, $userID, $t +ty, $time, $month, $day, $locker) = split; { file => $file, recordID => $recordID, M => $M, owner => $owner, pid => $pid, usedID => $userID, tty => $tty, time => $time, month => $month, day => $day, locker => $locker } } @array; print Dump \@array; } #SUB addLock() --- this sub loops through the array, and if it is the +user who locked # the file then add an Identifier "L" to the end of the elem +ent. If # no then add a "W" for user waiting. sub addLock { #Remove trailing whitespace from the end of each array element. for (my $x = 0; $x <= $#array; $x++) { if ($array[$x] =~ / +$/) { chop $array[$x]; } } #Add keyword "Locked" to an element where the user has a locked se +ssion. for (my $x = 0; $x <= $#array; $x++) { if ($array[$x+1] =~ /^--+/) { $array[$x] .= " Locked"; } } #Add keyword "Waiting" to an element where the user is waiting on +a locked session. for (my $x = 0; $x <= $#array; $x++) { if ($array[$x] !~ /Locked$/) { $array[$x] .= " Waiting"; } } } # SUB trimWhitespace() --- This sub will loop through the array and se +arch for spots that have # more than one 'whitespaces' and replace them with only + one space. sub trimWhitespace { for (my $x = 0; $x <= $#array; $x++) { if ($array[$x] =~ /\s{2,}/) { $array[$x] =~ s/\s+/ /g; } } } #Execute sub routine "clipEnd()". clipEnd; #Execute sub routine "buildArray()". buildArray; #Execute sub routine "trimWhitespace()". trimWhitespace; #Print the array to a file. open (FILEOUT, "> /home/mmartin/Documents/Prelude_Kill-Sessions/MYoutp +ut2.txt") or exit 2; for (my $x = 0; $x <= $#array; $x++) { print FILEOUT "$array[$x]\n"; } close (FILEOUT); #Print the whole array to screen print "\n\n=========================== Array ========================= +===\n"; for (my $x = 0; $x <= $#array; $x++) { print "$array[$x]\n"; } print "==============================================================\ +n\n"; #Call SUB Routine mapElements(), which will also dump the data in the +array out to the screen. mapElements;




Could someone also explain to me how the lines in mapElements() sub that uses the Dump and Map functions work? Not really sure how they work, I had just found of an example online using the map and dump functions?

Thanks in Advance,
Matt

In reply to How to reference to array keys? by mmartin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.