I am trying to read a line of text from a file, and use it to create an array called @list so that I can access the individual elements. The text file is called testhash.txt and at present contains a single line of text that reads:
'key1,value1','key2,value2','key3,value3','key4,value4'
The script behaves as if @list is a single line of text rather than an array. Yet if I simply copy and paste the line of text into @list like so:
@list=('key1,value1','key2,value2','key3,value3','key4,value4');
then I can do arrayish things with it.
Here is my script:
#testhash.txt has a single line of text that reads:
#'key1,value1','key2,value2','key3,value3','key4,value4'
open(HASH,"testhash.txt");
$line=(<HASH>);
chomp $line;
@list=($line);
@list=('key1,value1','key2,value2','key3,value3','key4,value4');
print "here is \@list:\n";
print $list[0];
print $list[3];
print "\nEnter the keycode:";
chop ($find=<STDIN>);
foreach (@list) {
($keycode,$keyvalue)=split /,/; #split up the array elements
if ($find=~/$keycode/i) {
print "Keycode $keycode has the value $keyvalue \n";
}
}
Any help would be much appreciated because I'm clearly missing some vital point here!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.