Home > Enterprise >  Loop through an array ignores \n
Loop through an array ignores \n

Time:09-09

@edit Below is the JSON getting saved as $content

[
    {
        "date_created": 1234,
        "fingerprint_hash": null,
        "address": "xx:xx:xx:xx:xx:xx",
        "name": null,
        "manufacturer": "xxx",
        "date_updated": 1234,
        "active": true,
        "date_last_active": 1234,
        "mac_group_info": {
            "name": null,
            "id": 0,
            "remarks": null
        },
        "id": 1234,
        "remarks": null,
        "arp_mapping_info": [
            {
                "ip_info": {
                    "date_changed": 1234,
                    "date_last_kerberos_login": null,
                    "dns_name": "xxxx",
                    "id": 6,
                    "remarks": null,
                    "date_created": 1234,
                    "kerberos_user_name": null,
                    "ip_group_info": {
                        "remarks": null,
                        "id": 0,
                        "name": null
                    },
                    "address": "x.x.x.x"
                },
                "interface_info": [
                    {
                        "link": "on",
                        "name": "x",
                        "discovery_info": null,
                        "speed": 1234,
                        "host_info": {
                            "backup_sensor_id": 0,
                            "type": "xxxx",
                            "radius_coa_flags": null,
                            "moment": 1,
                            "name": null,
                            "host_group_info": {
                                "name": null,
                                "id": 0,
                                "remarks": null
                            },
                            "interface_mib": "D",
                            "uplink_count": 1,
                            "sensor_info": {
                                "id": 1,
                                "os_version": "xxxx",
                                "architecture": "xxxx",
                                "remarks": null,
                                "last_contact": "xxxx",
                                "queue": 0,
                                "address": "x.x.xx",
                                "status": "active",
                                "os_name": "linux",
                                "name": "xxxx",
                                "version": "x.x.x-x"
                            },
                            "mode": "xx",
                            "address": "x.x.x.x",
                            "radius_secret": null,
                            "arp_mib": "2",
                            "cam_count": 1,
                            "discovery2_mib": "C",
                            "discovery1_mib": "3",
                            "radius_coa_port": null,
                            "radius_requests_count": 0,
                            "vlan_count": 1,
                            "access_to_port": 1,
                            "vlan_mib": "C",
                            "interval": 1,
                            "snmp_traps_community": null,
                            "engine_mode": "dynamic",
                            "manufacturer": "ciscoSystems",
                            "engine_id": null,
                            "snmp_traps_version": "1",
                            "mib_options": null,
                            "remarks": null,
                            "snmp_write_version": "2c",
                            "snmp_write_community": "private",
                            "access_to_cam": 1,
                            "status": "ok",
                            "access_to_cto": 1,
                            "access_to_interface": 1,
                            "snmp_read_version": "1",
                            "snmp_read_community": "public",
                            "id": 1,
                            "cam_mib": "C",
                            "arp_count": 1,
                            "access_to_arp": 1,
                            "port_count": 1,
                            "main_sensor_id": 1
                        },
                        "status": "on",
                        "index": 1,
                        "protocols": [
                            "ARP",
                            "NDP"
                        ],
                        "id": 1,
                        "remarks": null,
                        "sniffer_mode": null
                    }
                ],
                "host_id": 1
            }
        ],
        "cam_mapping_info": []
    }
]

Thats my code:

foreach ($content){
    $content =~ s/\[/\[\n\t/g;
    $content =~ s/{/{\n\t/g;
    $content =~ s/\]/\n\]/g;
    $content =~ s/}/}\n/g;
    $content =~ s/,/\n\t/g;
print $content;
}

Todo: get the same output as the JSON above but with a perl skript runing it withouth any modules, normaly we use postman for it. As I asked my "couch" for help he only said: just learn to programm. Yeah...

The output I get with my regex is almost the solution. It just stops tabing after the first tab, doent repeat it on another opening bracket.

CodePudding user response:

$line eq "{" || "[" is interpreted as

(($line eq '{') or '[')

(as Deparse would show you). Use

$line eq '{' || $line eq '['

Moreover, $data is a scalar variable, i.e. it contains a single value. Iterating over it makes just a single step.

Normally, you'd iterate over an array:

foreach my $line (@lines)

or maybe an array reference:

foreach my $line (@$data)

or whatever else

foreach my $line ($line1, $line2, @rest_of_lines)
# or
foreach my $line (split /\n/, $message)

CodePudding user response:

Using the YAML module, you can get a quick human readable format. I'm sure there are other and/or better ways to format your text, but this is one example. The benefit here is that you can transform a JSON data structure (using JSON::PP, a core Perl module that comes pre-installed) into a Perl data structure and then into a YAML structure that you can print easily. I did this with a few lines of code and a few modules, using some sample JSON data.

use strict;
use warnings;
use JSON::PP;
use YAML;

my $text = do { local $/; <DATA> };
my $json = decode_json($text);
print Dump $json;

__DATA__
{
    "ITEM":[
        {
            "-itemID": "1000000" ,
            "-itemName": "DisneyJuniorLA" ,
            "-thumbUrl": "" ,
            "-packageID": "1" ,
            "-itemPrice": "0" ,
            "-isLock": "true"
        },
        {
            "-itemID": "1000001" ,
            "-itemName": "31 minutos" ,
            "-thumbUrl": "" ,
            "-packageID": "1" ,
            "-itemPrice": "0" ,
            "-isLock": "true"
        },
        {
            "-itemID": "1000002" ,
            "-itemName": "Plaza SÚsamo" ,
            "-thumbUrl": "" ,
            "-packageID": "1" ,
            "-itemPrice": "0" ,
            "-isLock": "true"
        }
    ]
}

Output:

---
ITEM:
  - -isLock: true
    -itemID: 1000000
    -itemName: DisneyJuniorLA
    -itemPrice: 0
    -packageID: 1
    -thumbUrl: ''
  - -isLock: true
    -itemID: 1000001
    -itemName: 31 minutos
    -itemPrice: 0
    -packageID: 1
    -thumbUrl: ''
  - -isLock: true
    -itemID: 1000002
    -itemName: Plaza S┌samo
    -itemPrice: 0
    -packageID: 1
    -thumbUrl: ''

EDIT: With the new json data, I get this output:

- active: !!perl/scalar:JSON::PP::Boolean 1
  address: xx:xx:xx:xx:xx:xx
  arp_mapping_info:
    - host_id: 1
      interface_info:
        - discovery_info: ~
          host_info:
            access_to_arp: 1
            access_to_cam: 1
            access_to_cto: 1
            access_to_interface: 1
            access_to_port: 1
            address: x.x.x.x
            arp_count: 1
            arp_mib: 2
            backup_sensor_id: 0
            cam_count: 1
            cam_mib: C
            discovery1_mib: 3
            discovery2_mib: C
            engine_id: ~
            engine_mode: dynamic
            host_group_info:
              id: 0
              name: ~
              remarks: ~
            id: 1
            interface_mib: D
            interval: 1
            main_sensor_id: 1
            manufacturer: ciscoSystems
            mib_options: ~
            mode: xx
            moment: 1
            name: ~
            port_count: 1
            radius_coa_flags: ~
            radius_coa_port: ~
            radius_requests_count: 0
            radius_secret: ~
            remarks: ~
            sensor_info:
              address: x.x.xx
              architecture: xxxx
              id: 1
              last_contact: xxxx
              name: xxxx
              os_name: linux
              os_version: xxxx
              queue: 0
              remarks: ~
              status: active
              version: x.x.x-x
            snmp_read_community: public
            snmp_read_version: 1
            snmp_traps_community: ~
            snmp_traps_version: 1
            snmp_write_community: private
            snmp_write_version: 2c
            status: ok
            type: xxxx
            uplink_count: 1
            vlan_count: 1
            vlan_mib: C
          id: 1
          index: 1
          link: on
          name: x
          protocols:
            - ARP
            - NDP
          remarks: ~
          sniffer_mode: ~
          speed: 1234
          status: on
      ip_info:
        address: x.x.x.x
        date_changed: 1234
        date_created: 1234
        date_last_kerberos_login: ~
        dns_name: xxxx
        id: 6
        ip_group_info:
          id: 0
          name: ~
          remarks: ~
        kerberos_user_name: ~
        remarks: ~
  cam_mapping_info: []
  date_created: 1234
  date_last_active: 1234
  date_updated: 1234
  fingerprint_hash: ~
  id: 1234
  mac_group_info:
    id: 0
    name: ~
    remarks: ~
  manufacturer: xxx
  name: ~
  remarks: ~
  • Related