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

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.

Replies are listed 'Best First'.
Re^4: Removing and replacing values from an array.
by Laurent_R (Canon) on Aug 18, 2014 at 17:52 UTC
    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.

        Sorry, Kenneth, I had not seen anywhere any specification that there is an empty line delimiting the IP and URL blocks. Rereading the whole thread, it is true that the data pseudo-sample provided has one such separator, but is there any guarantee that there are just one IP block and one URL block and not several blocks of each type? Or that the block will always really be in that order?

        I personally would not rely on that unless I have a firm commitment from the data supplier that it will be always this way and exactly this way (or unless I am producing myself the data, say in another application). In brief, I still think it would be safer and more robust to perform some validation of the type of data at hand (but, of course, with a better regex than the really basic one I provided).

Re^4: Removing and replacing values from an array.
by douggie305 (Initiate) on Aug 18, 2014 at 17:26 UTC

    Thanks, that helps alot!!!