perl – 如何读取文件并用名称替换值

perl – 如何读取文件并用名称替换值,第1张

概述我有一个文件,我将称之为源ID.我想读取该文件,并根据源ID为其添加名称,例如15431 = MyTV8.我有一个列出源ID和名称的文件,但我不知道如何读取原始ID并将通道映射文件中的名称添加到包含源ID的文件中. 我查询的源ID的一小部分示例 1343104112412911412001205500412900121251212001095953705380 我有一个文件,我将称之为源ID.我想读取该文件,并根据源ID为其添加名称,例如15431 = MyTV8.我有一个列出源ID和名称的文件,但我不知道如何读取原始ID并将通道映射文件中的名称添加到包含源ID的文件中.

我查询的源ID的一小部分示例

1343104112412911412001205500412900121251212001095953705380410902012101233411493004107568704008004166537842030120560000428641830041294037010555122091120588460053804719041664117415004183041231039104183415155314107417941854203420304107

它们只是一个文本文件,这里是源ID信道映射

1052 SCIFI                  1053 COMEDY                  1066 CSPAN2                  1067 ESPN CLASSIC            1100 SUNSHINE FOOTBALL       1133 TRUTV                   1241 SHOWTIME 2 EAST         1312 MC MetaL                1315 MC KIDS ONLY            1318 MC HIP HOP & R&B        1319 MC MC MIX TAPE          1324 MC liTE CLASSICAL       1326 MC SINGERS & SWING      1330 MC R&B CLASSICS         1332 MC MEXICANA             1334 MC RAGGAE               1338 MC HIP HOP CLASSICS     1339 MC SOUND OF SEASON      1340 MC HIT List             1346 MC PARTY FAVORITES      1348 MC POP HITS             2006 ColLEGE SPORTS TV       2469 WGCU WORLD              2470 WGCU ED TV              3014 ACTION MAX WEST         3048 SHOWTIME SHOWCASE 3     3051 STARZ! 1 WEST           3059 JEWELRY TV              3064 STARZ! IN BLACK         3068 WAM.                    3069 STARZ! 5 CINEMA WEST    3093 NICK TOO                4003 ACTION WEST             4008 NOGGIN                  4011 ENCORE DRAMA EAST       4029 logo MTV                4037 MOVIEPLEX               4068 FCS SOCC                4070 EWTN DIGITAL            4081 TELEFUTURA              4100 ADS-AMC                 4101 ADS-A&E                 4103 ADS-BET                 4104 ADS-BRVO                4105 ADS-C-SPAN              4107 ADS-TOON                4108 ADS-CMT                 4109 ADS-CNBC                4110 ADS-CNN                 4112 ADS-CMDY                4115 ADS-TRUTV/COURT-TV      4117 ADS-E!                  4119 ADS-ESPN                4120 ADS-ESPN2               4121 ADS-EWTN                4123 ADS-HALLMARK            4124 FIT TV                  4125 ADS-FX                  4127 ADS-CCTV                4128 ADS-HIST                4129 ADS-HGTV                4130 ADS-HSN                 4150 ADS-MSNBC               4151 ADS-MTV                 4156 ADS-NICK                4157 barKER                  4159 PLAYBOY DIR. CUT /CP    4163 ADS-STYLE               4164 ADS-QVC                 4166 ADS-SCIFI               4178 ADS-DSC                 4179 ADS-FAM                 4181 ADS-TLC                 4182 ADS-SPIKE               4183 ADS-USA                 4184 ADS-TWC-DIGITAL         4185 ADS-TNT                 4186 ADS-FOOD                4187 ADS-VH-1                4203 ADS-FXNEW               4241 ADS-TRAVL               4286 ADS-TCM                 4460 GAME SHOW               4485 MTV2.                   4545 SUNDANCE EAST           4557 ADS-TVL                 4702 ADS-VERSUS              4705 ADS-SPEED               4719 ADS-GolF                4730 ADS-WGN                 5012 ADS TEST 1              5013 ADS TEST 2              5014 ADS TEST 3              5015 ADS TEST 4              5016 ADS TEST 5              5017 ADS TEST 6              5018 ADS TEST 7              5019 ADS TEST 8              5020 ADS TEST 9              5021 ADS TEST 10             5022 ADS TEST 11             5023 ADS TEST 12             5024 SUN SPORTS HD           5286 SHOP NBC.               5368 ADS-WEDU                5369 ADS-WXPX                5370 ADS-WFLA                5377 ADS-WTOG                5378 ADS-WTSP                6031 ADS-WRXY                6166 ADS-WWSB                6206 ADS-WZVN                6884 DXD / TOON disNEY       7064 MY38 WB HD              7077 BLOOMBERG TV            7085 BIG TEN                 7086 BIG TEN 2               7090 ADS-HALLMARK VS         7331 CURRENT TV              7795 G4.                     8282 BIOGRAPHY               8285 HISTORY INTER.          8434 ADS-WTTA                8445 OUTDOOR CH              8499 OXYGEN                  8675 DO IT YOURSELF          8841 WEATHERSCAN             8846 liFETIME MOVIE NET.     8888 BRAVO                   9144 NHL & MLB GAME 1        9145 NHL & MLB GAME 2        9145 NHL & MLB GAME 2        9145 NHL & MLB GAME 2        9146 NHL & MLB GAME 3        9146 NHL & MLB GAME 3        9147 NHL & MLB GAME 4        9147 NHL & MLB GAME 4        9147 NHL & MLB GAME 4        9148 NHL & MLB GAME 5        9148 NHL & MLB GAME 5        9148 NHL & MLB GAME 5        9149 NHL & MLB GAME 6        9149 NHL & MLB GAME 6        9149 NHL & MLB GAME 6        9150 NHL & MLB GAME 7        9150 NHL & MLB GAME 7        9150 NHL & MLB GAME 7        9151 NHL & MLB GAME 8        9151 NHL & MLB GAME 8        9151 NHL & MLB GAME 8        9152 NHL & MLB GAME 9        9152 NHL & MLB GAME 9        9152 NHL & MLB GAME 9        9153 NHL & MLB GAME 10       9153 NHL & MLB GAME 10       9201 FUSE                    9237 THEN / GAS              9472 C-SPAN3                 9544 TV GAMES                9550 SOAPNET                 9597 GAC                     10000 CINEMAX                 10001 ENCORE EAST             10002 HBO-2                   10004 HBO-3 SIGNATURE         10005 SHOWTIME EAST           10006 STARZ! 1 EAST           10008 disNEY CHANNEL.         10010 TMC                     10011 SHOWTIME EXTREME E      10012 HBO WEST                10014 CINEMAX WEST            10021 ADS-ANPL                10023 CINEMAX MORE MAX -2     10025 TMC XTRA 2 EAST         10026 ACTION MAX EAST         10028 THRILLER MAX EAST       10041 NICKTOONS               10042 MTV JAMS                10045 VH-1 SOul               10046 CMT PURE                10057 OUTERMAX EAST           10063 STARZ! 4 KIDS FAMILY    10065 STARZ! 5 CINEMA EAST    10067 VH-1 CLASSIC            10098 HBO LATINO E            10389 WORD                    10391 NATIONAL GEOGRAPHIC     10398 DAYSTAR TV              10475 SHOWTIME FAMILY         10555 @MAX EAST               10556 WOMANMAX                10558 5STARMAX EAST           10584 FCS ATL                 10585 FCS CEN                 10586 FCS PAC                 10911 SHOWTIME HDTV           10935 THEATER HD CMCHD4       10959 MTV HITS                10962 FINE liVING/COOK TV     11101 ADS-CW6/WTVK46          11126 ADS-pax / ION           11132 ADS-WMOR                11205 ADS-SNN                 11242 RFD-TV                  11299 ADS-ESPN HD             11320 ADS-liFE                11324 ADS-FSN-FLORIDA         11328 ADS-CC-SPORT-S          11406 ADS-DHC                 11411 ADS-SUN                 11412 ADS-CNN-HDLN            11414 ESPNU                   11431 NBA TV                  11446 SPORTSMAN               11493 TENNIS TV               11514 ESPN SPPV1              11515 ESPN SPPV2              11516 ESPN SPPV3              11517 ESPN SPPV4              11518 ESPN SPPV5              11519 ESPN SPPV6              11647 Gol TV                  11684 UNIVERSAL HD CMCHD2     11714 STARZ COMEDY            11716 STARZ HD                11855 TV ONE.                 11911 ADS-TNTHD               12002 CENTRIC                 12016 HBO EAST                12020 IFC                     12023 lovE STORIES E.         12027 MYSTERY EAST            12030 OVATION                 12032 SHOWTIME 2 EAST         12037 STARZ! EDGE             12044 WESTERNS EAST           12045 ACTION EAST             12049 disCOVERY KIDS          12050 PLANET GREEN            12051 disCOVERY SCIENCE       12055 WE.                     12056 MIliTARY CHANNEL.       12112 ESPNEWS                 12120 disC ID                 12125 NFL NETWORK             12130 FliX EAST               12174 SHOWTIME BEYOND EAST    12175 BBC AMERICA             12209 ADS-TBS                 12306 ADS-REALTY-TV-PC        12333 ADS-LOPC                12334 ADS-ACCESS-CH-19        12336 ADS-SaraSOTA-ED         12339 ADS-LO                  12340 ADS-TV-GUIDE            12393 WXPXHD                  12800 ADS-ESPN2HD             12810 WFTX FOX HD             12817 ADS-TBS HD              13008 HBO ZONE EAST           13011 HBO FAMILY EAST         13013 HBO COMEDY EAST         13017 WZVN PC HDTV            13039 WBBH PC HDTV            13050 NAT GEO WILD            13072 HALLMARK MOVIE CH       13104 GOSPEL MUSIC TV         13135 HBO HD CMCHD5           13136 MAX HD CMCHD5           13229 SPROUT "E" NETWORKS     13268 WFLA NBC HD             13289 WTVT FOX HD             13406 EMPLOYEE CHANNEL        13430 INDIEPLEX               13431 RETROPLEX               13467 ADS-SCI-FI HD           13551 PALADIEA HD             13738 HGTV HD CMCHD1          13745 NATGEO HD CMCHD3        13907 TV FOOD HD CMCHD2       14102 NFLHD                   14507 ADS-WNFM-8              14710 A&E HD CMCHD1           15000 IN DEMAND 1             15001 IN DEMAND 2             15045 SHORTEEZ ADulT          15067 NBA TEAM 1              15068 NBA TEAM 2              15069 NBA TEAM 3              15070 NBA TEAM 4              15071 NBA TEAM 5              15072 NBA TEAM 6              15073 NBA TEAM 7              15074 NBA TEAM 8              15075 NBA TEAM 9              15076 NBA TEAM 10             15128 REELZ                   15201 VERSUS - HD             15238 NHL & MLB GAME 14       15239 NHL & MLB GAME 13       15240 NHL & MLB GAME 12       15241 NHL & MLB GAME 11       15364 MTV SPANISH TR3         15542 PENTHOUSE/TEN ADulT     16116 ADS-FOX BUSInesS        16120 ANIMAL HD CMCHD4        16121 SCIENCE HD CMCHD8       16122 TDC HD CMCHD3           16123 TLC HD CMCHD5           16129 USA HD CMCHD3           16141 CNN HD CMCHD7           16240 liFETIME MOVIENET HD    16280 NHL NETWORK             16317 RETIREMENT TV           16346 FX HD CMCHD9            16403 GolF HD CMCHD7          16414 AMC HD CMCHD7           16430 BIOGRAPHY HD            16543 SPEED HD CMCHD9         16547 SPIKE HD                16686 disNEY HD CMCHD-8       16692 FAMILY HD CMCHD8        16713 NICKELODEON HD          16756 FOX NEWS HD CMCHD9      16788 WEDU HD                 16792 WTSP DIGITAL WEATHER    16793 WTSP HDTV               16798 liFETIME HD             16807 WWSB HDTV               16817 PLANET GREEN HD         17000 FRESH ADulT             17512 MLB NETWORK             17535 MLBHD                   18030 WGCU HD PBS             18324 headliNE NEWS HD        18333 TCM HD                  18411 TRU TV HD               18630 FOX SPORTSNET FL HD     19005 3DTV 1                  19421 3DTV 2                  20101 MC 90'S                 20102 MC ADulT ALTERNATIVE    20104 MC RAP                  20105 MC THROWBACK JAMZ       20106 MC CONTEMP CHRISTIAN    20107 MC CLASSIC COUNTRY      20108 MC TODAY'S COUNTRY      20110 MC ROCK                 20112 MC GOSPEL               20113 MC RETRO ROCK           20115 MC TODDLER TUnes        20116 MC SOFT ROCK            20119 MC SOUNDSCAPES          20120 MC JAZZ                 20121 MC SMOOTH JAZZ          20122 MC CLASSICAL MASTERP    20124 MC 80'S                 20126 MC EASY ListENING       20127 MC POP LATINO           20128 MC SolID GolD olDIES    20129 MC R&B SOul             20131 MC TRUE COUNTRY         20282 MC STAGE & SCREEN       20284 MC MUSICA URBANA        20287 MC TROPICALS            20289 MC CLASSIC ROCK         20338 MC CLASSIC ALTERNATV    20339 MC DANCE ELECTRONICA    20357 MC ALTERNATIVE          20358 MC ROMANCES             20359 MC BLUES                20360 MC 70'S                 21170 STARZ! HD CMCHD1        21203 SCI FI HD CMCHD2        21442 THC HD CMCHD4           23104 E! HD                   23105 TRAVEL HD               23106 CARTOON HD              23107 CSS HD.                 23109 NFL RED ZONE            23110 IND HDPPV               23111 IND HDPPV2              23112 ESPNU HD
解决方法 这里有一个通用的模式,就是这个:这是一个集交集问题,因此可以使用Perl“sets”轻松解决,我们倾向于称之为哈希:).

方法是创建一个数据文件的集合(即将其索引到哈希表中,如下所示:%set =(a => 1,b => 1,c => 1),实际值是无关紧要的,因为我们将使用“set membership operator”来测试它.

完成之后,通过其他数据集并在上面的%set中测试其中一个键(在您的情况下,源ID)的成员资格是一件简单的事情.

因此,假设频道映射文件采用以下格式:

ID,姓名

并且源ID文件每行只有一个ID,您可以执行以下 *** 作:

open my $CHANNEL_MAP,q{<},$channel_map_file_name or dIE $!;my %channel_map    = map {        chomp;        my @mapPing = split /,/;        $mapPing[0] => $mapPing[1] # not needed,just being explicit.      } <$CHANNEL_MAP>;close $CHANNEL_MAP or dIE $!;# source ID file passed as a command line argumentwhile (<>) {    chomp;    print "$_ = $channel_map{$_}\n"        if exists $channel_map{$_};}

但是,这似乎有点无意义,因为您似乎想要做的是按现有源ID过滤通道映射文件.在这种情况下,您可以简单地索引源ID文件(使用上面的技术),然后只需浏览通道映射文件并打印源ID中存在ID的每一行.

总结

以上是内存溢出为你收集整理的perl – 如何读取文件并用名称替换值全部内容,希望文章能够帮你解决perl – 如何读取文件并用名称替换值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239256.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存