The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 27 06-07-2009 06:05 PM
[Perl] Accessing array elements within a sed command in Perl script userix Shell Programming and Scripting 2 10-03-2008 01:05 PM
how to get last value in an array in perl meghana Shell Programming and Scripting 7 02-04-2008 05:12 PM
grep in array -perl zedex Shell Programming and Scripting 1 12-06-2007 03:49 AM
join 2 array in perl? gusla Shell Programming and Scripting 2 09-03-2004 10:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-19-2009
jseager jseager is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 2
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 (permalink)  
Old 01-19-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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 (permalink)  
Old 01-19-2009
jseager jseager is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 2
Quote:
Originally Posted by KevinADC View Post
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 (permalink)  
Old 01-20-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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];
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0