I'm working in Perl.
I start from a tab-delimited txt file with two columns.
cat1 val1
cat1 val2
cat2 val3
cat3 val4
cat1 val5
cat4 val6
I want to push the unique categories from column 1 into an array & create empty variables that have the same name as these unique categories
so at the end I would have:
@unique_categories = ("cat1", "cat2", "cat3", "cat4");
$cat1 = '';
$cat2 = '';
$cat3 = '';
$cat4 = '';
This is what I've tried:
#! /usr/bin/perl
use strict;
use warnings;
open(my $file,'<',"file.txt") || die ("Could not open file $!"); #input file
my $categories = '';
my @categories_unique = '';
while(<$file>){
chomp;
my $line = $_;
my @elements = split ("\t", $line);
$categories = $elements[0]; #everything seems fine until here
push(@categories_unique, $categories) unless grep{$_ eq $categories} @categories_unique; #with this line I want to store the unique values in an array
#here I want to create the empty variables, but don't know how to
}