Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

list from file

by persianswallow (Novice)
on Jul 29, 2011 at 20:24 UTC ( [id://917540]=perlquestion: print w/replies, xml ) Need Help??

persianswallow has asked for the wisdom of the Perl Monks concerning the following question:

hi friends.in this following code i want to create list instead of my $list in the script from a text file that has a word in each line.please help me.

#!/usr/bin/perl use strict; use warnings; my $list = <<LIST; PF10417.3 PF11001.3 PF05.3 LIST my @domains1 = ('PF05.3', 'PF11001.3', 'PF00389.24', 'PF10417.3'); my @domains2 = ('PF01', 'PF02', 'PF11001.3', 'PF00389'); my @domains3 = ('PF00389.24', 'PF05.3', 'PF01', 'PF00389'); my %neighbours; for my $domain (\@domains1, \@domains2, \@domains3) { for my $idx (0 .. $#$domain) { my @near = grep {$_ >= 0 && $_ <= $#$domain} $idx - 1, $idx + +1; push @{$neighbours{$domain->[$idx]}}, map {$domain->[$_]} @nea +r; } } open my $inList, '<', \$list or die "cannot open 'domainlist'becuase:$ +!"; while (defined (my $domain = <$inList>)) { chomp $domain; print "Domain $domain has neighbours: @{$neighbours{$domain}}\n"; }

Replies are listed 'Best First'.
Re: list from file
by Not_a_Number (Prior) on Jul 29, 2011 at 21:37 UTC

    Please clarify what you mean by "i want to create list instead of my $list"

    (And by the way, you should acknowledge the source of the code you post...)

    Update: Linkified 'source'.

      this code works just when i have a list in script i mean i am limited with this list in script .i have different lists and i want to use them instead of list in the script. i want to make list from a file and use that in program. and yes,i took it from monk but i didnt know how i should show the source.:-(

        Based on your questions to date,
        917552 Re^2: list from file 2011-07-29 16:45 917540 list from file 2011-07-29 15:24 917434 Re^6: array in hashes 2011-07-29 05:44 917421 Re^4: array in hashes 2011-07-29 04:36 917414 Re^2: array in hashes 2011-07-29 04:04 917406 array in hashes 2011-07-29 03:14 917302 Re: array of arrays 2011-07-28 12:16 917230 Re^4: finding neighbors of an element in arrays 2011-07 +-28 04:42 917222 Re^4: finding neighbors of an element in arrays 2011-07 +-28 04:27 917211 finding neighbors of an element in arrays 2011-07-28 03 +:19
        it appears that it's time for you to start actually studying Perl (aka, one hopes, "learning Perl") rather than simply asking each question as it occurs to you.

        It's our goal to help you learn; not to provide an unlimited, question-by-question answer-service.

        Also, pay strict attention to Not_a_Number's request for clarity... and the caution about crediting the source of code you post; this community takes a very serious view of plagarism and its various cousins.

Re: list from file
by GrandFather (Saint) on Jul 29, 2011 at 22:00 UTC

    Replace \$list with a file name - it's as simple as that. See the documentation for open.

    The \$list syntax allows a string to be used as a file and is an excellent way of providing a "file" in a small stand alone sample script.

    Generally if you don't understand everything that is going on in a script you should read the documentation (in this case "perldoc -f open" would help), or ask someone. Simply copying code you don't understand is "cargo culting" and is an excellent way of getting into bad habits and wasting a lot of time.

    True laziness is hard work

      thank you so much for your help and show a good link.

      i have tried several ways to check my list. if i replace the file name instead of \$list the program will not use from my file.i tried to use this code instead to check my list element

      open (DOMAINLIST,'<domainlist') or die "cannot open 'domainlist'becuas +e:$!"; chomp(my @domain=<DOMAINLIST>); for my $domain(@domain){ my @array=@{$neighbours{$domain}};

      but there is a problem with reference that i couldn't solve it yet.then i came back to list.with replacing file name with \$list , my $list and its elements will not be useful , because \$list is a reference for that (i think) then i delete that,but still it doesn't work without error but no output.it shows that script hasn't opened the domainlist.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://917540]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found