Perl Array Error when run - I think???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Array Error when run - I think???
# 1  
Old 01-19-2009
Perl Array Error when run - I think???

My code isn't pretty, my verbage isn't exact and I probably don't have all my facts together (story of my life). I know what I want with this script: to join three simple lines line 1,2,3 then repeat over and over again with 4,5,6 until the end of the file. the lines are the same, date,date,text. The text line will often end in a date however making things difficult for me, I think. My current code when run gives me this output:

ARRAY (0x99a03c)
ARRAY (0x99a08c) ........... I imagine hidden behind all that is my data?


</c>
my $line = <fh>;

# Skip first line
die if !defined($line);
$line = <fh>;

# Skip second line
die if !defined($line);
$line = <fh>;

while (defined($line) && $line !~ /^© Copyright/) {
chomp( my $date1 = $line );
$line = <fh>;

die if !defined($line);
chomp( my $date2 = $line );
$line = <fh>;

die if !defined($line);
my $body = $line;
$line = <fh>;

my $body = '';
while ( defined($line)
&& $line !~ /^\d{2}\/\d{2}\/\d{4}$/
&& $line !~ /^© Copyright/
) {
$body .= $line;
$line = <fh>;
}
chomp( $body );
push @recs, [ $date1, $date2, $body ];

}
print @recs, [ $date1, $date2, $body ];

foreach(@recs) {
print "array element: $_\n";
}

select ($out);
printf @res;


# Skip copyright line
die if !defined($line);
$line = <fh>;

# Skip another line
die if !defined($line);
$line = <fh>;
die if defined($line);

close ($out);
close (fh);

</c>


Thanks,
Jon
# 2  
Old 01-19-2009
You have an array of arrays, so instead of this:

Code:
foreach(@recs) {
   print "array element: $_\n";
}

you need to dereference the arrays that are in the @recs array:

Code:
foreach (@recs) {
   print "array elements: @{$_}\n";
}

# 3  
Old 01-19-2009
Quote:
Originally Posted by KevinADC
You have an array of arrays, so instead of this:

Code:
foreach(@recs) {
   print "array element: $_\n";
}

you need to dereference the arrays that are in the @recs array:

Code:
foreach (@recs) {
   print "array elements: @{$_}\n";
}



HOORAY!!! This got me past the annoying frosted glass problem. I can now see the two date fields. But what, pray tell, have I done wrong that is prohibiting the text field from showing up. The third line. Below is the actual data I am working with. The FIX you provided allows me to only view the Dates. Thanks again. Jon

01/16/2009
01/14/2009
Document #:R40129:> Projections of FY2009 Federal SCHIP Allotments Under CHIPRA 2009
01/16/2009
01/14/2009
Document #:R40130:> H.R. 2: The Children's Health Insurance Program Reauthorization Act of 2009
01/16/2009
01/13/2009
Document #:R40131:> Administrative Appeals in the Bureau of Land Management (BLM) and the Forest Service
# 4  
Old 01-20-2009
Hard to say, your code is, well, uhh...... strange. If your file is consistent and its:

date
date
text
date
date
text

there is no need to do all that checking, just put the three lines into your array

chomp(my $d1 = <fh>);
chomp(my $d2= <fh>);
chomp(my $line = <fh>);
push @recs, [$di, $d2, $line];
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

5. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

6. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

7. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

8. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

9. Shell Programming and Scripting

Run perl file in Crontab error.

Hi. I can run the script okay when I log in with root. Then I add this script to crontab and when crontab run i got this message error. I has installed DBD for Oracle already. I set ORACLE_HOME, ORACLE_BASE to root profile already. Please help me to fix it. It urgent. Thank so much. ... (2 Replies)
Discussion started by: raccsdl
2 Replies

10. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question