[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

creating a hash

hey guys, I have a file like this:

name:filename
date:year
time:date

and i have a subroutine that splits each line of this file wherever there's a colon and it makes an array from it. Now, I want to create a hash where the keys are name, data and time and the values are filename, year and date. But before creating them I want to make sure that the key is a word in another array I have. If it is, then create a key "time" and a value "date" otherwise go to the next line. attached is what i have for right now:
if(-e gInput){
	my @input = &readinput(gInput);
	
		while (my $value = <@input>){
		if(&Check($value,@Attributes) {
			
			push(@{$ghash{$value}};
			
				if(&Check($value,@POptions)){
				
					push(@{$ghash{$value}},$value);
			
					}
				}
			}
		}
 
sub readinput{
	my $UserInput = shift;
	my @list;
	open(my $fh, "<", $UserInput) or die "Coudln't open file $!";
	close($UserInput);
		while(my $line = <$fh>){
				next if $line =~ /^\s*(?:#|$)/;
				push(@list,split(/:/,$line));
					}close($fh);					
	
	return @list;
					
}
 
sub Check{
	my ($value, @array) = @_;
	
		foreach my $element (@array)	
			{	
				if($element =~ /\Q$value\E/){
					return 1;
	      				}
				
			}
		return 0;
	}
 
sub pinputs{
	my @inputs = ("67","97","nup","up","1","2","3","Y","N"); 
	return @inputs;
}
 
sub attributes{
	my @attributes = ("UPDATE_BASELINE", "PROPERTIES", "MODE");
	return @attributes;
}

Open in new window

0
cucugirl
Asked:
cucugirl
  • 2
  • 2
1 Solution
 
ozoCommented:
@Attributes{@Attributes}=(1)x@Attributesy;
@POptions{@POptions}=(1)x@POptions;
{local @ARGV=("gInput");
 while( <>  ){
  my($k,$v)=split/:/;
  push @{$ghash{$k}},$v if $Attributes{$k} && $POptions{$v};
 }
}
 
0
 
ozoCommented:
if elements of @POptions do not end in $/, and the lines in the gInput file contain ony one :
then you may want to add a
  chomp;
before the
  my($k,$v)=split/:/;


0
 
vikaskhoriaCommented:
Check this script, I guess this should help.
#!/usr/bin/perl
 
open(INFILE, "input.txt");
@array = ("name","etc","time");
 
while(<INFILE>)
{
  @columns = split(/:/, $_);
  foreach (@array)
  {
        if ( $columns[0] eq $_ ) {
        $input_hash{$columns[0]}=$columns[1];
        }
  }
}

Open in new window

0
 
vikaskhoriaCommented:
There is one issue in my script, I forgot to chomp after reading the input.
You better do that as well.

so you insert:

while(<INFILE>)
{
   $line = $_;
   chomp($line);
@columns = split(/:/, $line);
...
...
...

}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now