in reply to Re: Removing and replacing values from an array.
in thread Removing and replacing values from an array.

Thanks for the quick reply kennethk!

This is what I've been using as a guide so far, http://www.perl.org/books/beginning-perl/

The file's contents looks like this.

1[.]1[.]1[.]1 description 1[.]1[.]1[.]1 description 1[.]1[.]1[.]1 description url[.]com description url[.]com description url[.]com description url[.]com description

When done I want it to look like this.

1.1.1.1 1.1.1.1 1.1.1.1 url.com url.com url.com url.com

Or like this.

@ips 1.1.1.1 1.1.1.1 1.1.1.1 @urls url.com url.com url.com url.com

because eventually they'll need to be 2 files.

Again thanks for all the help!

Replies are listed 'Best First'.
Re^3: Removing and replacing values from an array.
by kennethk (Abbot) on Aug 18, 2014 at 17:14 UTC
    If I had this task, I would probably solve your issue with a one liner:
    perl -pibak -e 's/^\s.*//; s/\[\.\]/./g;' filename
    See -p, -i[extension], and -e commandline in perlrun for details on the command line flags. In script form, this might be implemented as (where I have now stored data rather than modifying the input file)
    #!/usr/bin/perl use warnings; use strict; my @ips; while (my $line = <STDIN>){ last if $line =~ !/\S/; $line =~ s/^\s.*//; $line =~ s/\[\.\]/./g; push @ips, $line; } my @urls; while (my $line = <STDIN>){ last if $line =~ !/\S/; $line =~ s/^\s.*//; $line =~ s/\[\.\]/./g; push @urls, $line; }
    or more simply
    #!/usr/bin/perl use warnings; use strict; my @ips; while (<>){ last if !/\S/; s/^\s.*//; s/\[\.\]/./g; push @ips, $_; } my @urls; while (<>){ last if !/\S/; s/^\s.*//; s/\[\.\]/./g; push @urls, $_; }
    See Loop Control in perlsyn for details on last.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Unless I misunderstood the OP's requirement, it seems to me that this code will push both URLs and IPs into both arrays. Something is needed, such as a regex, to distinguish them and put URLs and IPs only into the right array. Finally, I don't see any reason to read the input file twice. Reading the file, doing all the cleaning of description and brackets, and dispatching the data to the right array could all be done in one go. Something like this very quick (untested) example:
      my (@ips, @urls); while (<>){ last if !/\S/; s/\s+.*//; s/\[\.\]/./g; if (/[^\d\.]) { push @urls, $_; } else { push @ips, $_; } }
      The regex to match URLs could be refined (and I would refine it in my real programs), but that's just a quick example.

        this code will push both URLs and IPs into both arrays
        I'll assume you mean the script version, as opposed to the one-liner. You're forgetting that the reads are stateful. Specifically,
        while (<>){ last if !/\S/; ... }
        reads STDIN until it sees an empty line, and then bails. The second while (<>){ begins at that point, so if he has an empty line delimiting his IP and URL blocks, the first loop reads only the first block, and the second loop reads only the second. There is no seek, so I can' be reading the input twice.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks, that helps alot!!!