Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: get array number

by helphand (Pilgrim)
on Jan 20, 2006 at 06:14 UTC ( [id://524406]=note: print w/replies, xml ) Need Help??


in reply to get array number

A couple of comments;

  • you really really should;
    use strict; use warnings;
  • chomp() is safer than chop() in this context.

As for your question, if I understand it correctly, you need to fix the print statement to refer to your array variable, as written, it is simply printing the text that is enclosed in the quotes.

print "name = $LINEREC[0]\n"; print "postcode = $LINEREC[3]\n";

Scott

Replies are listed 'Best First'.
Re^2: get array number
by darrengan (Sexton) on Jan 20, 2006 at 06:24 UTC
    Hi Scott,

    Thanks for your feedback. The problem is that i might not know what array "Postcode" is and the sequence in the file might not be in orders e.g. it can be Postcode;Name;Address2;Address3;.....

    I still need to know postcode or other value is in which array. Is there any way to do it?

    Thanks...

      You wrote:
      I still need to know postcode or other value is in which array. Is there any way to do it?
      Of course there is some way :) We know that postcode is consists of numbers only, name does not include any number, and adress is words & numbers.
      So.. that is draft code for You.
      my @file; $file[0]="Alexey;123456789;USA some state some street home # 12\n"; $file[1]="20102020;Nikola;Russian Federation some state some street ho +me # 13\n"; $file[2]="England some state some street home # 14;5545487987;Max\n"; my @sorted_values; foreach $file_string(@file){#like your while(<>) chomp $file_string; my @string_values=split/;/,$file_string; my $sorted; foreach (@string_values){ if (/\D/ && !/\W/ ){#This is name $sorted->{NAME}=$_; }elsif(/\D/ && /\W/){#This is adress $sorted->{ADRESS}=$_; }else{#This is postcode $sorted->{POSTCODE}=$_; } } push @sorted_values, $sorted; } foreach (@sorted_values){ print "NAME: $_->{NAME} ADRESS: $_->{ADRESS} POSTCODE: $_->{POSTCODE} +\n"; }

      Provided you know the field names in advance, then ikegami's solution will work perfectly for you.

      Scott

      how do you know whose fild is the postcode? on the file have any way to know, cant you organize the file? sorry about my english...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found