I've got a code snippet below that reads in some data and maps the clip name with the clip number. I used a foreach loop to iterate through the data, split each line then put the data into a hash. I would like to change this foreach loop to map.

my attempt at using map failed as shown below. Ideally I would like something like:

$VAR1 = { 'D:\\Main\\Running a Business\\Euro\\EuroIssues' => 0 'D:\\Main\\Running a Business\\Euro\\EuroIssues' => 1 'D:\\Main\\News\\Business News\\about euro\\Introduction to the Eur +o' => 3
This is the output from the map command, dumped via Data::Dumper
$VAR1 = { '1,D:\\Main\\Running a Business\\Euro\\EuroIssues' => 13, '6,D:\\Main\\News\\Business News\\about euro\\Euro-Intro' => 13, '0,D:\\Main\\mainmenu' => 13, '4,D:\\Main\\News\\Business News\\about euro\\Euro Issues' => 13, '7,D:\\Main\\News\\Business News\\about euro\\whatis emu' => 13, '10,D:\\Main\\News\\Business News\\about euro\\what uk and emu' => 1 +3, '5,D:\\Main\\News\\Business News\\Bugs\\Millenium Bug Menu' => 13, '2,D:\\Main\\News\\Business News\\Main Business News Menu' => 13, '3,D:\\Main\\News\\Business News\\about euro\\Introduction to the Eu +ro' => 13, '9,D:\\Main\\News\\Business News\\about euro\\govt decide' => 13, '8,D:\\Main\\News\\Business News\\about euro\\your question answered +' => 13 };
Here is the code.
use strict; my (%ClipNames,@clips,@DataArray); @DataArray = <DATA>; @clips = grep { /^[0-9]+,[A-Z]:/ } @DataArray; # Extract clip info chomp (@clips); # QUERY: Can this foreach loop be replaced by a map? # Following map line does not work properly. # my %ClipNames = map { $_, $. } @clips; foreach (@clips) # Create hash of clip IDs & clip names. { my @ClipLine = split(/,/,$_); $ClipNames{$ClipLine[0]} = $ClipLine[1]; # Clip ID -> ClipName } use Data::Dumper; $Data::Dumper::Indent = 1; print Dumper(\%ClipNames); exit; __DATA__ 0,D:\Main\mainmenu 1,D:\Main\Running a Business\Euro\EuroIssues 2,D:\Main\News\Business News\Main Business News Menu 3,D:\Main\News\Business News\about euro\Introduction to the Euro 4,D:\Main\News\Business News\about euro\Euro Issues 5,D:\Main\News\Business News\Bugs\Millenium Bug Menu 6,D:\Main\News\Business News\about euro\Euro-Intro 7,D:\Main\News\Business News\about euro\whatis emu 8,D:\Main\News\Business News\about euro\your question answered 9,D:\Main\News\Business News\about euro\govt decide 10,D:\Main\News\Business News\about euro\what uk and emu
thanks for your help!

dmtelf


In reply to Data to hash via map problem by dmtelf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.