develooper Front page | perl.beginners | Postings from October 2009

Re: Printing the contents of an array

Thread Previous
From:
Jim Gibson
Date:
October 22, 2009 23:23
Subject:
Re: Printing the contents of an array
Message ID:
p06240802c706f780d614@[192.168.1.3]
At 11:21 AM +0530 10/23/09, <pradeepkumar.surabhi@wipro.com> wrote:
>Hi,
>
>The contents in my array looks like this...
>change_id="B77_ip_sync_idl"  state="released" customer_rel="B77"
>change_name="ip_vke_sync" short_description="This feature introduces the
>Sync idl." planned_baseline="B77_135.00"
>change_id="B77_ip_sync_idl"  state="accepted" customer_rel="B77"
>change_name="ip_sync" short_description="This feature introduces the
>Sync ." planned_baseline="B77_136.00"


Where is this array? What is its name? In your program, you are 
reading from a file, not an array. The only array is @fields, which 
contains the results of split on each line in the file. It is hard to 
tell from the above data where your lines start and stop. At least 
one line looks like it is wrapped, since a string in quotes is 
continued from one line to the next.

>
>How do i just print planned_baseline="B77_135.00"  & state="released" ?

You find that data in your input according to whatever rules you 
determine about the structure of your input and print the data 
accordingly. What about those elements make you want to print them? 
The fact that your lines vary in length and format make it difficult 
to extract the information you want.

>
>I tried the logic in the script below but it didn't help, as the fields
>keep changing everytime in the array.
>
>#!/usr/bin/perl -w


You should have the following line in all Perl programs to help you 
catch errors:

use strict;

The "-w" switch turns on warnings, but a better, more flexible way is 
to put the following line in your program:

use warnings;

>
>$filename="/home/pradeep/report1.txt";
>
>open FILE, "< $filename" or die "Can't open input file: $!";


The 3-argument form of open is better, as are lexically-scoped file 
handle variables:

open( my $file, '<', $filename ) or die("Can't open $filename: $!");

>
>  while (my $line = <FILE>) {


This line should then be

while( my $line = <$file> ) {

>           my @fields = split(" ", $line);      # Fields separated by
>colons

Your fields are not separated by colons; they are separated by 
spaces, so this comment is confusing. In addition, some of your 
fields have spaces in them, so a simple split will not give you your 
fields intact.

>           print "$fields[2] $fields[3]\n";     # Display selected fields


Your selected fields are not always in the 3rd and 4th positions in the array.

>           print "@fields\n";     # Display selected fields


Add some separators to tell where you fields are:

   print join(',',@fields),"\n";

>         }
>
>Any help would be appreciated.

You might try splitting on spaces, but then combining successive 
array elements until the data fields begin and end with quotes. You 
could also try splitting on either the strings q(=") or q(" ?) 
(untested):

	my @fields = split(/="|"\s?), $line;

That might work.

You could also use regular expressions:

   while( $line =~ /\s*(\w+)="([^"]+)"/g ) {
     print "$1: $2\n";
   }


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About