Perl display selected data with a specific argument with Getopt::Long

I have a file with input this

Store::ID_AZD|AZD|Category::1314559|Séries
Store::ID_AZD|AZD|Category::1314557|Emissions
Store::ID_AZD|AZD|Category::1314560|Jeunesse
Store::ID_AZD|AZD|Category::1314558|Information
Store::ID_FRA|CHANNEL 2|Category::1294563|Séries
Store::ID_FRA|CHANNEL 2|Category::1294059|Info
Store::ID_FRA|CHANNEL 2|Category::1295062|Magazine
Store::ID_FRA|CHANNEL 2|Category::1300056|Documentaire
Store::ID_FRA|CHANNEL 2|Category::1299056|DIVERTISSEMENT
Store::ID_FRA|CHANNEL 2|Category::1295060|Jeu
Store::ID_FRA|CHANNEL 2|Category::1294058|Sport
Store::ID_FRA|CHANNEL 2|Category::1294562|Culture
Store::ID_GRB|CHANNEL 3|Category::1295063|Séries
Store::ID_GRB|CHANNEL 3|Category::1295059|Info
Store::ID_GRB|CHANNEL 3|Category::1295058|Magazine
Store::ID_GRB|CHANNEL 3|Category::1296557|Documentaire
Store::ID_GRB|CHANNEL 3|Category::1300055|DIVERTISSEMENT
Store::ID_GRB|CHANNEL 3|Category::1294576|Jeunesse
Store::ID_GRB|CHANNEL 3|Category::1294559|Jeu
Store::ID_GRB|CHANNEL 3|Category::1295057|Sport
Store::ID_GRB|CHANNEL 3|Category::1295556|Culture
Store::ID_UKR|CHANNEL 5|Category::1294577|Jeunesse
Store::ID_UKR|CHANNEL 5|Category::1296055|Info
Store::ID_UKR|CHANNEL 5|Category::1295061|Magazine
Store::ID_UKR|CHANNEL 5|Category::1294556|Documentaire
Store::ID_UKR|CHANNEL 5|Category::1299556|Culture
Store::ID_UKR|CHANNEL 5|Category::1326557|Sport

I would like to run my script with the command line perl cat.pl --name "Store::ID_FRA" The option is --name and could be any Store::ID from the first column, here I choose "Store::ID_FRA" . If no option set from the user, the script must do nothing.

I would like to have into a %getopt (the result of the concatenation of Store::ID and category chosen by the user in order to iterate with later) and display the result of each categories which are attached from the Store::ID

Store::ID_FRA|Category::1294563
Store::ID_FRA|Category::1294059
Store::ID_FRA|Category::1295062
Store::ID_FRA|Category::1300056
Store::ID_FRA|Category::1299056
Store::ID_FRA|Category::1295060
Store::ID_FRA|Category::1294058
Store::ID_FRA|Category::1294562

What I've done so far is to get into the %hash my input file with key Store::ID."|".Category and for values the file itself

Then I create another %channel to have only the Store::ID I may think that it can be possible to have this kind of data with a hash into another hash. I don't know how to process

Here the script

#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use feature 'say';
use Getopt::Long;

my $file = "/home/load/categories_file";
my (%hash,%channel,%getop) = ();

# Script
if (-e $file && ! -z $file) {
    open (my $TOP, "<", $file) or die ("Can't open \"$file\": $!\n");
    while (<$TOP>) {
        chomp;
        my @tab = split(/\|/, $_);
        my ($id,$name,$categories,$cat_name) = ($tab[0],$tab[1],$tab[2],$tab[3]);
        if (! $hash{$id."|".$categories}) {
             $hash{$id."|".$categories} = $id . "|" . $name . "|" . $categories . "|" . $cat_name;
             $channel{$id} = $id;
        }
    }
    close ($TOP);
}


# Getopt
GetOptions( "name:s" => \my $name );
if (defined $name) {
        $getop{$name} = $name;
}
else {
        my %exclude = (
                "null","null"
        );
        foreach my $line (sort keys %channel) {
                if (! $getop{$line}) {
                        $getop{$line} = $line;
                        if ($exclude{$line}) {
                                delete ($getop{$line});
                        }
                }
        }
}

if (%getopt) {
    foreach (sort values %getopt) {
        # do something;
    }
}

__END__

Solution 1:

It is not completely clear to me what you want to achieve, but does the following help you:

use feature qw(say);
use strict;
use warnings;
use Getopt::Long;

my $file = "categories_file";
my %hash;
open (my $TOP, "<", $file) or die "Can't open \"$file\": $!\n";
while (<$TOP>) {
    chomp;
    my ($id, $channel, $category, $cat_name) = split /\|/;
    $id =~ s/^\QStore::\E//;
    $category =~ s/^\QCategory::\E//;
    push @{$hash{$id}{categories}}, $category;
    push @{$hash{$id}{channels}}, $channel;
    push @{$hash{$id}{cat_names}}, $cat_name;
}
close ($TOP);

GetOptions( "name:s" => \my $name ) or die "Bad options";
if (exists $hash{$name}) {
    my $categories = $hash{$name}->{categories};
    my $channels = $hash{$name}->{channels};
    my $cat_names = $hash{$name}->{cat_names};
    # do something
}
else {
    say "Name $name not found!";
}