Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Parsing output from the CSV file

by kar_333 (Initiate)
on Feb 07, 2013 at 15:53 UTC ( #1017681=perlquestion: print w/replies, xml ) Need Help??
kar_333 has asked for the wisdom of the Perl Monks concerning the following question:

hello monks

I am having problem in parsing the output from the CSV file. I want to add pipe symbol in between the character to do mutliple search similar to egrep

open(my $data, '<', $Config_File) or die "Could not open '$Config_File +' $!\n"; my $reg_exp; my $severity; my @fields=(); while (my $line = <$data>) { chomp $line; if(!$line =~ /^$/) { @fields = split "," , $line; $reg_exp = $fields[0]; $severity = $fields[1]; print $reg_exp; } } #print $fields[0]; #last unless defined $line; close($data) ;

current Output

service entered the stopped stateservice entered the running state

desired output

service entered the stopped state|service entered the running state

Replies are listed 'Best First'.
Re: Parsing output from the CSV file
by Tux (Abbot) on Feb 07, 2013 at 16:03 UTC
Re: Parsing output from the CSV file
by CountZero (Bishop) on Feb 07, 2013 at 19:48 UTC
    Can you show us some lines of your input CSV-file?


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Parsing output from the CSV file
by manorhce (Beadle) on Feb 07, 2013 at 19:51 UTC

    Use like the below it will parse the data and will create fields according

    $csv = Text::CSV::Simple->new({ sep_char => q{,}, binary => 1 }); $csv->field_map( qw( field1 field2 field3 field4 ) ); my @objectlist = $csv->read_file($file_name); foreach my $data (@objectlit) { print $data->{field1}."field2",$data->{field2} }
Re: Parsing output from the CSV file
by GotToBTru (Prior) on Feb 08, 2013 at 22:14 UTC
    You never introduce the | character. I would suggest replacing:
    print $reg_exp;
    $accum .= "$reg_exp,";
    and then when ready to output your pipe delimited expression:
    $accum =~s/,$//; $accum =~ s/,/|/g; print $accum;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017681]
Approved by BrowserUk
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-02-25 15:57 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (314 votes). Check out past polls.