Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Splicing Arrays on pre-defined indices

by harishnuti (Beadle)
on Jun 27, 2008 at 17:51 UTC ( [id://694403]=note: print w/replies, xml ) Need Help??


in reply to Re: Splicing Arrays on pre-defined indices
in thread Splicing Arrays on pre-defined indices

Iam sorry if my first post confused a bit... Ok here is what iam doing... ** COBOL Program which extracts information from database to flat file with ! as delimiter... ** My perl programs reads the flat file and should convert it to CSV file based on value at Col 17( contains clients names) ** one CSV file per client
my %filehash = (); while (<>){ @pline = split (/\!/,$_); my $keycolumn = $pline[15]; # based on this col values split files unless ($filehash{$keycolumn}){ $filehash{$keycolumn}{name} = $pline[15]; open $filehash{$keycolumn}{handle},">$filehash{$keycolumn}{n +ame}; # open distinct file handles for each distinct value found in c +ol 15 } print {$filehash{$keycolumn}{handle}} @pline; } # In the above code iam considering all col's , but my new requirement + is , for some rows i should only some predifined col's which can 1, +3,5,6,7 i.e. i need slice these indices from @pline before writing it + into corresponding file

Replies are listed 'Best First'.
Re^3: Splicing Arrays on pre-defined indices
by harishnuti (Beadle) on Jun 27, 2008 at 18:01 UTC
    Thx for reply, let me make my question rather more simple
    # Assume below line is read from a file my @array = qw/i will skip some words here using splice/; print "Array before deletion \n"; print join("-->",@array),"\n"; print "Enter the Indices which you like to skip\n"; chomp($indices=<STDIN>); # i entered 2,4,5 my @indices = split(/\,/,$indices); foreach (@indices){ splice (@array,$_,1); # remove element } print "Updated array is as below \n"; print join("==>",@array),"\n"; # if there is any easy way of doing above in short , i can integrate i +n my existing code

      All you need is an array slice:

      ## Assume @array contains fields (from wherever) ## Assume @indices contains field numbers to keep (from wherever) @array = @array[ @indices ]; ## Now, @array only contains the fields you want to keep.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://694403]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found