How to store inventory using Perl hashes?

For college, we need to make a script in Perl, which allows us to manage inventory for an electronic store. (The given example was Amazon). Users can place orders in a fully text environment, and inventory must be updated after order completion.

Each item in the inventory has from 3 to 4 attributes: product code, title, price and some amount (for example, MP3 does not have this attribute)

Since this is my first meeting with Perl, I don’t know how to start. My main problem is how I should “implement” the inventory in the program. One of the functions of the program is a search by name. Another is to place an order where the user must specify the product code.

My first idea was a hash with product code as a key. But if I wanted to look in the headers that might be the problem because of this: hashkey will be something like DVD-123, the information belonging to this key could be “Green Mask 12” (without quotes), where 12 indicates how many of this DVDs are currently in stock. So I would have to find a way to ignore 12 at the end.

Another solution was to use the name as a key, but that would also be cumbersome.

Is there a way to make a hash table with two keys, and when I give only one, it returns an array with other values? (Including another key and other information) Thus, I could use a different key depending on what information I need from my inventory.

, :

MP3-72|Lady Gaga - Kiss and Run (Fear of Commitment Monster)|0.99  
CD-400|Kings of Leon - Only By The Night|14.50|2  
MP3-401|Kings of Leon - Closer|0.85  
DVD-144|Live Free or Die Hard|14.99|2  
SOFT-864|Windows Vista|49.95  
+3
4

, -, SQL , .

-:

my $die_hard_4 = { code => 'DVD-144', title => 'Live Free or Die Hard', price => 14.99, stock => 2 };

:

my %inventory;
$inventory{'DVD-144'} = $die_hard_4;

, :

my %inventory_by_title;
$inventory_by_title{'Live Free or Die Hard'} = $die_hard_4;

, , - hashref, . :

my %inventory;
my %inventory_by_title;

while ( <> ) {   # for each line of input
    chomp;  # remove trailing newline
    my ($code, $title, $price, $amount) = split /\|/;  # split by '|' character
    my $item = {
        code => $code,
        title => $title,
        price => $price,
        stock => $amount,
    };
    $inventory{$code} = $item;
    $inventory_by_title{$title} = $item;
}

, .

+3
#!/usr/bin/perl

use strict; use warnings;
use YAML;

my @store;

while ( my $inv = <DATA> ) {
    chomp $inv;
    last unless $inv =~ /\S/;

    my ($id, $title, $price, $stock) = split qr{\|}, $inv;
    $stock ||= 0;
    my ($type, $code) = split /-/, $id;
    push @store, {
        type  => $type,
        code  => $code,
        title => $title,
        price => $price,
        stock => $stock,
    };
}

print "DVDs\n";
print Dump [ grep { $_->{type} eq 'DVD'} @store ];

print "Products that cost less than \$15\n";
print Dump [ grep { $_->{price} < 15 } @store ];

print "Products that are in stock\n";
print Dump [ grep { $_->{stock} } @store ];

print "Products with 'of' in the title\n";
print Dump [ grep { $_->{title} =~ /of/ } @store ];

__DATA__
MP3-72|Lady Gaga - Kiss and Run (Fear of Commitment Monster)|0.99
CD-400|Kings of Leon - Only By The Night|14.50|2
MP3-401|Kings of Leon - Closer|0.85
DVD-144|Live Free or Die Hard|14.99|2
SOFT-864|Windows Vista|49.95
+1

, ​​ sqlite, mysql .., . sql /// .

0

. , .

, " ", :

$inventory{$title} = $product_code;
$inventory{$product_code} = $title;

, , "DVD123". :

$inventory_by_title{$title} = ...
$inventory_by_code{$product_code} = ...
0

Source: https://habr.com/ru/post/1737808/


All Articles