如何使用 Perl 哈希存储库存?

发布于 2024-08-25 16:45:36 字数 800 浏览 3 评论 0原文

对于大学作业,我们必须用 Perl 编写一个脚本来管理电子商店的库存。 (给出的例子是亚马逊)。用户可以在完全基于文本的环境中下订单,并且订单完成后必须更新库存。

库存中的每个项目都有 3 到 4 个属性:产品代码、标题、价格和某些金额(例如 MP3 没有此属性)

因为这是我第一次接触 Perl,所以我真的不知道如何开始。我的主要问题是我应该如何在程序中“实施”库存。该程序的功能之一是通过标题进行搜索。另一种是下订单,用户应提供产品代码。

我的第一个想法是以产品代码作为密钥的哈希。但如果我想搜索标题,这可能会成为一个问题: 哈希键类似于 DVD-123,属于该键的信息可能是“The Green Mask 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  

For an assignment in college, we have to make a script in Perl that allows us to manage an inventory for an e-store. (The example given was Amazon). Users can make orders in a fully text-based environment and the inventory must be updated when an order is completed.

Every item in the inventory has 3 to 4 attributes: a product code, a title, a price and for some an amount (MP3's for example do not have this attribute)

Since this is my first encounter with Perl, I don't really 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 searching trough the titles. Another is to make an order, where the user should give a product code.

My first idea was a hash with the productcode as key. But if I wanted to search in the titles that could be a problem because of this:
the hashkey would be something like DVD-123, the information belonging to that key could be "The Green Mask 12" (without quotes) where the 12 indicates how many of this DVD are currently in stock. So I'd have to find a way to ignore the 12 in the end.

Another solution was to use the title as the key, but that would prove cumbersome too I think.

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

We have to read the default inventory from a text file looking like this:

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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筱果果 2024-09-01 16:45:36

由于您的课程可能不涉及 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;

剩下的就是将库存文件格式解析为像下面这样的哈希引用多于。举一个简单的例子:

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

希望这可以帮助您入门。

Since your course presumably does not cover SQL or databases, you may find it useful to represent your inventory as a hash of hashes.

Inventory items will be hash references:

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

Your inventory itself will be a hash:

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

You can create another hash to index your inventory by title:

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

All that remains is to parse the inventory file format into a hashref like the one above. As a quick example:

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

Hope this helps you get started.

笑脸一如从前 2024-09-01 16:45:36
#!/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
#!/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
梦在深巷 2024-09-01 16:45:36

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

you can use a database such as sqlite, mysql etc to store you inventory data instead of text files. You can then use sql commands to query/update/delete/select from the database and manipulate your inventory data easily

你げ笑在眉眼 2024-09-01 16:45:36

里面有很多问题。其中一个简单的方法是如何制作包含列表的哈希

不存在具有两个键的散列,但散列很乐意指向“向左和向右”,例如:

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

当且仅当您可以确定您不会拥有标题为“DVD123”的光盘时。使用两个哈希会更安全且更具可读性:

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

There are a lot of questions in there. One of the easy ones is how to make hashes containing lists.

There isn't a hash with two keys, but hashes are happy to point "leftward and rightward", for example:

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

if and only if you can be certain that you won't have a disc titled "DVD123". Using two hashes would be safer and more readable:

$inventory_by_title{$title} = ...
$inventory_by_code{$product_code} = ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文