in reply to reference question yet again

There's a bunch of things you're doing incorrectly:

I'm not sure what output you're expecting, is this program OK?

use Text::CSV_XS; my @data; input (); sub input { my @attrs = qw(_status _gwname _ac_ID_field); my $csv = Text::CSV_XS->new; while ( <DATA> ) { chomp; next if /^START/; $csv->parse($_); my @fields = $csv->fields; my %rec; @rec{@attrs} = @fields; push @data, \%rec; } } foreach my $info (@data) { print "$info->{_status}\n"; } __DATA__ adfadfasdfadsf,"adsf,asdf,adsf",adfasdf adsfasdfasdfasdf,asdfasdfasdf,adsfasdf adsfasdf,"adsf,adsf",adsfadsf "asdfasdf,asdfasdf",adfasdf,asdfasdf

Output:

adfadfasdfadsf adsfasdfasdfasdf adsfasdf asdfasdf,asdfasdf

If you're not already using Data::Dumper, you should be. It'll save you hours of frustration.

Replies are listed 'Best First'.
Re^2: reference question yet again
by convenientstore (Pilgrim) on Sep 09, 2007 at 21:35 UTC
    what is wrong with this script now? since I have to expand a @attrs_results I thought I could break it down below but program says _status is not recognzized all the way at the bottom when I try to extract it.
    use strict; use Text::CSV_XS; my @data; my $count = 0; input (); my @attrs_stop = qw( _status _gwname _ac_ID_field); my @attrs_attempt = qw( _status _gwname _ac_ID_field); sub input { my $csv = Text::CSV_XS->new; while ( <DATA> ) { chomp; next if /^START/; $count++; $csv->parse($_); my @fields = $csv->fields; my %rec; my @attrs_results; if (/^STOP/) { @attrs_results = @attrs_stop; } else { @attrs_results = @attrs_attempt; } @rec{@attrs_results} = @fields; push @data, \%rec; } } foreach my $info (@data) { print "$info->{_status}\n"; } print "There were $count calls\n"; __DATA__ adfadfasdfadsf,"adsf,asdf,adsf",adfasdf adsfasdfasdfasdf,asdfasdfasdf,adsfasdf adsfasdf,"adsf,adsf",adsfadsf "asdfasdf,asdfasdf",adfasdf,asdfasdf
      what is wrong with this script now?

      You're calling your input() routine before you've set up the arrays @attrs_stop and @attrs_attempt that you're using in the routine...

        gosh.. thank you.

      Be careful with those global variables. @data worries me.

      Why not let input() return an array ref?

      my $data = input(); foreach my $info (@$data) { # do other stuff } # ------------------ sub input { my @data; # do stuff return \@data; }


      TGI says moo