in reply to Building a dynamic array or some other method?

As far as I can tell, there are 3 questions here.

Firstly:
From a unix-like prompt, just do head -1 infile > outfile; cat tail -n+2 infile | sort -u >> outfile
But if you must use Perl (yay):
open IN,'<',$csvfile; @data=<IN>; close IN; open OUT,'>',$newfile; print OUT shift @data; foreach my $line (@data) {$uniq{$line}=1;} foreach my $line (keys %uniq) {print OUT $line;} close OUT;
Please do the usual 'strict' and 'warning's stuff.


Secondly, you talk about putting the data into some sort of structure in memory...
Here I second cavac's request for a SSCCE


Thirdly, to create deep structures, mostly Perl will 'Just do it for you'.
E.G. I can do $data{A}{B}{C}=[0,1]; $data{A}{X}=[5]; and I'll end up with the weird monstrosity I asked for:
DB<2> x \%data 0 HASH(0x555e18bb11b8) 'A' => HASH(0x555e184f6448) 'B' => HASH(0x555e18bb1218) 'C' => ARRAY(0x555e18bb1290) 0 0 1 1 'X' => ARRAY(0x555e18bb1278) 0 5 DB<3>

HTH!

Replies are listed 'Best First'.
Re^2: Building a dynamic array or some other method?
by jdporter (Paladin) on Apr 23, 2024 at 14:28 UTC

    I'd recommend always putting [SSCCE] in brackets, to make a link: SSCCE.

      Thank you for the link to SSCCE. It's very helpful, and points out many areas of improvement for my future posts.
Re^2: Building a dynamic array or some other method?
by CAdood (Acolyte) on Apr 23, 2024 at 07:47 UTC
    Sadly, the file is already sorted, and making it uniq based on one field isn't enough. Thank you though!

    Your deep structure uh, scares me. I'm just learning how to access the structure in $aoh (array of hashes) above and it's already been painful enough! (I'm crying uncle here!) I like where cavac was going with it. De-referencing arrays has been very painful for me.

    I knew I was going to get in trouble for the lack of use Strict and use Warning. (LOL)

    I don't know what HTH! means. (I'm new here)
      HTH = Hope This Helps

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Both solutions I offered filter the entire line.
      Not just one field.

      Unless I've totally misunderstood what you mean by CSV?

      (As an aside, if the source is already filtered, unix has a command uniq that removes duplicate lines from pre-sorted files,so in my first solution just change sort -u for uniq)