Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Read File into array and split

by tokyobuddha (Novice)
on Mar 07, 2008 at 02:46 UTC ( [id://672650]=perlquestion: print w/replies, xml ) Need Help??

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

First off i have a file with hostname and dir like so

hostname
"/etc/"
"/usr"
hostname

want to open file into array and. $client = shift(@array); and pass client and the dir into a sub. Thought split function would work.
my $file = '/scripts/clients'; open(DATA, $file) || die "Can't open $file: $!\n"; $raw_data=<DATA>; while (<DATA>) { foreach ($raw_data) { @new_data= split(/ms/, @raw_data); $client = shift(@new_data);

Thanks for your help

Replies are listed 'Best First'.
Re: Read File into array and split
by Joost (Canon) on Mar 07, 2008 at 02:52 UTC
    split works fine, provided you really want to split on the "ms" substring.

    this however:

    $raw_data=<DATA>; while (<DATA>) { foreach ($raw_data) {
    is almost certainly wrong and at least very confusing.

    you probably want:

    while (<DATA>) { my @new_data = split /ms/; # do stuff }
    Also note that the DATA file handle is special. you may want to use some other name if you're reading from some random file.

      Thanks for your help but my main goal is to put the
      hostname in to $client and the dir into @dir and each client starts with ms.
      open(DATA, "/scripts/clients"); while (<DATA>) { my @dir = split /ms/; foreach (@dir) { $client = shift(@new_data); backup($client);
      again thanks for your help.

        I'm not sure how you think split can return a list of dirs for every line, especially since the lines containing "ms" don't contains dirs.

        my main goal is to put the hostname in to $client and the dir into @dir and each client starts with ms.

        "starts with" implies split probably isn't the right tool.
        That you don't have a list of similar items implies split probably isn't the right tool.

        Assuming the data is as you showed in in your original post, and assuming that the hostnames are in square brackets and you simply didn't bother to fix your post, the following should do the trick.

        my $file = '/scripts/clients'; open(my $fh, '<', $file) or die("Unable to open file \"$file\": $!\n"); my $hostname; my @dirs; while (<$fh>) { chomp; if (/^\[/) { backup($client, \@dirs) if defined($hostname); s/^\[//; s/\]$//; $hostname = $_; @dirs = (); } else { s/^"//; s/"$//; push @dirs, $_; } } backup($client, \@dirs) if defined($hostname);

        DATA is a special name. You shouldn't use it. It's bad to use global variables anyway.

        Can you show some example data? Otherwise we are just guessing.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-19 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found