Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Help with logic/syntax

by choroba (Bishop)
on Jul 25, 2013 at 16:53 UTC ( #1046391=note: print w/replies, xml ) Need Help??

in reply to Help with logic/syntax

Your example code is problematic. Shouldn't \n be doublequoted?

I had no problems with using /^/ for the beginning of a line - maybe because I processed the records line by line.

#!/usr/bin/perl use warnings; use strict; $/ = "#"; while (<DATA>) { next if $. == 1; # Skipt the empty "first" record; my @lines = split /\n/; my ($vlan, $ifconfig) = (0, 0); for (@lines) { if (/^hostname/) { print "$_\n"; } elsif (/^vlan/) { $vlan++ for m/\S+/g; $vlan -= 3; } elsif (/^ifconfig/) { $ifconfig++ for m/\S+/g; $ifconfig -= 3; } } print "vlan $vlan\nifconfig $ifconfig\n"; print "WRONG!!!\n" if $vlan != $ifconfig; }
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Help with logic/syntax
by randompug (Initiate) on Jul 25, 2013 at 17:23 UTC

    With my code I was able output such as:

    $ ./ This is element : 0 This is element : 1 This is element : 2 This is element : 3 found a vlan line found an ifconfig line This is element : 4 found a vlan line found an ifconfig line

    Using choroba's code I had to change 1 thing, probably because I'm horrible at communicating :) I just commented out what to match on the ifconfig line

    } elsif (/^ifconfig/) { $ifconfig++ #for m/\S+/g; }

    because here I just want to count the lines themselves until we run out of ifconfig lines, not the number of words on each line like in the vlan example, then compare that with how many vifs we counted.

    At first I was getting this:

    hostname host97 vlan 72 ifconfig 504 WRONG!!!

    But now I get this:

    hostname host97 vlan 72 ifconfig 72

    Guess I was overcomplicating it by reading it into an array and then trying to iterate an array since I already have the data preformatted (from another script) when I opened the filehandle in this one. That too can be cleaner, but this is functional and that is what matters until we learn more!

    Thank you choroba! this gives me other ideas on other little things like this I'm trying to do!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046391]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-05-24 05:17 GMT
Find Nodes?
    Voting Booth?