我该如何使用Perl哈希表来存储库存?

3
为了完成大学的一项作业,我们需要使用Perl编写一个脚本来管理电子商店的库存(例如Amazon)。用户可以在完全基于文本的环境中下订单,当订单完成时,库存必须得到更新。
库存中的每个物品都有3到4个属性:产品代码、标题、价格以及某些物品的数量(例如MP3没有这个属性)。
由于这是我第一次接触Perl,我不知道如何开始。我的主要问题是如何在程序中“实现”库存。程序的一个功能是通过标题进行搜索。另一个功能是下订单,用户需要提供产品代码。
我的第一个想法是使用产品代码作为键的哈希表。但是如果我想在标题中搜索,那可能会有问题,因为哈希键会是像DVD-123这样的东西,属于该键的信息可能是“绿色面具12”(不带引号),其中的12表示目前库存中有多少这种DVD。所以我必须找到一种方法来忽略末尾的12。
另一个解决方案是将标题用作键,但我认为这也很麻烦。
是否有一种方法可以制作一个具有2个键的哈希表,当我只给出一个键时,它会返回包含其他值的数组?(包括另一个键和其他信息)这样,我可以根据需要从我的库存中使用另一个键。
我们需要从类似于以下文本文件中读取默认库存:
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
你的英语非常好。我见过很多美国学校毕业生犯的错误更糟糕。 - daotoad
您可能会发现 perldsc 和 perlreftut perldoc 文章很有用。可以使用命令“perldoc perldsc”在计算机上阅读它们。您还可以在 perldoc.perl.org 上找到文档 - http://perldoc.perl.org/perlreftut.html 。当您刚开始使用 Perl 时,perlfunc 文章中的按类别分类的 Perl 函数部分也非常方便 -http://perldoc.perl.org/perlfunc.html#Perl-Functions-by-Category 。 - daotoad
4个回答

3

由于您的课程可能不包括SQL或数据库,因此您可能会发现将库存表示为哈希表是有用的(hash of hashes)

库存条目将是哈希引用:

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;

现在只需将库存文件格式解析为像上面那样的哈希引用即可。以下是一个快速示例:

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;
}

希望这能帮助您入门。

哈希引用与哈希不同:它们是使用{}(或\%hash)而不是()构造的,并且使用$hash->{key}而不是$hash{key}进行访问。在Perl中,哈希不能具有其他哈希或数组作为值,因此必须传递对它们的引用。 - rjh

1
#!/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

0

这里有很多问题。其中一个简单的问题是如何制作包含列表的哈希(hashes containing lists)

虽然没有两个键的哈希,但哈希可以愉快地指向“左侧和右侧”,例如:

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

只有在您确定不会有一个名为“DVD123”的光盘时,才可以使用单个哈希。使用两个哈希会更安全和更易读:

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

0

你可以使用数据库(如sqlite,mysql等)来存储库存数据,而不是文本文件。然后,您可以使用SQL命令从数据库中查询/更新/删除/选择并轻松操作您的库存数据


这似乎是一个相当不错的解决方案,但我也确信我们不被允许使用这样的工具。一切都必须只使用“基本”的Perl代码编写。(即没有外部使用)太糟糕了 :( - Harm De Weirdt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接